Visit Our UNIX and Linux User Community

Getopts - What am I missing?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getopts - What am I missing?
# 1  
Old 04-03-2020
Getopts - What am I missing?


According to my knowledge and my re-searches on the forum, the getopts block in this code should work....
	bool2str() { # [options] $?
	# Returns a string from a BOOL input
		swarm.protect "$FUNCNAME" "${@}" && exit 1
		# Defaults
			local caps #=false
			local CAPS #=false
			local mode #="yes"
			local out=""
			echo " ---------- $@ ----------- "
		# Catching Arguments
			# A ':' after a char indicates that this option requires an argument
			# Get access to the passed value of the argument by using $OPTARG
			while getopts "cCdoty" opt
			 	case ${opt} in
				"c")	caps=true
						shift	;;
				"C")	CAPS=true
						shift	;;
				"d")	mode="done"
						echo " -- SHOULD -- get here"	;;
				"t")	mode="true"
						shift	;;
				"y")	mode="yes"
						shift	;;
				"o")	mode="on"
						shift	;;
			#	*)	echo opt $opt ; exit 99	;;
			shift $(($OPTIND - 1))
		# Display & Action
		# This also handles translations
			case "${mode}" in
			"done")	case "$1" in
				0)	out="$SWARM_MSG_BOOL2STR_DONE"	;;
				1)	out="$SWARM_MSG_BOOL2STR_FAIL"	;;
			"true")	case "$1" in
				0)	out="$SWARM_MSG_BOOL2STR_TRUE"	;;
				1)	out="$SWARM_MSG_BOOL2STR_FALSE"	;;
			"on")	case "$1" in
				0)	out="$SWARM_MSG_BOOL2STR_ON"	;;
				1)	out="$SWARM_MSG_BOOL2STR_OFF"	;;
			"yes"|*)	case "$1" in
				0)	out="$SWARM_MSG_BOOL2STR_YES"	;;
				1)	out="$SWARM_MSG_BOOL2STR_NO"	;;

			# Show the output
			if ${CAPS:-false}
			then	$PRINTF "${out^^}"
			elif ${caps:-false}
			then	$PRINTF "${out^}"
			else	$PRINTF "$out"

However, once the function is sourced and I execute the code like:
set -x ; bool2str -d 1 ; set +x

All I get is this:
+ bool2str -d 1
+ swarm.protect bool2str -d 1
+ typeset arg ac=0
+ local fname=bool2str
+ shift
+ for arg in "$@"
+ ac=1
+ case "$arg" in
+ for arg in "$@"
+ ac=2
+ case "$arg" in
+ return 1
+ local caps
+ local CAPS
+ local mode
+ local out=
+ echo ' ---------- -d' '1 ----------- '
 ---------- -d 1 ----------- 
+ getopts cCdoty opt
+ shift 1
+ case "${mode}" in
+ case "$1" in
+ out=no
+ false
+ false
+ printf no
no+ set +x

Expected output:
'fail' instead of 'no' (yes/no is default, but should be changed by/with the passed option '-d' to 'done/fail')

Up to return 1 it's within the function swarm.protect which protects from code injection.

My biggest surprise is that it doesnt go into the "d" case - at all....
Not even when I changed the getopts case to "-d" rather than just "d".

Any ideas please?

Last edited by sea; 04-03-2020 at 09:11 PM..
# 2  
Old 04-03-2020
Oh dang, guess I cant just copy paste the argument handling from the previous files...

Originally Posted by man getopts
Note that shell functions share OPTIND with the calling shell even though the positional parameters are
changed. If the calling shell and any of its functions uses getopts to parse arguments, the results are un
Guess this is this kind of an unspecified behaviour...
Welp, guess I'll need to work around (read:without) getopts within functions.... Smilie

This User Gave Thanks to sea For This Post:
# 3  
Old 04-27-2020
Just came to mind - before this goes archive, if you look for a solution to this issue, here is mine:

for opt in "${@}"
do 	# Only parse for Arguments
	# if variable:'opt' starts with '-'
	[[ "-" == "${opt:0:1}" ]] && \
		case "${opt:1}" in
		"h" | "-help")
			echo "Usage: $FUNCNAME [options] arg1 arg2"
			exit 0
		"1" | "2" | "3")

Hope this helps and stay healthy

Last edited by sea; 04-30-2020 at 07:05 AM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #134
Difficulty: Easy
Linus Torvalds is known for disagreeing with other developers strongly and calls himself a 'really unpleasant person'.
True or False?

10 More Discussions You Might Find Interesting

1. Red Hat

Yum - resolving missing dependencies that are not missing

I am trying to install VirtualBox on RHEL 5 but I need the 32 bit version for 32 bit Windows. When I run yum I get the following: sudo yum localinstall /auto/spvtg-it/spvss-migration/Software/VirtualBox-4.3-4.3.2_90405_el6-1.i686.rpm Loaded plugins: fastestmirror Setting up Local Package... (13 Replies)
Discussion started by: gw1500se
13 Replies

2. SuSE

How to resolve missing missing dependencies with opensuse 11.3 and 12.3?

Hello, This is a programming question as well as a suse question, so let me know if you think I should post this in programming. I have an application that I compiled under opensuse 12.2 using g77-3.3/g++3.3. The program compiles and runs just fine. I gave the application to a colleague who... (2 Replies)
Discussion started by: LMHmedchem
2 Replies

3. Shell Programming and Scripting

Getopts how to handle missing '-' in command line args.

I'm using getopts to process command line args in a Bash script. The code looks like this: while getopts ":cfmvhs:t:" option; do case $option in c) operationMode="CHECK" ;; f) operationMode="FAST" ;; m) ... (6 Replies)
Discussion started by: gencon
6 Replies

4. Shell Programming and Scripting

Using getopts. Need help

Hi all... I have been looking on here for the past few days for an answer and Im gonna have to break down and ask. I just learned about the getopts command last week so have been trying to utilize it in my scripts. Below, I am trying to set up a case structure for options using getopts.... (1 Reply)
Discussion started by: losingit
1 Replies

5. Shell Programming and Scripting

? used in getopts

Suppose I have a code below . while getopts a: opt do case $opt in a) app_name="$OPTARG";; *) echo "$opt is an invalid option"; exit 1;; ?) echo "The value of $OPTARG is an invalid option"; exit 1;; esac done Could anyone please tell me in which case my... (1 Reply)
Discussion started by: maitree
1 Replies

6. UNIX for Dummies Questions & Answers


Hey, i need help with the use of getopts in shell script. tried reading a lot online, but found incomplete examples (maybe complete but cudn't make out). PLzz help...explain in deatil plzzz, i am a newbie:confused: (3 Replies)
Discussion started by: SasankaBITS
3 Replies

7. Shell Programming and Scripting

using getopts

Hi, I have a program where I want to use getopts. I want to use "-i" option and then optionally supply arguments. If user dosent supply arguments, then also it should work. Please tell me how to proceed. Here is some code, this is not right code btw but a sample to understand what I want to... (1 Reply)
Discussion started by: som.nitk
1 Replies

8. Shell Programming and Scripting

option followed by : taking next option if argument missing with getopts

Hi all, I am parsing command line options using getopts. The problem is that mandatory argument options following ":" is taking next option as argument if it is not followed by any argument. Below is the script: while getopts :hd:t:s:l:p:f: opt do case "$opt" in -h|-\?)... (2 Replies)
Discussion started by: gurukottur
2 Replies

9. Shell Programming and Scripting

help in getopts

hey need help with getopts again. i am using getopts to read my command line options and arguments. i can manage to do for options that have only one argument e.g srcipt_name -f 3 i am able to use getopts to do this but i am having problems two accept more than two agruments e.g.... (1 Reply)
Discussion started by: problems
1 Replies

10. Shell Programming and Scripting


I have a script which fires a command based on certain parameters. I am posting the code below.. The options needs be given such that -u option goes along with -d and -s, -f goes with -d and -t goes with -s and -d. 1) How do I ensure that user misses any of the option then he should be... (5 Replies)
Discussion started by: yerra
5 Replies

Featured Tech Videos