标签:引号 没有 pre 用户 gre echo ber exist linu
shell中的逻辑判断[root@akuilinux01 shell]# cat if1.sh
#!/bin/bash
a=5
if [ $a -gt 3 ]
then
echo ok
fi
[root@akuilinux01 shell]# sh -x if1.sh
+ a=5
+ ‘[‘ 5 -gt 3 ‘]‘
+ echo ok
ok
[root@akuilinux01 shell]# cat if2.sh
#!/bin/bash
a=1
if [ $a -gt 3 ]
then
echo ok
else
echo nook
fi
[root@akuilinux01 shell]# sh -x if2.sh
+ a=1
+ ‘[‘ 1 -gt 3 ‘]‘
+ echo nook
nook
[root@akuilinux01 shell]# cat if3.sh
#!/bin/bash
a=1
if [ $a -gt 3 ]
then
echo ok
elif [ $a -lt 3 ]
then
echo "小于3"
else
echo nook
fi
[root@akuilinux01 shell]# sh -x if3.sh
+ a=1
+ ‘[‘ 1 -gt 3 ‘]‘
+ ‘[‘ 1 -lt 3 ‘]‘
+ echo $‘\345\260\217\344\272\2163‘
小于3
[root@akuilinux01 shell]# cat if1.sh
#!/bin/bash
a=5
b=1
if ((a>b))
then
echo ok
fi
[root@akuilinux01 shell]# sh -x if1.sh
[root@akuilinux01 shell]# cat if1.sh
#!/bin/bash
a=5
if [ $a -gt 3 ] && [ $a -lt 10 ]
then
echo ok
fi
[root@akuilinux01 shell]# sh -x if1.sh
+ a=5
+ ‘[‘ 5 -gt 3 ‘]‘
+ ‘[‘ 5 -lt 10 ‘]‘
+ echo ok
ok
[root@akuilinux01 shell]# vim if1.sh
[root@akuilinux01 shell]# cat if1.sh
#!/bin/bash
a=3
if [ $a -gt 3 ] && [ $a -lt 10 ]
then
echo ok
elif [ $a -gt 3 ] || [ $a -lt 10 ]
then
echo "a大于3或者小于10"
else
echo no
fi
[root@akuilinux01 shell]# sh -x if1.sh
+ a=3
+ ‘[‘ 3 -gt 3 ‘]‘
+ ‘[‘ 3 -gt 3 ‘]‘
+ ‘[‘ 3 -lt 10 ‘]‘
+ echo $‘a\345\244\247\344\272\2163\346\210\226\350\200\205\345\260\217\344\272\21610‘
a大于3或者小于10
[root@akuilinux01 shell]# cat file1.sh
#!/bin/bash
f=/tmp/akuilinux
if [ -f $f ]
then
echo ok
else
touch $f
fi
[root@akuilinux01 shell]# sh -x file1.sh
+ f=/tmp/akuilinux
+ ‘[‘ -f /tmp/akuilinux ‘]‘
+ touch /tmp/akuilinux
[root@akuilinux01 shell]# sh -x file1.sh
+ f=/tmp/akuilinux
+ ‘[‘ -f /tmp/akuilinux ‘]‘
+ echo ok
ok
[root@akuilinux01 shell]# cat file1.sh
#!/bin/bash
f=/tmp/akuilinux
if [ -f $f ]
then
rm -f $f
fi
[root@akuilinux01 shell]# cat file2.sh
#!/bin/bash
f=/tmp/akuilinux
[ -f $f ] && rm -f $f
[root@akuilinux01 shell]# cat file1.sh
#!/bin/bash
f=/tmp/akuilinux
if [ ! -f $f ]
then
touch $f
fi
[root@akuilinux01 shell]# cat file2.sh
#!/bin/bash
f=/tmp/akuilinux
[ -f $f ] || touch $f
在判断变量是否为空时,[]里面需要加双引号,-z和-n只能判断变量是否为空,不能判断文件是否为空
[root@akuilinux01 shell]# cat if4.sh
#!/bin/bash
#先判断文件是否存在,不存在退出,以免有bug
if [ ! -f /tmp/lalal ]
then
echo "/tmp/lalal not exist."
exit
fi
n=`wc -l /tmp/lalal`
#如果值是空的退出
if [ -z "$n" ]
then
echo error
exit
#如果有值就打印出来
elif [ -n "$n" ]
then
echo $n
fi
[root@akuilinux01 shell]# cat /tmp/lalal
dadhaj
dahdajhj
127676
adadahj
[root@akuilinux01 shell]# sh if4.sh
4 /tmp/lalal
-w表示匹配的是一个单词
-q是不打印grep匹配的内容
case 变量名 in
value1)
command
;;
value2)
command
;;
*)
command
;;
esac
2|3)
command
;;
#!/bin/bash
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "Please input a number."
exit 1
fi
n1=`echo $n|sed ‘s/[0-9]//g‘`
if [ -n "$n1" ]
then
echo "Please input a number."
exit 1
fi
if [ $n -lt 60 ] && [ $n -ge 0 ]
then
tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
#!/bin/bash
#read和用户交互,把用户输入的值赋给n
read -p "Please input a number: " n
#如果n为空,用户没有输入
if [ -z "$n" ]
then
echo "Please input a number."
exit 1
fi
#判断输入的是否为数字,不是数字时提示并退出
n1=`echo $n|sed ‘s/[0-9]//g‘`
if [ -n "$n1" ]
then
echo "Please input a number."
exit 1
fi
#如果是数字,小于60时,打个标记,如果大于60小于80,打个标记2,以此类推
if [ $n -lt 60 ] && [ $n -ge 0 ]
then
#判断输入的是否为数字,不是数字时提示并退出
n1=`echo $n|sed ‘s/[0-9]//g‘`
if [ -n "$n1" ]
then
echo "Please input a number."
exit 1
fi
#如果是数字,小于60时,打个标记,如果大于60小于80,打个标记2,以此类推
if [ $n -lt 60 ] && [ $n -ge 0 ]
then
tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
tag=2
elif [ $n -ge 80 ] && [ $n -lt 90 ]
then
tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
tag=4
else
tag=0
fi
#使用case判断标记,并给出结果
case $tag in
1)
echo "not ok"
;;
2)
echo "ok"
;;
3)
echo "ook"
;;
4)
echo "oook"
;;
*)
echo "The number range is 0-100."
;;
esac
shell中的逻辑判断,if 判断文件、目录属性,if判断的一些特殊用法
标签:引号 没有 pre 用户 gre echo ber exist linu
原文地址:http://blog.51cto.com/akui2521/2141219