You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
total=$[ $1 * $2 ]
echo the first parameter is $1
echo the second number is $2
echo the total is $total
执行该脚本时候使用:./test 10 20;
可以在命令行上用文本字符串作为参数如:
echo Hello $1,glad to meet you;执行该脚本时候使用:./test ningbaoqi;每个参数都是用空格分隔的,所以shell会将空格当作成两个值的分隔符,要是在参数值中包含空格,必须使用引号(双引号和单引号都可),将文本字符串作为参数传递时,引号并非数据的一部分,他们只是表明数据的起始位置:如./test "ning bao qi;
如果脚本需要的命令行参数不止9个,就需要使用${10}的形式来表示:如:
total=$[ ${10} * ${11} ]
echo the parameter is ${10}
echo the parameter is ${11}
echo the total is $total
执行该脚本时候使用:./test 1 2 3 4 5 6 7 8 9 10 11 12;
读取脚本名
可以使用$0参数获取shell在命令行启动的脚本名,这在编写多功能工具时很方便;如:echo the zero parameter is set to :$0;注意在执行该脚本的时候使用:bash test;
basename命令会返回不包含路径的脚本名;如:
name = $(basename $0)
echo
echo the script name is : $name
count=1
while [ -n "$1" ]
do
echo "parameter #$count = $1"
count=$[ $count + 1 ]
shift
done
执行该脚本文件:./test one two three four;注意:使用shift命令的时候要小心,如果某个参数被移除,它的值就会被丢弃了,无法再恢复;可以一次性移动多个位置,只需要在shift命令提供一个参数,指明要移动的位置数就行了:如:
echo
echo "the original parameter is $*"
shift 2
echo "here's the new first parameter : $1"
执行该脚本文件:./test one two three four five;
处理选项
选项是跟当破折号后面对的单个字母,它能改变命令的行为;
查找选项
处理简单选项
echo
while [ -n "$1" ]
do
case "$1" in
-a) echo "found the -a option";;
-b) echo "found the -b option";;
-c) echo "found the -c option";;
*) echo "$1 is not an option";;
esac
shift
done
echo
while [ -n "$1" ]
do
case "$1" in
-a) echo "found the -a option";;
-b) echo "found the -b option";;
-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
echo
while [ -n "$1" ]
do
case "$1" in
-a) echo "found the -a option";;
-b) param="$2"
echo "found the -n 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
set -- $(getopt -q ab:cd "$@")
echo
while [ -n "$1" ]
do
case "$1" in
-a) echo "found the -a option";;
-b) param="$2"
echo "found the -b option while parameters 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
echo
while getopts :ab:c opt
do
case "$opt" in
a) echo "found the -a option";;
b) echo "found the -b option with value $OPTARG";;
c) echo "found the -c option";;
*) echo "unknow option:$opt";;
esac
done
echo
while getopts :ab:cd opt
do
case "$opt" in
a) echo "found the -a option";;
b) echo "found the -b option with value $OPTARG";;
c) echo "found the -c option";;
d) echo "found the -d option";;
*) echo "unknow option : $opt";;
esac
done
shift $[ $OPTIND -1 ]
echo
count=1
for param in "$@"
do
echo "parameter $count:$param"
count=$[ $count + 1 ]
done
执行该脚本可以使用:./ test -a -b test1 -d test2 test4 test4 test5;
read -n1 -p "do you want to continue [Y/N]?"answer //-n选项和值1一起使用,告诉read命令在接受单个字符后退出,只要接下单个字符回答后,read命令会接受输入并将它传给变量,无需按回车键
case $answer in
Y | y) echo
echo "fine continue on......";;
N | n) echo
echo "ok goodbye"
exit;;
esac
echo "this is the end of the script"