内容纲要
4.1 命令行参数
- 命令行参数允许在运行脚本时向命令 行添加数据。
- bash shell通过==位置参数==分配参数:。位置参数变量是标准的数字:$0是程序名,$1是第 一个参数,$2是第二个参数,依次类推,直到第九个参数$9,在 第9个变量之后,你必须在变量数字周围加上花括号,比如${10}。
- 多个命令行参数,用空格分开,参数中有空格用引号括住(单引号和双引号均可)
$ cat test.sh
#!/bin/bash
echo the program name is $(basename $0)
echo the first parameter is $1
echo the second parameter is $2
echo the tenth parameter is ${10}
########the result:########
$ sh test.sh 1 2 3 4 5 6 7 8 9 10 11
the program name is test.sh
the first parameter is 1
the second parameter is 2
the tenth parameter is 10
其中==basename $0== 是返回不包含路径的脚本名。
4.2 特殊参数变量
变量 | 含义 |
---|---|
$# | 命令行参数的个数 |
${!#} | 最后 一个命令行参数变量 |
$* | 所有的命令行参数,一个整体 |
$@ | 所有的命令行参数,多个个体 |
#!/bin/bash
#$*
count=0
for val in "$*"
do
echo $val
count=$[ $count + 1 ]
done
echo "\$* val count:$count"
#$@
count=0
for val in "$@"
do
echo $val
count=$[ $count + 1 ]
done
echo "\$@ val count:$count"
########the resutl:########
$ sh test.sh 1 2 3
1 2 3
$* val count:1
1
2
3
$@ val count:3
4.3 shift命令
默认情况下它会将每个参数变量向左移动一个位置。所以,变量$3 的值会移到$2中,变量$2的值会移到$1中,而变量$1的值则会被删除(注意,变量$0的值,也 就是程序名,不会改变)。
#!/bin/bash
count=1
while [[ -n "$1" ]]; do
echo "Parameter #$count = $1"
count=$[ $count + 1 ]
shift
done
########the resutl:########
$ sh test.sh 1 2 3 4
Parameter #1 = 1
Parameter #2 = 2
Parameter #3 = 3
Parameter #4 = 4
一次移动多个位置,后面提供参数n即可:
shift n
4.4 获得用户输入,Read命令
#!/bin/bash
##001
#通过read 变量赋值
echo -n "Enter parameter value:"
read val
echo "Parameter value is $val"
##002
#-p 命令行指定提示符
read -p "Enter parameter value:" val
echo "Parameter value is $val"
##003
#环境变量REPLY:命令行中不指定变量,
#read命令会将它收到的任何数据都放进REPLY
read -p "Enter parameter value:"
echo
echo "Parameter value is $REPLY"
##004
#超时 -t选项来指定一个计时器,单位是秒
#当计时器过期后,read命令会返回一个非零退出状态码
if read -t 3 -p "Enter parameter value:"
then
echo "Parameter value is $REPLY"
else
echo 'Timeout'
fi
##005 隐藏方式读取
#-s选项可以避免在read命令中输入的数据出现在显示器上
#(实际上,数据会被显示,只是 read命令会将文本颜色设成跟背景色一样)
read -s -p "Enter your password:" password
echo
echo "Your password is $password"
##006 从文件中读取
#用read命令来读取Linux系统上文件里保存的数据。
#每次调用read命令,它都 会从文件中读取一行文本。
#当文件中再没有内容时,read命令会退出并返回非零退出状态码。
cat test.sh | while read line
do
echo "Line $count: $line"
count=$[ $count + 1]
done
##007 接受多个值
# -a 以数组的形式接受多个值
# ${#fruits[@]} 数组元素个数${#fruits[*]}也可以
# ${fruits[@]} 列出数组中所有的元素${fruits[*]}也可以,区别与$*,$@一样
echo -n "Enter your favorite fruit:"
read -a fruits
echo
echo "fruits count: ${#fruits[@]}"
for val in "${fruits[@]}"
do
echo $val
done
##008
#-n选项还允许检测箭头键和某些其他不寻常的键,但不包含回车键
# -n N 接受N个字符
# 用多个参数选项较麻烦,因为它们需要按正确的顺序排列。
read -s -n1 -p "Hit a key " keypress
echo;
echo "Keypress was "\"$keypress\""."
##009
# -r 通常,在读取输入期间,输入\会取消换行,r选项使输入的\当做普通字符处理
read -r -p "Enter parameter value:" val
echo "Parameter value is $val"
########the resutl:########
$ ./test.sh
Enter parameter value:abc\
Parameter value is abc\
read -p "Enter parameter value:" val
echo "Parameter value is $val"
########the resutl:########
$ ./test.sh
Enter parameter value:abc\
def\
ghi
Parameter value is abcdefghi
## ...
[转载请注明出处:
斜杠猿:http://www.80soho.com/?p=981]