1.$1,$2…${10},${11}代表传参 测试传参相加
#!/bin/bash echo $[$1+$2] xqkang@xqkang xqkang@xqkang:/qj/test$ bash test.sh 1 232 2332.踢掉传参shift shift默认是1,后面接n,代表踢掉n个参数 踢掉一个代码:
#!/bin/bash echo "First and Second pos $1,$2" shift echo "Third pos $1"测试:
xqkang@xqkang:/qj/test$ bash shift.sh 1 23 4 First and Second pos 1,23 Third pos 23踢掉两个代码:
#!/bin/bash echo "First and Second pos $1,$2" shift 2 echo "Third pos $1"测试:
xqkang@xqkang:/qj/test$ bash shift.sh 1 232 dd First and Second pos 1,232 Third pos dd练习:写一个脚本,通过命令传递连个稳健给脚本,计算其空白行数之和
#!/bin/bash file1=$(grep "^$" $1 | wc -l) file2=$(grep "^$" $2 | wc -l) echo "$1和$2连个文件的空白行之和为:$[ file1 + file2 ]" xqkang@xqkang:/qj/test$ bash lines.sh 222.txt 333.txt 222.txt和333.txt连个文件的空白行之和为:8发现:当没有写成$[ $file1 + $file2 ]也能正确引用
特殊变量: $0: 脚本文件路径名称
#!/bin/bash file1=$(grep "^$" $1 | wc -l) file2=$(grep "^$" $2 | wc -l) echo "$1和$2连个文件的空白行之和为:$[ $file1 + $file2 ]" echo $0 xqkang@xqkang:/qj/test$ bash lines.sh 222.txt 333.txt 222.txt和333.txt连个文件的空白行之和为:8 lines.sh xqkang@xqkang:/qj/test$ bash /qj/test/lines.sh 222.txt 333.txt 222.txt和333.txt连个文件的空白行之和为:8 /qj/test/lines.sh$#可显示传参个数
#!/bin/bash echo $# xqkang@xqkang:/qj/test$ bash pos.sh 11 22 2$*:所有参数 “hi” “hello” “come” $@:所有参数"hi hello come"