Linux&shell之处理用户输入
写在前面:案例、常用、归类、解释说明。(By Jim)
命令行参数
$1为第一个参数,$2为第二个参数,依次类推...
示例:
#!/bin/bash # using one command line parameter factorial=1 for((number = 1;number<=$1;number++)) do factorial=$[ $factorial*$number ] done echo The factorial of $1 is $factorial
调用
./test1 5
(这样就把参数传递进去了)
结果:
The factorial of 5 is 120
#!/bin/bash # testing parameters before use if [ -n "$1" ] then echo Hello $1,glad to meet you. else echo "Sorry,you didn't identify yourself." fi
(最好能验证一下是否有参数传入,这样最安全了,编程时也要这样处理)
获取所有数据
$* 所有参数作为一个单词处理
$@ 所有参数作为多个单词处理
$# 最后一个参数
#!/bin/bash # testing $* and $@ echo "Using the \$* method:$*" echo "Using the \$@ method:$@" ./test1 rich jessica tom
结果:
Using the $* method:rich jessica tom
Using the $@ method:rich jessica tom
貌似没差别,不过我们来看一个例子,就知道差别了
#!/bin/bash # testing $* and $@ count=1 for param in "$*" do echo "\$* Parameter #$count = $param" count=$[ $count + 1 ] done count=1 for param in "$@" do echo "\$@ Parameter #$count = $param" count=$[ $count + 1 ] done
测试:test1 1 2 3 4 5
结果:
$* Parameter #1 = 1 2 3 4 5
$@ Parameter #1 = 1
$@ Parameter #2 = 2
$@ Parameter #3 = 3
$@ Parameter #4 = 4
$@ Parameter #5 = 5
(由结果就能看出差别了)
移位
shift将参数变量左移一个位置,于是,变量$3的值就移给变量$2,变量$2的值移给变量$1,变量$1的值被丢弃。
#!/bin/bash # demonstrating the shift command count=1 while [ -n "$1" ] do echo "Parameter #$count = $1" count=$[ $count +1 ] shift done
执行:
test1 1 2 3 4 5
结果:
Parameter #1 = 1
Parameter #2 = 2
Parameter #3 = 3
Parameter #4 = 4
Parameter #5 = 5
(每次向前移一位,最后被置空了)
shift 2表示移动两位
处理选项
#!/bin/bash # extracting options and parameters 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
测试:
test1 -a -b -c test1 test2 -c
结果:
Found the -a option
Found the -b option
Found the -c option
test1 is not an option
test2 is not an option
Found the -c option
(每一个参数都遍历一次)
测试2:test1 -a -b -c -- test1 test2 -a
结果:
Found the -a option
Found the -b option
Found the -c option
Parameter #1:test1
Parameter #2:test2
Parameter #3:-a
(跳出循环,并将剩余的参数存入$@中去,供下面的代码使用,即便有-a出现也不会被发现了)
使用getopt命令
getopt -q ab:cd -a -b -test1 -cde test2 test3
(表示有a,b,c,d,其中b后面跟一个参数)
解析的结果为
-a -b '-test1' -c -d -- 'test2' 'test3'
getopt -q ab:cd -a -b -test1 -d -c -cd test2 -c test3
解析结果为
-a -b '-test1' -d -c -c -d -c -- 'test2' 'test3'
(由此可以看出,它会将所有的选项与参数分离,按照一定的顺序,之间会自动加上--)
来看一段完整的代码
#!/bin/bash # extracting options and parameters set -- `getopt -q ab:c "$@"` 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
测试:test1 -a -b test1 -cd test2 test3
结果:
Found the -a option
Found the -b option,with parameter value 'test1'
Found the -c option
Parameter #1:'test2'
Parameter #2:'test3'
(可以想象它就是按照解析的数据进行处理的)
getopts和它的堂兄弟getopt很相像。
案例:
#!/bin/bash # simple demonstration of the getopts command while getopts :ab:c opt do case "$opt" in a) echo "Found the -a option" ;; b) echo "Found the -b option,with parameter value $OPTARG";; c) echo "Found the -c option" ;; *) echo "Unknown option:$opt" ;; esac done
(它会自动处理移位,自动处理参数到变量$OPTARG,自动处理)
测试:
test1 -ab test1 -c -d
结果:
Found the -a option
Found the -b option,with parameter value test1
Found the -c option
Unknown option:?
getopts命令每个处理选项,环境变量OPTIND的值会加1,。当到达getopts处理的末尾时,可以使用shift命令
和OPTIND值进行操作来移动到参数。
看代码:
#!/bin/bash # simple demonstration of the getopts command while getopts :ab:cd opt do case "$opt" in a) echo "Found the -a option" ;; b) echo "Found the -b option,with parameter value $OPTARG";; c) echo "Found the -c option" ;; d) echo "Found the -d option" ;; *) echo "Unknown option:$opt" ;; esac done shift $[ $OPTIND -1 ] count=1 for param in "$@" do echo "Parameter $count : $param" count=$[ $count+1 ] done
测试:test1 -a -b test1 -cd test2 test3
结果:
Found the -a option
Found the -b option,with parameter value test1
Found the -c option
Found the -d option
Parameter 1 : test2
Parameter 2 : test3
获取用户输入
基本读取
read命令接受标准输入,得到输入后,read命令将数据存放到一个标准变量中。
案例:
#!/bin/bash # testing read echo "Enter your name:" read name echo "Welcome ,$name !"
测试:
test1
Enter your name:
jim
Welcome ,jim !
#!/bin/bash # testing read echo -n "Enter your name:" read name echo "Welcome ,$name !"
测试:
[root@localhost shellscript]# test1
Enter your name:jim
Welcome ,jim !
(抑制后面新的字符出现,-n就不会换行了)
read有个-p命令,允许在read命令行中直接指定一个提示:
#!/bin/bash # testing read -p option read -p "Please enter your age:" age days=$[ $age * 360 ] echo "That makes you over $days days old!"
测试:
[root@localhost shellscript]# test1
Please enter your age:25
That makes you over 9000 days old!
(25是输入的)