[root@hf-01 shell]# vim file1.sh [root@hf-01 shell]# cat !$ cat file1.sh #! /bin/bash n=`wc -l /tmp/lala` if [ -z "$n" ] then echo error exit elif [ $n -gt 100 ] then echo djsjdd fi [root@hf-01 shell]# sh -x file1.sh ++ wc -l /tmp/lala wc: /tmp/lala: 沒有那個文件或目錄 + n= + '[' -z '' ']' + echo error error + exit [root@hf-01 shell]#
[root@hf-01 shell]# vim file1.sh [root@hf-01 shell]# cat !$ cat file1.sh #! /bin/bash if [ ! -f /tmp/lala ] then echo "/tmp/lala not exit." exit fi n=`wc -l /tmp/lala` if [ -z "$n" ] then echo error exit elif [ $n -gt 100 ] then echo djsjdd fi [root@hf-01 shell]# sh file1.sh /tmp/lala not exit. [root@hf-01 shell]#
[root@hf-01 shell]# if [ -n 01.sh ]; then echo ok; fi ok [root@hf-01 shell]# echo $b [root@hf-01 shell]# if [ -n "$b" ]; then echo $b; else echo "b is null"; fi b is null [root@hf-01 shell]#
[root@hf-01 shell]# grep -w 'zabbix' /etc/passwd zabbix:x:998:995:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin [root@hf-01 shell]# if grep -wq 'zabbix' /etc/passwd; then echo "zabbix exist"; fi zabbix exist [root@hf-01 shell]#
if [ ! -e file ]; then 表示文件不存在時會怎麼樣shell
if (($a<1)); then …等同於 if [ $a -lt 1 ]; then…vim
[ ] 中不能使用<,>,==,!=,>=,<=這樣的符號bash