Sponsored Content
Full Discussion: Problem parsing args
Top Forums Shell Programming and Scripting Problem parsing args Post 302889536 by sea on Friday 21st of February 2014 10:23:11 AM
Old 02-21-2014
Problem parsing args

Heya

Tooltip: Parsing (getopts) for -u successfully sets mode=umnt, but case umnt is not executed,
instead it either executes/show help or regular mount screen.

I had copy pasted the structure of a getopts 'structure' from Man Page for getopts (posix Section 1) - The UNIX and Linux Forums but it seems my adaption has some bogus i dont see / figure out.

The script is ment to create a credentials file for mounting a nas, and an additional file for each share.
The creation of the config files and mounting work as it should.

However, when i want to unmount the share, it either shows the help screen (happens with none or wrong arguments) or the mount screen again.

The issue is, it doesnt seem to execute umount eventhough i passed -u and it did recognize it.
However, other arguments are executed properly...
Code:
[root@dell ~]# nas -e 192.168.10.110/Public
# |                                                 NAS Mount Helper (0.1)                                                  | #
todo edit

Some output of a set -x run while passing -u (italic in code section: Variable handling):
Code:
+ tui-title 'NAS Mount Helper (0.1)'
# |                                                 NAS Mount Helper (0.1)                                                  | #
+ '[' '!' 0 -eq 0 ']'
+ getopts edhmou: name
+ case $name in
+ doUnmount=true
+ mode=umnt
+ getopts edhmou: name
+ shift 2

And here is the code, followed by 1 successfull mount, and 2 umount tries:
Code:
[root@dell ~]# cat $(ls -l $(which nas)|awk '{print $11}')
#!/bin/sh
#	Author: 	sea
#	Contact:	erat.simon@gmail.com
#	Created:	2014.09.12
#	License:	CC
#	Description:	
#
#	Variable defaults
#
	source tui
	#set -x
	script_version=0.1
	ME="$(basename $0)"
	TITLE="NAS Mount Helper"
	LOCAL_IP="$(for STR in [a-z];do ip addr|awk '{print $2}'|grep ^$STR -A2|grep -iv ::|\
grep -iv 127|grep [0-9].[0-9].[0-9]|sed s,"/"," ",g|awk '{print $1}';done)"
	LOCAL_DOMAIN="$(hostname|sed s,'\.',' ',g|awk '{print $2}')"
	help_text="\n$ME ($script_version) - $TITLE
		\r\nUsage:\t$ME [options] [//]SERVERNAMEORIP/SHARENAME /local/mount/location [USERNAME PASSWORD [DOMAIN]]
		\rWhere options are:
		\r\t-h\tThis screen
		\r\t-u\tUnmount provided nas/share/s
		\r\t-d\tDelete provided nas/share/s
		\r\t-e\tEdit provided nas/share/s settings
		\r\t-o\tOverwrite provided nas/share/s settings
		\r\t-m\tDisplays a TUI/CLI menu to select from
		\rExamples:
		\r\tFirst time: \t\t\t$ME //$LOCAL_IP/Example /mnt/examples MyName MyPassword $LOCAL_DOMAIN
		\r\tLater times:\t\t\t$ME [$LOCAL_IP/]Example
		\r\tDelete single share:\t\t$ME -d [//]$LOCAL_IP/Example
		\r\tDelete nas configuration:\t$ME -d [//]$LOCAL_IP
		\r\n"
	ARGS=(${@})
	ARGS_COUNT=$#
	CONFIG="$HOME/.config/nas"
	#[ ! -t $ST_USER_NAS ] && CONFIG="$ST_USERS_NAS"
	[ -e $CONFIG ] || mkdir -p "$CONFIG/"{creds,shares}
	CONFIG_CREDS="$CONFIG/creds"
	CONFIG_SHARES="$CONFIG/shares"
	DEFAULT_MOUNT_OPTIONS=",iocharset=utf8,file_mode=0777,dir_mode=0777,sec=ntlmv2"
	doEdit=false
	doDelete=false
	doUnmount=false
	doOverwrite=false
	doMenu=false
	mode=mount

	tui-title "$TITLE ($script_version)"
	[ ! 0 -eq $UID ] && tui-echo "Requires Root Access" "$FAIL" && exit 1
#
#	Variable handling
#
	while getopts edhmou: name
	do 	case $name in
		e)	doEdit=true
			mode=edit		;;
		d)	doDelete=true
			mode=del		;;
		h)	printf "$help_text"
			exit $RET_HELP 		;;
		m)	doMenu=true
			mode=menu		;;
		o)	doOverwrite=true
			mode=ow		;;
		u)	doUnmount=true
			mode=umnt
			#shift $(($OPTIND - 1))
			;;
		?)	printf "$help_text"
			exit $RET_HELP	 	;;
		# d)	doEdit=true	;;
		# u)	printf 'blub "%s"\n\n';;
		esac
	done
	
	shift $(($OPTIND - 1))
	ARGS=(${*})
	ARGS_COUNT=$#

#
#	Exit if no arguments supplied, 
#		split first argument otherwise
#
	if [ -z $1 ] && [ ! $doMenu ] ; then
		printf "$help_text"
		exit $RET_HELP
	elif [ -z $1 ]
	then	printf "$help_text"
		exit $RET_HELP
	else	if [ ! $mode = mount ]
		then	export	SERVER=$(printf "$2"|sed s,'/',' ',g|awk '{print $1}') \
			SHARE=$(printf "$2"|sed s,'/',' ',g|awk '{print $2}')
		else	export	SERVER=$(printf "$1"|sed s,'/',' ',g|awk '{print $1}') \
			SHARE=$(printf "$1"|sed s,'/',' ',g|awk '{print $2}')
		fi
		export	CONFIG_NAS="$CONFIG_CREDS/$SERVER" \
			CONFIG_SHARE="$CONFIG_SHARES/$SERVER.$SHARE"
	fi
	#echo "$CONFIG_SHARE :: $mode $doMount";read

#
#	Functions
#
	FirstUse() { # SERVER/SHARE MOUNT [USERNAME PASSWORD [DOMAIN]]
	# Writes the supplied information into a credentials file
	# Defaults are: $HOME/.config/nas/{creds,shares}
		CredNAS() { # Username Password [DOMAIN]
		# Write NAS Credentials
		# 
			CREDS=""
			[ ! -z $1 ] && CREDS="username=\"$1\""
			[ ! -z $2 ] && CREDS="$CREDS\npassword=\"$2\""
			( [ ! -z $3 ] || [ ! "" = "$3" ] ) && CREDS="$CREDS\ndomain=\"$3\"\n"
			printf "$CREDS" > "$CONFIG_NAS"
		}
		ConfShare() { # ADRESS MOUNTPOINT
		# Write share config of given NAS
		#
			printf "adress=\"//$1\"\nmountpoint=\"$2\"\n" > "$CONFIG_SHARE"
		}
		CredNAS "$3" "$4" "$5"
		ConfShare "$1" "$2"
	}
	Mount() { # [SERVER/]SHARENAME
	# Descriptional
	# Text here
		#RESULTS=$(ls $CONFIG_SHARES)
		mountpoint=""
		this_file=""
		
		#if [ 1 -eq ${#RESULTS} ]
		#then	this_file=$RESULTS
		#elif [ 2 -gt ${#RESULTS} ]
		#then	select ITEM in $RESUlTS;do this_file=$ITEM;break;done
		#fi
		source $CONFIG_SHARE
		
		if [ -z $mountpoint ] 
		then 	tui-status 1 "Fatal Error, could not find credentials file"
			exit 1
		fi
		
		mount 	-t cifs \
			"$adress" \
			"$mountpoint" \
			-o "credentials=$CONFIG_NAS,$DEFAULT_MOUNT_OPTIONS"
		export mountpoint
	}
#
#	Display & Action
#
	case $mode in
	edit)	echo todo edit	;;
	del)	echo todo del	;;
	menu)	echo todo menu	;;
	ow)	echo todo over	;;
	umnt)	tui-header "1:$1 - serv:$SERVER - share:$SHARE"
		tui-printf "Unmounting $SERVER/$SHARE from $mountpoint" "$WORK" ; sleep 0.4
		source $CONFIG_SHARE
		umount $mountpoint
		tui-status $? "Unmounted $mountpoint"		;;
	mount)	RESULTS=$(ls $CONFIG_SHARES)
		if [ "" = "$(echo $RESULTS)" ] || [ ! -f $NAS_CRED ]
		then	FirstUse $*
			tui-status $? "Created: $CONFIG_NAS"
			tui-status $? "Created: $CONFIG_SHARE"
			[ 0 -eq $UID ] && tui-echo $USER
		fi
		
		source $CONFIG_SHARE
		tui-printf "Mounting $SERVER/$SHARE to $mountpoint" "$WORK" ; sleep 0.4
		
		Mount $SERVER/$SHARE
		tui-status $? "Mounted $SHARE to $mountpoint"
				;;
	esac
#
#	Clearance
#
	export CREDS="" SERVER="" SHARE="" CONFIG="" CONFIG_CRED="" CONFIG_SHARE="" \
		CONFIG_SHARES="" CONFIG_NAS="" RESULTS="" adress="" mountpoint=""

Code:
[root@dell ~]# nas 192.168.10.110/Public
# |                                                 NAS Mount Helper (0.1)                                                  | #
# | Mounted Public to /mnt/nas/                                                                                    [  ✔   ] | #
[root@dell ~]# nas -u 192.168.10.110/Public
# |                                                 NAS Mount Helper (0.1)                                                  | #

nas (0.1) - NAS Mount Helper
		
Usage:	nas [options] [//]SERVERNAMEORIP/SHARENAME /local/mount/location [USERNAME PASSWORD [DOMAIN]]
Where options are:
	-h	This screen
	-u	Unmount provided nas/share/s
	-d	Delete provided nas/share/s
	-e	Edit provided nas/share/s settings
	-o	Overwrite provided nas/share/s settings
	-m	Displays a TUI/CLI menu to select from
Examples:
	First time: 			nas //192.168.10.4/Example /mnt/examples MyName MyPassword MyDomain
	Later times:			nas [192.168.10.4/]Example
	Delete single share:		nas -d [//]192.168.10.4/Example
	Delete nas configuration:	nas -d [//]192.168.10.4
		
[root@dell ~]# nas 192.168.10.110/Public -u
# |                                                 NAS Mount Helper (0.1)                                                  | #
# | Mounting 192.168.10.110/Public to /mnt/nas/                                                                    [  ∞   ] | #
mount error(16): Device or resource busy
Refer to the mount.cifs(8) manual page (e.g. man mount.cifs)
# | Mounted Public to /mnt/nas/                                                                                    [  ✔   ] | #
[root@dell ~]#

Any idea why its showing the helpscreen eventhough the $mode was set to unmount (umnt)?
Thank you in advance

---------- Post updated at 16:23 ---------- Previous update was at 15:23 ----------

Its working, but not really sure why...

I removed all the OPTIND stuff, i thought that should remove the "-u" from the arglist, but neither OPTIND itself, nor OPTIND -1 helped on that matter (or i did wrong):
Code:
	while getopts edhmou: name
	do 	case $name in
		e)	doEdit=true
			mode=edit
			#shift $(($OPTIND - 1))
		;;
		d)	doDelete=true
			mode=del
		;;
		h)	printf "$help_text"
			exit $RET_HELP
 		;;
		m)	doMenu=true
			mode=menu
		;;
		o)	doOverwrite=true
			mode=ow
		;;
		u)	doUnmount=true
			mode=u
			#shift $(($OPTIND))
		;;
		?)	printf "$help_text"
			exit $RET_HELP
	 	;;
		esac
	done
	
	#shift $(($OPTIND - 1))

and
Code:
if [ -z $1 ] && [ ! $doMenu ] ; then
		printf "$help_text"
		exit $RET_HELP
	elif [ $ARGS_COUNT -lt 1 ]
	then	printf "$help_text"
		exit $RET_HELP
	else	if [ ! $mode = mount ]
		then	export	SERVER=$(printf "$2"|sed s,'/',' ',g|awk '{print $1}') \
				SHARE=$(printf "$2"|sed s,'/',' ',g|awk '{print $2}')
		else	export	SERVER=$(printf "$1"|sed s,'/',' ',g|awk '{print $1}') \
				SHARE=$(printf "$1"|sed s,'/',' ',g|awk '{print $2}')
		fi
		
		export	CONFIG_NAS="$CONFIG_CREDS/$SERVER" \
			CONFIG_SHARE="$CONFIG_SHARES/$SERVER.$SHARE"
	fi

Have a nice day Smilie
/solved

Last edited by sea; 02-21-2014 at 11:46 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

parsing problem with script

I'm trying to parse the variables out of a comma delimited expression, but i'm having trouble with script: num_var=1 while do a=`echo "a=7, b=8, c=9" | awk '{print $num_var}' | cut -d= -f2` b=`echo $a | cut -d, -f1` echo $b num_var=`expr $num_var + 1`... (5 Replies)
Discussion started by: mike@freddiemac
5 Replies

2. Shell Programming and Scripting

awk parsing problem

I need help with a problem that I have not been able to figure out. I have a file that is about 650K lines. Records are seperated by blank lines, fields seperated by new lines. I was trying to make a report that would add up 2 fields and associate them with a CP. example output would be... (11 Replies)
Discussion started by: timj123
11 Replies

3. Shell Programming and Scripting

Parsing problem

I need to separate out the contents in the string "xyz","1233","cm_asdfasdf" as xyz,1233,cm_asdfasdf Can anyone help me on this?? (1 Reply)
Discussion started by: Sushir03
1 Replies

4. Shell Programming and Scripting

Parsing problem

Hi, i need to parse a string which looks like this "xyz","1233","cm_asdfasdf" (2 Replies)
Discussion started by: Sushir03
2 Replies

5. Shell Programming and Scripting

Parsing problem

I need to parse a string which looks like "xyx","sdfsdf","asf_asdf" into var1="xyx" var2="sdfsdf" var3="asf_asdf" (3 Replies)
Discussion started by: Sushir03
3 Replies

6. Shell Programming and Scripting

Parsing Problem

Hi all, I am having problems parsing the following file: cat mylist one,two,three four five,six My goal is to get each number on a seperate line. one two three four five six I tried this command: sed -e 's/\,/^M/g' mylist (11 Replies)
Discussion started by: rob11g
11 Replies

7. Shell Programming and Scripting

problem with KSH script: command line args

Hi I am executing a KSH script by passing command line arguments example: Red Green Dark Red Blue when I am splitting the arguments by using " "(Space) as delimiter But the colour Dark Red is a single parameter. But it is getting splitted in between How to avoid this. Please help Also... (4 Replies)
Discussion started by: hemanth424
4 Replies

8. Shell Programming and Scripting

Parsing problem

Hello, I have a similar problem so I continue this thread. I have: my_script_to_format_nicely_bdf.sh | grep "RawData" |tr -s ' '|cut -d' ' -f 4|tr -d '%' So it supposed to return the percentage used of RawData FS: 80 (Want to use it in a alert script) However I also have a RawData2 FS so... (17 Replies)
Discussion started by: drbiloukos
17 Replies

9. Shell Programming and Scripting

Store args passed in array but not the first 2 args

Store args passed in array but not the first 2 args. # bash declare -a arr=("$@") s=$(IFS=, eval 'echo "${arr}"') echo "$s" output: sh array.sh 1 2 3 4 5 6 1,2,3,4,5,6 Desired output: sh array.sh 1 2 3 4 5 6 3,4,5,6 (2 Replies)
Discussion started by: iaav
2 Replies

10. Shell Programming and Scripting

Problem parsing

Hi, I want to fetch a text.Clipping. ... (5 Replies)
Discussion started by: protocomm
5 Replies
All times are GMT -4. The time now is 08:01 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy