unix shell getopts in a function

Hi
I like to parse my command line arguments in a function.
The following script does not work
var1=$1
var2=$2
DB_UserName=
DB_Password=

usage(){
    echo "usage: sqlexed [-u username ] [-p password]" 1>&2; exit 1;
}

processparameters(){
while getopts ":s:p:" o; do
    case "${o}" in
        s)
            DB_UserName=${OPTARG}
            ;;
        p)
            DB_Password=${OPTARG}
            ;;
        *)
            usage
            ;;
    esac
done
shift $((OPTIND-1))

if [ -z "${DB_UserName}" ] || [ -z "${DB_Password}" ]; then
    usage
fi
}

Main() {
processparameters $var1 $var2
}
Main | tee autosql.log


Not able to process the command line arguments. What do I have to change to be able to parse them in a function_
thx
mruffAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

murugesandinsShell_script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX C C++ CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NTCommented:
Code handling exception:
#!/bin/bash
CurrentShell=0
unset -f DoNotExecuteInCurrentShell
DoNotExecuteInCurrentShell ()
{
	if [[ "bash" = "$0" ]]
	then
		echo "Do not execute this script in current shell"
		CurrentShell=1
		return $CurrentShell
	fi
	Ret=$?
	return $Ret
}
# Display script usage
unset -f usage
usage ()
{
	echo "Usage:"
	echo "$0 -u RequiredUserName -p RequiredPassword"
	if [[ 0 -ne $# ]]
	then
		echo "$@"
	fi
	if [[ "bash" != "$0" ]]
	then
		exit 1;
	fi
}
# Set value for UserName and Password
# If no parameter passed to this function, call usage
# If any option excluding -u -p options are passed, call usage
# Set the value for UserName and Password for -u and -p parameters respectively.
unset -f ParserParameters
ParserParameters ()
{
	if [[ 4 -ne $# ]]
	then
		echo "Current usage:"
		echo "$0 $@"
		usage
	fi
	while getopts "u:p:" RequiredArgument;
	do
		case "${RequiredArgument}" in
			u)
				UserName=${OPTARG}
				;;
			p)
				Password=${OPTARG}
				;;
			*)
				usage
				;;
		esac
	done
}
DoNotExecuteInCurrentShell $@
if [[ 0 -eq $CurrentShell ]]
then
	# If no parameter passed to this function, call usage
	if [[ 0 -eq $# ]]
	then
		usage
	# If parameter passed count is not equal to four call usage
	elif [[ 4 -ne $# ]]
	then
		usage "Pass four parameters"
	else
		ParserParameters $@
		# Handle exceptions inside ParserParameters
		# if UserName or Password variables are not set, call usage
		if [ -z "${UserName}" ] || [ -z "${Password}" ];
		then
			usage
		else
			echo "UserName = ${UserName}"
			echo "Password = ${Password}"
		fi
	fi
fi

Open in new window

Test result:
$ ./29075713.sh
Usage:
./29075713.sh -u RequiredUserName -p RequiredPassword
$ ./29075713.sh -a
Usage:
./29075713.sh -u RequiredUserName -p RequiredPassword
Pass four parameters
$ ./29075713.sh -u
Usage:
./29075713.sh -u RequiredUserName -p RequiredPassword
Pass four parameters
$ ./29075713.sh -u -p
Usage:
./29075713.sh -u RequiredUserName -p RequiredPassword
Pass four parameters
$ ./29075713.sh -u -p
Usage:
./29075713.sh -u RequiredUserName -p RequiredPassword
Pass four parameters
$ ./29075713.sh -u $LOGNAME -p PASSWORD
UserName = murugesandins
Password = PASSWORD
$ . ./29075713.sh -u $LOGNAME -p PASSWORD
Do not execute this script in current shell
$ . ./29075713.sh
Do not execute this script in current shell

Open in new window


Based on google search [ like svn or cvs or your base version history :) ] it seems that you are using base version from:
https://stackoverflow.com/questions/16483119/example-of-how-to-use-getopts-in-bash
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mruffAuthor Commented:
Perfect, thx
I did not know how to pass the arguments to the function, that solved the issue> $@ as parameter
Many thanks
0
murugesandinsShell_script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX C C++ CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NTCommented:
Here goes updated code based on your requirement:
#!/bin/bash
CurrentShell=0
#Assign current time to AUTOMATED_SQL_LOG_FILE
CURRENT_TIME=''`/bin/date "+%d_%b_%Y_%H_%M_%S"`''
AUTOMATED_SQL_LOG_FILE="./autosql_$CURRENT_TIME"".log"
if [[ -f "$AUTOMATED_SQL_LOG_FILE" ]]
then
	#If AUTOMATED_SQL_LOG_FILE is present, sleep for 1 second and update AUTOMATED_SQL_LOG_FILE
	/bin/sleep 1
	CURRENT_TIME=''`/bin/date "+%d_%b_%Y_%H_%M_%S"`''
	AUTOMATED_SQL_LOG_FILE="./autosql_$CURRENT_TIME"".log"
	if [[ -f "$AUTOMATED_SQL_LOG_FILE" ]]
	then
		echo "Cannot execute same script more than one time"
		CurrentShell=1
	fi
fi
unset -f DoNotExecuteInCurrentShell
DoNotExecuteInCurrentShell ()
{
	if [[ "bash" = "$0" ]]
	then
		echo "Do not execute this script in current shell" 2>&1 | /usr/bin/tee -a $AUTOMATED_SQL_LOG_FILE
		CurrentShell=1
		return $CurrentShell
	fi
	Ret=$?
	return $Ret
}
# Display script usage
unset -f usage
usage ()
{
	CurrentShell=1
	echo "Usage:" | /usr/bin/tee -a $AUTOMATED_SQL_LOG_FILE
	echo "$0 -u RequiredUserName -p RequiredPassword" 2>&1 | /usr/bin/tee -a $AUTOMATED_SQL_LOG_FILE
	if [[ 0 -ne $# ]]
	then
		echo "$@" 2>&1 | /usr/bin/tee -a $AUTOMATED_SQL_LOG_FILE
	fi
	if [[ "bash" != "$0" ]]
	then
		exit 1;
	fi
}
# Set value for UserName and Password
# If no parameter passed to this function, call usage
# If any option excluding -u -p options are passed, call usage
# Set the value for UserName and Password for -u and -p parameters respectively.
unset -f ParserParameters
ParserParameters ()
{
	if [[ 4 -ne $# ]]
	then
		echo "Current usage:" 2>&1 | /usr/bin/tee -a $AUTOMATED_SQL_LOG_FILE
		echo "$0 $@" 2>&1 | /usr/bin/tee -a $AUTOMATED_SQL_LOG_FILE
		usage
	fi
	while getopts "u:p:" RequiredArgument;
	do
		case "${RequiredArgument}" in
			u)
				UserName=${OPTARG}
				;;
			p)
				Password=${OPTARG}
				;;
			*)
				usage
				;;
		esac
	done
}
unset -f Main
Main ()
{
	ParserParameters $@
	return $?
}
DoNotExecuteInCurrentShell $@
if [[ 0 -eq $CurrentShell ]]
then
	# If no parameter passed to this function, call usage
	if [[ 0 -eq $# ]]
	then
		usage
	# If parameter passed count is not equal to four call usage
	elif [[ 4 -ne $# ]]
	then
		usage "Pass four parameters"
	else
		Main $@
		# Handle exceptions inside ParserParameters
		# if UserName or Password variables are not set, call usage
		if [ -z "${UserName}" ] || [ -z "${Password}" ];
		then
			usage 2>&1 | /usr/bin/tee -a $AUTOMATED_SQL_LOG_FILE
		else
			echo "UserName = ${UserName}" | /usr/bin/tee -a $AUTOMATED_SQL_LOG_FILE
			echo "Password = ${Password}" | /usr/bin/tee -a $AUTOMATED_SQL_LOG_FILE
		fi
	fi
fi
if [[ -f "$AUTOMATED_SQL_LOG_FILE" ]]
then
	echo "Same output/error written in $AUTOMATED_SQL_LOG_FILE"
else
	echo "$AUTOMATED_SQL_LOG_FILE no file"
fi

Open in new window

Also completed the same testing again.
Sample result:
$ ./29075713.sh -u $LOGNAME -p PASSWORD
UserName = murugesandins
Password = PASSWORD
Same output/error written in ./autosql_29_Dec_2017_13_39_12.log
$ /bin/cat ./autosql_29_Dec_2017_13_39_12.log
UserName = murugesandins
Password = PASSWORD

Open in new window

0
murugesandinsShell_script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX C C++ CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NTCommented:
$ # echo "Advanced Happy new year 2018 to you and all" | /usr/bin/wc
$ # wc => welcome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.