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...
[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):
+ 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:
[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=""
[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):
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
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
/solved