Shell变量知识进阶

 

1.位置变量

2. $n给脚本传参

  • 给脚本传参(默认以空格分隔,如果想参数中包含空格需要加双引号,这样会作为一个字符串参数)

范例1:

root@whatislinux:/home/whatispython/workspace/shell# cat test6.sh 
echo $1
root@whatislinux:/home/whatispython/workspace/shell# sh test6.sh hello
hello
root@whatislinux:/home/whatispython/workspace/shell# sh test6.sh hello world    #默认以空格作为分隔符
hello
root@whatislinux:/home/whatispython/workspace/shell# sh test6.sh "hello world"  # 加引号后会将引号内的变量当成一个参数进行传递。
hello world

 

范例2:

root@whatislinux:/home/whatispython/workspace/shell# sh test7.sh zhangsan lisi wangwu zhaoliu
zhangsan lisi
root@whatislinux:/home/whatispython/workspace/shell# sh test7.sh "zhangsan lisi wangwu zhaoliu"
zhangsan lisi wangwu zhaoliu
root@whatislinux:/home/whatispython/workspace/shell# sh test7.sh "zhangsan lisi wangwu" zhaoliu
zhangsan lisi wangwu zhaoliu
root@whatislinux:/home/whatispython/workspace/shell# 

 

范例3:

root@whatislinux:/home/whatispython/workspace/shell# echo \${1..15} >test8.sh
root@whatislinux:/home/whatispython/workspace/shell# vim test8.sh 
root@whatislinux:/home/whatispython/workspace/shell# 
root@whatislinux:/home/whatispython/workspace/shell# 
root@whatislinux:/home/whatispython/workspace/shell# cat test8.sh 
echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15
root@whatislinux:/home/whatispython/workspace/shell# echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
root@whatislinux:/home/whatispython/workspace/shell# sh test8.sh {a..z}
a b c d e f g h i a0 a1 a2 a3 a4 a5

 


**输出结果与预期不符,位置参数$n,当n大于9时,输出结果就不对了,需要加上{},变成${n}**
root@whatislinux:/home/whatispython/workspace/shell# vim test8.sh 
root@whatislinux:/home/whatispython/workspace/shell# 
root@whatislinux:/home/whatispython/workspace/shell# 
root@whatislinux:/home/whatispython/workspace/shell# cat test8.sh 
echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} ${15}
root@whatislinux:/home/whatispython/workspace/shell# sh test8.sh {a..z}
a b c d e f g h i j k l m n o

 

 
位置变量$n在实际中的使用

服务启动命令如:/etc/init.d/mysqld start|stop|restart|status|reload,这几个参数都是通过位置变量加case语句实现的。

3.$0获取脚本名

获取脚本名,当执行脚本时带路径,会将路径一并输出。
范例1:

root@whatislinux:/home/whatispython/workspace/shell# vim test9.sh
root@whatislinux:/home/whatispython/workspace/shell# cat test9.sh 
echo $0
root@whatislinux:/home/whatispython/workspace/shell# sh test9.sh 
test9.sh
root@whatislinux:/home/whatispython/workspace/shell# cd
root@whatislinux:~# sh /home/whatispython/workspace/shell/test9.sh 
/home/whatispython/workspace/shell/test9.sh
root@whatislinux:~# cat test9.
cat: test9.: No such file or directory
root@whatislinux:~# cd -
/home/whatispython/workspace/shell
root@whatislinux:/home/whatispython/workspace/shell# 

 

范例2:
当执行脚本时带路径,$0会将路径一并输出。此时如果想单独获取路径和脚本名,可以使用dirname和basename命令

root@whatislinux:~# sh /home/whatispython/workspace/shell/test9.sh | dirname
dirname: missing operand
Try 'dirname --help' for more information.
root@whatislinux:~# dirname `sh /home/whatispython/workspace/shell/test9.sh`
/home/whatispython/workspace/shell
root@whatislinux:~# basename `sh /home/whatispython/workspace/shell/test9.sh`
test9.sh

 

位置变量$0在实际中的使用

当用户输入不符合脚本要求时,打印脚本名字和使用帮助。

3.$#:获取脚本传参个数

范例1:

root@whatislinux:/home/whatispython/workspace/shell# echo \${1..9} > test10.sh
root@whatislinux:/home/whatispython/workspace/shell# cat test10.sh 
$1 $2 $3 $4 $5 $6 $7 $8 $9
root@whatislinux:/home/whatispython/workspace/shell# vim test10.sh
root@whatislinux:/home/whatispython/workspace/shell# cat test10.sh
echo $1 $2 $3 $4 $5 $6 $7 $8 $9
root@whatislinux:/home/whatispython/workspace/shell# !vim
vim test10.sh
root@whatislinux:/home/whatispython/workspace/shell# !cat
cat test10.sh
echo $1 $2 $3 $4 $5 $6 $7 $8 $9
echo $#
root@whatislinux:/home/whatispython/workspace/shell# sh test10.sh {a..z}
a b c d e f g h i
26
root@whatislinux:/home/whatispython/workspace/shell# 

 

范例2:根据用户在命令行传参个数判断用户输入,不合要求的给予提示并退出

[ $# -ne 2 ] && {
 echo "USAGE:sh $0 arg1 arg2"
 exit 1
}

echo "success"

 

4.$∗和$@获取脚本传参个数

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">





posted @ 2019-01-30 17:27  whatislinux  阅读(103)  评论(0编辑  收藏  举报