首页 / 操作系统 / Linux / shell编程中用户输入处理
shell编程中用户输入处理
1.命令行参数
2.脚本运行时获取输入命令行参数 通过空格来进行分割的
位置参数 :+position +position 0,1, 1, 2 ....
$0 :程序名
1, 1, 2,3... 3... 9
10及其以上的
${10}add.sh#/bin/bashecho "file is $0"echo "1->$1"echo "2->$2"echo "10->${10}"echo "11->${11}" ./add.sh 1 2 3 4 5 6 7 8 9 10 11
file is ./add.sh
1->1
2->2
10->10
11->11$0表示 命令行输入的/root/sh/f.sh#! /bin/bashecho `basename $0`echo `dirname $0`[root@localhost110 sh]# /root/sh/f.shf.sh/root/shcalc.sh#! /bin/bashname=`basename $0`if [ $name = "add" ]thenresult=$[$1+$2]elif [ $name="minus" ]thenresult=$[$1-$2]fiecho "the $name result is $result"注意if 后的[]与变量之间必须有空格chmod u+x calc.shln -s calc.sh add
ln -s calc.sh minus执行命令 ./add 1 2
the add result is 3
./minus 5 1
the minus result is 4命令行参数-特殊变量
1.参数计数(参数个数):$#
2.所有参数: $*
3.参数列表: $@test.sh#! /bin/bashecho $#echo $*echo $@echo "#######################"for var in "$*"doecho "$* param=$var"doneecho "########################"for var in "$@"doecho "$@ param=$var"done执行结果[root@localhost110 sh]# ./test.sh 1 2 js php41 2 js php1 2 js php#######################$* param=1 2 js php########################$@ param=1$@ param=2$@ param=js$@ param=php本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-11/137229.htm