处理用户输入
读取参数
bash shell会将一些成为位置参数的特殊变量分配给输入到命令行中的所有参数。位置参数变量是彼岸准的数字:$0是程序名,$1是第一个参数,$2是第二个参数.....直到第九个参数$9
举例:
[root@iZbp11f8g5h7oozejqy6k6Z shell]# sh test10.sh 5 The factorial of 5 is 120 [root@iZbp11f8g5h7oozejqy6k6Z shell]# vim test10.sh [root@iZbp11f8g5h7oozejqy6k6Z shell]# sh -x test10.sh 5 + varl1=1 + (( number = 1 )) + (( number <= 5 )) + varl1=1 + (( number++ )) + (( number <= 5 )) + varl1=2 + (( number++ )) + (( number <= 5 )) + varl1=6 + (( number++ )) + (( number <= 5 )) + varl1=24 + (( number++ )) + (( number <= 5 )) + varl1=120 + (( number++ )) + (( number <= 5 )) + echo The factorial of 5 is 120 The factorial of 5 is 120 [root@iZbp11f8g5h7oozejqy6k6Z shell]# cat test10.sh #! /bin/bash varl1=1 for ((number = 1;number <= $1;number++)) do varl1=$[ $varl1 * $number ] done echo The factorial of $1 is $varl1
参数统计
特殊变量$#含有脚本运行时携带的命令行参数的个数。
[root@iZbp11f8g5h7oozejqy6k6Z shell]# sh test11.sh 1112 232 43 21 There were 4 parameters supplied [root@iZbp11f8g5h7oozejqy6k6Z shell]# cat test11.sh #! /bin/bash echo There were $# parameters supplied
这边if-then语句可以用-ne来测试参数数量,如果参数数量不对,会显示一条错误消息告知脚本
抓取所有数据
$*和$@变量可以访问所有的参数
两者区别:$* 将所有参数当成单个参数 $@ 会单独处理每个参数
[root@iZbp11f8g5h7oozejqy6k6Z shell]# sh test12.sh 11 22 33 44 $* parameter #1=11 22 33 44 $@ parameter #1=11 $@ parameter #2=22 $@ parameter #3=33 $@ parameter #4=44 [root@iZbp11f8g5h7oozejqy6k6Z shell]# cat test12.sh #! /bin/bash echo count=1 for param in "$*" do echo "\$* parameter #$count=$param" count=$[ $count + 1 ] done echo count=1 for param in "$@" do echo "\$@ parameter #$count=$param" count=$[ $count + 1 ] done
移动变量
shift命令能够用来操作命令行参数,会根据他们的相对位置来移动命令行参数,默认情况下每个参数变量会向左移动一个位置
[root@iZbp11f8g5h7oozejqy6k6Z test2]# sh test1.sh 11 12 13 14 parameter 1=11 parameter 2=12 parameter 3=13 parameter 4=14 [root@iZbp11f8g5h7oozejqy6k6Z test2]# cat test1.sh #! /bin/bash echo count=1 while [ -n "$1" ] do echo "parameter $count=$1" count=$[ $count + 1 ] shift done
处理选项
分离参数和选项
在同时使用参数和选项的情况下,需要用特殊字符将二者分开
[root@iZbp11f8g5h7oozejqy6k6Z test2]# sh test2.sh -a -b -c -d -- r h found the -a option found the -b option found the -c option -d is not an option parameter #1=r parameter #2=h [root@iZbp11f8g5h7oozejqy6k6Z test2]# cat test2.sh #! /bin/bash 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
getopts命令
命令格式:
getopts optstring variable
有效的选项字母都会列在optstring中,如果选项字母要求有个参数,就加一个冒号。要去掉错误消息的话,可以在optstring之前加一个冒号,
getopts命令将当前参数保存在命令行中定义的variable中
[root@iZbp11f8g5h7oozejqy6k6Z test2]# cat test3.sh #! /bin/bash echo while getopts :ab:c opt do case $opt in a) echo "found an option a";; b) echo "found an option b,with value $OPTARG";; c) echo "found an option c";; *) echo "unkonw option $opt ";; esac done [root@iZbp11f8g5h7oozejqy6k6Z test2]# sh test3.sh [root@iZbp11f8g5h7oozejqy6k6Z test2]# sh test3.sh -ab test1 -c c found an option a found an option b,with value found an option c
[root@iZbp11f8g5h7oozejqy6k6Z test2]# sh test3.sh -ab test -cde found an option a found an option b,with value found an option c unkonw option ? unkonw option ?
在getopts处理完成时,可以使用shift命令和OPTIND值来移动参数
[root@iZbp11f8g5h7oozejqy6k6Z test2]# cat test4.sh #! /bin/bash while getopts :ab:cd opt do case $opt in a) echo "found the -a option";; b) echo "found the -b option,with the value $OPTARG";; c) echo "found the -c option";; d) echo "found the -d option";; *) echo "unkown the option:$opt";; esac done shift $[ $OPTIND -1 ] echo count=1 for param in $@ do echo "parameter $count:$param" count=$[ $count + 1 ] done [root@iZbp11f8g5h7oozejqy6k6Z test2]# sh test4.sh -a -b test1 -d test2 test3 found the -a option found the -b option,with the value test1 found the -d option parameter 1:test2 parameter 2:test3
获得用户输入
[root@iZbp11f8g5h7oozejqy6k6Z test2]# cat test5.sh #! /bin/bash read -p "please enter your age: " age days=$[ $age * 365 ] echo "that make you over $days days old " [root@iZbp11f8g5h7oozejqy6k6Z test2]# sh test5.sh please enter your age: 10 that make you over 3650 days old
也可以在read中不指定变量,read会将收到的所有数据都放进特殊环境变量的REPLY中
[root@iZbp11f8g5h7oozejqy6k6Z test2]# sh test5.sh please enter your age: 10 that make you over 3650 days old [root@iZbp11f8g5h7oozejqy6k6Z test2]# cat test5.sh #! /bin/bash read -p "please enter your age: " days=$[ $REPLY * 365 ] echo "that make you over $days days old "
可以让read来统计输入字符
[root@iZbp11f8g5h7oozejqy6k6Z test2]# sh test6.sh da you want to contiune [Y/N]? y fine,contiune on ... end.... [root@iZbp11f8g5h7oozejqy6k6Z test2]# sh test6.sh da you want to contiune [Y/N]? n okey, bey [root@iZbp11f8g5h7oozejqy6k6Z test2]# cat test6.sh #! /bin/bash read -n1 -p "da you want to contiune [Y/N]? " answer case $answer in Y|y ) echo echo "fine,contiune on ...";; N|n ) echo echo "okey, bey" exit;; esac echo "end...."
影藏方式读取
-s选项可以让read命令将文本颜色设置成跟背景颜色一样
[root@iZbp11f8g5h7oozejqy6k6Z test2]# sh test7.sh please enter your password: your pass is 12 [root@iZbp11f8g5h7oozejqy6k6Z test2]# cat test7.sh #! /bin/bash read -s -p "please enter your password: " pass echo echo "your pass is $pass"
从文件中读取
[root@iZbp11f8g5h7oozejqy6k6Z test2]# sh test8.sh Line 1:122312 Line 2:12434 1231 Line 3:21 342 Line 4:1222 v 43543 Line 5:asd 2342ewer gfd [root@iZbp11f8g5h7oozejqy6k6Z test2]# cat test8.sh #! /bin/bash count=1 cat test | while read line do echo "Line $count:$line" count=$[ $count + 1 ] done