- 论坛徽章:
- 145
|
回复 3# KEN6503
The getopts function check for options, you need check argument by yourself with $1 after shift
please refer below:
$ ./t.sh -f abc
file is : abc
abc
didn't get the argument
$ ./t.sh -f abc 1 2 3
file is : abc
abc
$ ./t.sh -f abc -a 1 2 3
file is : abc
abc
error
$ ./t.sh
didn't get the argument
$ cat t.sh
#!/bin/bash
while getopts :f: arg
do
case $arg in
f) echo file is : $OPTARG
# shift; shift
echo $OPTARG;;
\?) echo "error"
esac
done
while ((++n < OPTIND)); do shift; done
if [ "$1" == "" ]; then
echo "didn't get the argument"
exit
fi
|
|