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?
 
Murugesan NagarajanConnect With a Mentor Subject-matter expert at C++ C delivery, implementation, at UNIX oriented operating systems (Windows: CYGWIN_NT MINGW32_NT MINGW64_NT)Commented:
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
 
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
 
Murugesan NagarajanSubject-matter expert at C++ C delivery, implementation, at UNIX oriented operating systems (Windows: CYGWIN_NT MINGW32_NT MINGW64_NT)Commented:
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
 
Murugesan NagarajanSubject-matter expert at C++ C delivery, implementation, at UNIX oriented operating systems (Windows: CYGWIN_NT MINGW32_NT MINGW64_NT)Commented:
$ # echo "Advanced Happy new year 2018 to you and all" | /usr/bin/wc
$ # wc => welcome
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.