练习
写一个脚本,可接受一个文件路径作为参数,
如果参数个数小于1,则提示用户"至少给出一个参数",并立即退出
如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数
# cat -n not.sh 1 #!/bin/bash 2 test $# -lt 1 && echo "At least not now" && exit 1 3 lines=$(grep -c '^[[:space:]]$' $1) 4 echo "The lines of blank: $lines " 5 exit 2 # chmod +x not.sh
如果参数个数小于1,则提示用户"至少给出一个参数",并立即退出
# ./not.sh At least not now # echo $? 1
如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数
# ./not.sh /etc/rc.d/rc.sysinit The lines of blank: 3 # echo $? 2
如果hostname存在,或等于Localhost.domain,重命名为www.magedu.com
[ -z "$(hostname)" ] || [ "$(hostname)" == "localhost.domain" ] && hostname www.magedu.com
#字符测试中,只有PATTERN才不能用引号,且必须在` ` 中,其他必须用"",用在任意括号中
[ -z "$(hostname)" -o "$(hostname)" == "localhost.domain" ] && hostname www.magedu.com
/bin/cat 存在且可执行,运行cat fstab
# [ -e /bin/cat ] && [ -x /bin/cat ] && cat /etc/fstab
#文件和数值测试中不能用引号
# [ -e /bin/cat -a -x /bin/cat ] && cat /etc/fstab
转载于:https://blog.51cto.com/sonlich/1953090