浏览量 5056
2013/10/10 02:17
[root@localhost ~]# cat -n /test51
1 #!/bin/bash
2 while [ -n "$1" ]
3 do
4 case "$1" in
5 -a) echo "-a";;
6 -b) echo "-b";;
7 *) echo "nothing";;
8 esac
9 shift
10 done
[root@localhost ~]# sh /test51 -a d -b s
-a
nothing
-b
nothing
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
当出现双波折线-- shell就会停止处理选项,并将剩下的参数都当作命令行参数。
[root@localhost ~]# cat /test52
#!/bin/bash
while [ -n "$1" ]
do
case "$1" in
-a) echo "-a";;
-b) echo "-b";;
--) shift
break;;
*)echo "$1 is not an option";;
esac
shift
done
count=1
for param in $@
do
echo "parameter #$count: $param"
count=$[ $count +1 ]
done
[root@localhost ~]# sh /test52 da dfas"d" ss "s" -a df -b --test -- test3 tes
da is not an option
dfasd is not an option
ss is not an option
s is not an option
-a
df is not an option
-b
--test is not an option
parameter #1: test3
parameter #2: tes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@localhost ~]# cat /1
#!/bin/bash
while [ -n "$1" ]
do
case "$1" in
-a) echo " Found the -a option ";;
-b) param="$2"
echo " Found the -b option,with parameter value $param"
shift;;
-c) echo "Found the -c option ";;
--)shift
break;;
*) echo "$1 is not an option";;
esac
shift
done
count=1
for param in "$@"
do
echo "Parameter #$count: $param"
count=$[ $count + 1 ]
done
[root@localhost ~]# sh /1 -a b a -b test1 -d dsdfsd ds -- 32s
Found the -a option
b is not an option
a is not an option
Found the -b option,with parameter value test1
-d is not an option
dsdfsd is not an option
ds is not an option
Parameter #1: 32s
上一篇 搜索 下一篇