[BASH] getopts, OPTARG is passed but empty


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [BASH] getopts, OPTARG is passed but empty
# 1  
Old 04-26-2014
[BASH] getopts, OPTARG is passed but empty

EDIT: -- SOLVED --

Heyas,

Getting used to optargs, but by far not understanding it.
So i have that script that shall be 'changeable', trying to use the passed arguments to modify the script visuals.

Passing:
Code:
browser -t test -d sect $HOME

Where -t should change the title,
and -d should change the 'dir-label'.
While the title works, the 'dir-label' doesnt.

Code:
	set -x
	while getopts "m:d:f:t:u:h(help)": name
	do 	#echo "$name -- $OPTARG -- $OPTIND"	# DEBUG help	TODO rm when working
		case $name in
		t|title)	lbl_title="$OPTARG"	;;
		u|user) 	lbl_user="$OPTARG"	;;
		d|dir) 		lbl_dir="$OPTARG"	;;
		f|file) 	lbl_file="$OPTARG"	;;
		m|mode)		mode="$OPTARG"		;;
		h|help)		printf "$help_text"
				exit $RET_HELP		;;
		esac
	done
	shift $(($OPTIND - 1))
	set +x

	[ -z $lbl_dir ] && tui-echo "Error retrieving 'Folders'-label..." "$FAIL" && exit 1

Output 1:
Code:
$ tui-browser -t test -d sect $HOME
t -- test -- 3
d -- sect -- 5
# | Error retrieving 'Folders'-label...                                                                    [  ✘   ] | #

Output 2:
Code:
$ tui-browser -t test -d sect $HOME
+ getopts 'm:d:f:t:h(help):' name
+ echo 't -- test -- 3'
t -- test -- 3
+ case $name in
+ lbl_title=test
+ getopts 'm:d:f:t:h(help):' name
+ echo 'd -- sect -- 5'
d -- sect -- 5
+ case $name in
+ lbl_dir=
+ getopts 'm:d:f:t:h(help):' name
+ set +x
# | Error retrieving 'Folders'-label...                                                                    [  ✘   ] | #

I dont understand how lbl_dir is set to empty?
Could you bring in some light please?

Thank you in advance

EDIT:
Added the red-colored part, and it worked as expected, wierd...
Thought, i wasnt testing what i just added.. Smilie

Last edited by sea; 04-26-2014 at 06:53 AM.. Reason: typo
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash variable not being passed

In the bash below the variable date displays in the echo. However when I use it in the for loop it does not. Basically, the user inputs a date then that date is converted to the desired format of (month-day-year, no leading 0). That input is used in the for loop to return every file that matches... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. UNIX for Advanced & Expert Users

[BASH] Getopts/shift within a function, unexpected behaviour

Hello Gurus :) I'm "currently" (for the last ~2weeks) writing a script to build ffmpeg with some features from scratch. This said, there are quite a few features, libs, to be downloaded, compiled and installed, so figured, writing functions for some default tasks might help. Specialy since... (3 Replies)
Discussion started by: sea
3 Replies

3. Shell Programming and Scripting

Multiple bash variables passed into nawk

I have a file that has 2 fields called b_file: 11977 DAR.V3.20150209.1.CSV 3295 DAR.V3.20150209.1.CSV 1721 DAR.V2.20150210.1.CSV I need to search a sftplog using the field 1, but want to maintain the relationship between field 1 and 2. I am passing field 1 as a parameter in a bash loop. ... (14 Replies)
Discussion started by: smenago
14 Replies

4. Shell Programming and Scripting

Getopts - space in argument (OPTARG)

Hi, I want to capture space as well from the argument eg: script.ksh -m "Message from xyz" -e "email@xyz.com" script.ksh -m 'Message from xyz' -e 'email@xyz.com' I am parsing using getopts, but for option "m" OPTARG is returning only "Message". Please use code tags next time for... (9 Replies)
Discussion started by: tostay2003
9 Replies

5. Shell Programming and Scripting

[BASH] Using getopts

Heyas Just recently there was a thread about parsing arguments, where i read the first time about getopts. This said, i'd like to 'provide' a list function that can be 'trigered' using an 'option'(?). The regarding code snippets are: while getopts... (7 Replies)
Discussion started by: sea
7 Replies

6. Shell Programming and Scripting

bash:getopts command help

How can I say one of the options is required? can I use an if statement? let say: while getopts ":c:u:fp" opt; do case $opt in c) echo "-c was triggered, Parameter: $OPTARG" >&2;; u) echo "-u was triggered, Parameter: $OPTARG" >&2;; f) echo "-u was triggered,... (2 Replies)
Discussion started by: bashily
2 Replies

7. Shell Programming and Scripting

[bash] getopts not executing when called second time.

Hi, Unexpectedly, the function below doesn't seem to work when called a second time. The output shows that when the function is called the first time, it works as expected but when it is called a second time, the loop that processes the options passed to the function, with the while loop,... (2 Replies)
Discussion started by: ASGR
2 Replies

8. Shell Programming and Scripting

Parameters passed to commands but not working in Bash shell

Hi, I am trying to do this thing useing my shell bash ( sorry for my english ) I have in a file 63 hostnames, i wanna ask to the DHCP admin, to reserv that reserves 63 IP addresses of this hosts, using their mac address. I have thinked this script: for ((i=1;i<63;i++)); do arp $(head... (10 Replies)
Discussion started by: Cypress
10 Replies

9. Shell Programming and Scripting

bash array passed to oracle

Hi all.. Does anyone know have an example of passing the contents of a bash array to oracle? basically I am looking to loop through the contents of a file(not csv!) and store each line into a bash array. Once i have this I can then pass the array into an oracle procedure that accepts an array... (1 Reply)
Discussion started by: satnamx
1 Replies

10. Shell Programming and Scripting

How to check if two variable are empty strings at once? (bash)

I need to check if $1 or $2 are empty before continuing but I don't know if bash has any logic of the sort. This is what I'm looking for - except that "and" doesn't seem to work. if and ;then ... Thank you! :D (4 Replies)
Discussion started by: ph0enix
4 Replies
Login or Register to Ask a Question