标签:shell test ;; utils 等于 字符 rect 其他 data
root@lejian:/data# cat demo1 #!/bin/bash if date then echo "hello world" fi root@lejian:/data# ./demo1 Fri Dec 2 05:26:27 CST 2016 hello world
root@lejian:/data# cat demo2 #!/bin/bash if aaa then echo "hello world" fi root@lejian:/data# ./demo2 ./demo2: line 2: aaa: command not found
root@lejian:/data# cat demo3 #!/bin/bash if aaa then echo "hello world" else echo "hello java" fi root@lejian:/data# ./demo3 ./demo3: line 2: aaa: command not found hello java
root@lejian:/data# cat demo4 #!/bin/bash if aaa then echo "hello world" elif who then echo "hello java" else echo "hello linux" fi root@lejian:/data# ./demo4 ./demo4: line 2: aaa: command not found root pts/0 2016-12-02 04:39 (122.91.222.126) hello java
比较 | 描述 |
n1 -eq n2 | 检查n1是否与n2相等 |
n1 -ge n2 | 检查n1是否大于等于n2 |
n1 -gt n2 | 检查n1是否大于n2 |
n1 -le n2 | 检查n1是否小于等于n2 |
n1 -lt n2 | 检查n1是否小于n2 |
n1 -ne n2 | 检查n1是否不等于n2 |
root@lejian:/data# cat demo3 #!/bin/bash val1=10 val2=20 if [ $val1 -gt $val2 ] then echo "The test value $val1 is greater than $val2 " elif [ $val1 -lt $val2 ] then echo "The test value $val1 is less than $val2" else echo "The test value $val1 is equal $val2 " fi root@lejian:/data# ./demo3 The test value 10 is less than 20
比较 | 描述 |
str1 = str2 | 检查str1是否和str2相同 |
str1 != str2 | 检查str1是否和str2不同 |
str1 > str2 | str1是否比str2大 |
str1 < str2 | str1是否比str2小 |
-n str1 | 检查str1的长度是否非0 |
-z str2 | 检查str1的长度是否为0 |
root@lejian:/data# cat demo4 #!/bin/bash testUser=$USER badUser=Tom if [ $USER = $testUser ] then echo "hello $USER" fi if [ $USER = $badUser ] then echo "hello $USER" else echo "User is $USER" fi root@lejian:/data# ./demo4 hello root User is root
root@lejian:/data# cat demo5 #!/bin/bash val1="hello" val2="Hello" if [ $val1 \> $val2 ] then echo "The result is $val1 greater than $val2" elif [ $val1 \< $val2 ] then echo "The result is $val1 less than $val2" fi root@lejian:/data# ./demo5 The result is hello greater than Hello root@lejian:/data# cat demo6 #!/bin/bash val1="hello" val2="world" if [ $val1 \> $val2 ] then echo "The result is $val1 greater than $val2" elif [ $val1 \< $val2 ] then echo "The result is $val1 less than $val2" fi root@lejian:/data# ./demo6 The result is hello less than world
root@lejian:/data# cat demo7 Hello hello root@lejian:/data# sort demo7 hello Hello
root@lejian:/data# cat demo8 #!/bin/bash val1="testing" val2="" if [ -n $val1 ] then echo "The string ‘$val1‘ is not empty" else echo "The string ‘$val1‘ is empty " fi if [ -n $val2 ] then echo "The string ‘$val2‘ is not empty" else echo "The string ‘$val2‘ is empty " fi if [ -n $val3 ] then echo "The string ‘$val3‘ is not empty" else echo "The string ‘$val3‘ is empty " fi root@lejian:/data# ./demo8 The string ‘testing‘ is not empty The string ‘‘ is not empty The string ‘‘ is not empty
代码2-8中:
比较 | 描述 |
-d file | 检查file是否存在并是个目录 |
-e file | 检查file是否存在 |
-f file | 检查file是否存在并是个文件 |
-r file | 检查file是否存在并可读 |
-s file | 检查file是否存在并非空 |
-w file | 检查file是否存在并可写 |
-x file | 检查file是否存在并可执行 |
-O file | 检查file是否存在并属当前用户所拥有 |
-G file | 检查file是否存在并且默认组与当前用户相同 |
file1 -nt file2 | 检查file1是否比file2新 |
file1 -ot file2 | 检查file1是否比file2旧 |
root@lejian:/data# cat demo9 #/bin/bash if [ -d $HOME ] then echo "Your home directory exists" cd $HOME ls -a else echo "There is a problem with your HOME directory" fi root@lejian:/data# ./demo9 Your home directory exists . .. .bash_history .bashrc .cache .hivehistory .pip .profile .pydistutils.cfg .rediscli_history .scala_history .selected_editor .spark_history .ssh .vim .viminfo
root@lejian:/data# cat demo1 #!/bin/bash file="text" touch $file if [ -s $file ] then echo "The $file file exists and has data in it" else echo "The $file exists and is empty" fi date > $file if [ -s $file ] then echo "The $file file exists and has data in it" else echo "The $file exists and is empty" fi root@lejian:/data# ./demo1 The text exists and is empty The text file exists and has data in it
root@lejian:/data# cat demo2 #!/bin/bash if [ -d $HOME ] && [ -w $HOME/testing ] then echo "The file exists and you can write it" else echo "I cannot write to the file" fi root@lejian:/data# ./demo2 I cannot write to the file
符号 | 描述 |
val++ | 后增 |
val-- | 后减 |
++val | 先增 |
--val | 先减 |
! | 逻辑求反 |
- | 位求反 |
** | 幂运算 |
<< | 位左移 |
>> | 位右移 |
& | 位布尔和 |
| | 位布尔或 |
&& | 逻辑和 |
|| | 逻辑或 |
root@lejian:/data# cat demo3 #!/bin/bash val1=10 if(($val1 ** 2 > 90)) then ((val2=$val1 ** 2)) echo "The square of $val1 is $val2" fi root@lejian:/data# ./demo3 The square of 10 is 100
root@lejian:/data# cat demo4 #!/bin/bash if [[ $USER=r* ]] then echo "hello $USER" else echo "Sorry. I do not know you" fi root@lejian:/data# ./demo4 hello root
root@lejian:/data# cat demo5 #!/bin/bash case $USER in root|Tom) echo "Welcome $USER" echo "Please enjoy your visit";; testing) echo "Special testing account";; jessica) echo "Do not forget to log off when you‘re done";; *) echo "Sorry. you are not allowed here";; esac root@lejian:/data# ./demo5 Welcome root Please enjoy your visit
标签:shell test ;; utils 等于 字符 rect 其他 data
原文地址:http://www.cnblogs.com/baoliyan/p/6127901.html