写在前面:案例、常用、归类、解释说明。(By Jim)
使用if-then语句
如果命令的退出状态是0(成功执行命令),将执行then后面的所有命令。
如果命令的退出状态是0以外的其他值,那么then后面的命令将不会执行,bash shell会移动到脚本的下一条命令。
#!/bin/bash # testing the if statement if date then echo "it worked" fi
(date返回0,执行then语句it worked)
#!/bin/bash #testing multiple commands in the then section testuser=jiqing9006 if grep $testuser /etc/passwd then echo The bash files for user $testuser are: ls -a /home/$testuser/.b* fi
(The bash files for user jiqing9006 are:
/home/jiqing9006/.bash_logout /home/jiqing9006/.bash_profile /home/jiqing9006/.bashrc
if语句行使用grep命令搜索/etc/passwd文件,查看系统是否正在使用一个特定的用户名。
如果一个用户拥有该登录名,脚本会显示一些文本,然后列出用户HOME目录下的bash文件
)
if-then-else语句
#!/bin/bash #testing multiple commands in the then section testuser=jim if grep $testuser /etc/passwd then echo The bash files for user $testuser are: ls -a /home/$testuser/.b* else echo "The user name $testuser does't exist on this system" fi
(如果不存在,执行下面的语句)
嵌套if语句
#!/bin/bash #testing multiple commands in the then section user1=jim user2=jiqing9006 if grep $user1 /etc/passwd then echo The bash files for user $user1 are: ls -a /home/$user1/.b* elif grep $user2 /etc/passwd then echo The bash files for user $user2 are: ls -a /home/$user2/.b* else echo "The user name $user1 and $user2 does't exist on this system" fi
(不是elseif 而是elif ,注意与其它语言的区别)
test命令
if [condition]
then
commands
fi
test命令能够评估三类条件
数值
字符串
文件
a.数值比较
n1 -eq n2(是否等于)
n1 -ge n2(是否大于等于)
n1 -gt n2(是否大于)
n1 -le n2(是否小于等于)
n1 -lt n2(是否小于)
n1 -ne n2(不等于)
#!/bin/bash #using numeric test comparisons val1=10 val2=11 if [ $val1 -gt 5 ] then echo "The test value $val1 is greater than 5" fi if [ $val1 -eq $val2 ] then echo "The values are equal" else echo "The values are not equal" fi
(注意if与[之间有空格,[与$val1之间有空格)
test命令无法处理浮点数,bash shell只能处理整数数字
b.字符串比较
str1 = str2 (str1与str2是否相同)
str1 != str2 (是否不同)
str1 < str2(是否小于)
str1 > str2 (是否大于)
-n str1(长度是否大于0)
-z str1(长度是否为0)
#!/bin/bash #testing string equality testuser=root if [ $USER = $testuser ] then echo "Welcome $testuser" fi
(比较相等)
#!/bin/bash #testing string equality testuser=baduser if [ $USER != $testuser ] then echo "This isn't $testuser" else echo "Welcome $testuser" fi
(不相等比较,所有标点符号和大小写都考虑在内)
使用大小于号,需要转义一下
#!/bin/bash #mis-using string comparisons val1=baseball val2=hockey val3=book val4=Book if [ $val1 > $val2 ] then echo "$val1 is greater than $val2" else echo "$val1 is less than $val2" fi if [ $val1 > $val3 ] then echo "$val1 is greater than $val3" else echo "$val1 is less than $val3" fi if [ $val1 > $val2 ] then echo "$val3 is greater than $val4" else echo "$val3 is less than $val4" fi
结果:
baseball is less than hockey
baseball is less than book
book is greater than Book
(test命令采用的是ascii码排序的,sort采用的是当前语言设置定义的排列顺序。由结果可知,比较第一个字母b小于h,如果第一个字符相同,比较第二个字符,a小于o。小写字符,大于大写字符a97,A65。)
ls -lt(按时间排序,最新的在最前面)
ls -l|sort -k1
ls -l|sort -k2
ls -l|sort -k3(按照第几列进行排序)
..
#!/bin/bash #testing string length str1="helloworld" str2="" if [ -n $str1 ] then echo "str1 is not null" else echo "str1 is null" fi if [ -z $str2 ] then echo "str2 is null" else echo "str2 is not null" fi
(我自己写的代码,发现str中不能有空格,否则无法判断)
c.文件比较
test命令能够测试Linux文件系统上的文件状态和路径。
-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新
file2 -ot file2 检查file1是否比file2旧
如果想将文件写到一个目录下,或试图改变到目录位置之前,最好检查一下-d
#!/bin/bash #look before you leap if [ -d $HOME ] then echo "Your HOME directory exists" cd $HOME ls -l else echo "There is a problem with your HOME directory" fi
(检查一个目录是否存在,存在就做一些事情,不存在就提示错误。)
在脚本中使用文件或目录之前,-e比较能够检查它们是否存在
#!/bin/bash # check if a directory exists if [ -e $HOME ] then echo "Your home directory exists" #check if file exists in the directory if [ -e $HOME/testing ] then echo "$HOME/tesing exist in the directory" date >>$HOME/testing else echo "Create a new file" date >$HOME/testing fi else echo "Your home directory doesn't exists" fi
(这里稍微复杂了一点,用了嵌套,并且注释很清晰,提示很到位)
-f检测文件
#!/bin/bash #testing if a file if [ -e $HOME ] then echo "The obj exist,if it is a file?" if [ -f $HOME ] then echo "Yes,it is a file!" else echo "No,it is not a file!" if [ -f $HOME/testing ] then echo "But $HOME/testing is a file!" else echo "$HOME/testing is not a file too!" fi fi else echo "The obj doesn't exist" fi
(这个好无聊,不过展示了一个流程,逐级向下进行查询)
是否能读
在尝试从文件中读取数据之前,通常首先检查一下是否能读取文件。-r
#!/bin/bash #testing if can read a file pwfile=/etc/shadow #first,check if it is a file if [ -f $pwfile ] then #now test if you can read it if [ -r $pwfile ] then tail $pwfile else echo "sorry,the file can be read." fi else echo "sorry,it is not a file." fi
(注意变量使用是一定要加上$符,不然就会出错)
检查空文件
应该用-s比较来检查文件是否为空,尤其是想删除文件时。注意,-s比较成功时,它表明文件包含数据
#!/bin/bash #testing a file is empty file=testfile touch $file if [ -s $file ] then echo "The $file exists and has data in it" else echo "The $file doesn't exist or is empty" fi date >$file if [ -s $file ] then echo "The $file exists and has data in it" else echo "The $file doesn't exist or is empty" fi
(真表示有数据,假表示无数据)
检查是否能够写入数据-w
#!/bin/bash #checking if a file if writeable logfile=$HOME/logtest touch $logfile chmod u-w $logfile now=`date +%Y%m%d-%H%M` if [ -w $logfile ] then echo "The program ran at:$now">>$logfile echo "The first attempt succeeded" else echo "The first attempt failed" fi chmod u+w $logfile if [ -w $logfile ] then echo "The program ran at:$now">>$logfile echo "The second attempt succeeded" else echo "The second attempt failed" fi
(这里的判断有点问题,需要再研究)
..
检查文件日期
#!/bin/bash #testing file dates if [ ./test1 -nt ./test10 ] then echo "The test1 file is newer than test10" else echo "The test10 file is newer than test1" fi if [ ./test1 -ot ./test10 ] then echo "The test1 file is older than test10" else echo "The test10 file is older than test1" fi
(比较两个文件的新旧)
复合条件检查
&&
||
#!/bin/bash #testing compound comparisons if [ -d $HOME ] && [ -x $HOME/testing ] then echo "The file exists and you can execute it" else echo "You can't execute the file" fi
(并列执行)
if-then的高级特征
(())双圆括号,可以进行复杂的算术操作
val++ 后增量
val-- 后减量
++val 前增量
--val 前减量
! 逻辑否定
~ 取反
** 取幂
<< 左移
>> 右移
&
|
&& 逻辑与
|| 逻辑或
#!/bin/bash #using double parenthesis val1=10 if (($val1**2>90)) then ((val2 =$val1**2)) echo "The square of $val1 is $val2" fi
(双圆括号里面的内容更加智能,会识别很多东西,不用总是空格空格的编写代码了)
[[]]双方括号
可以进行字符串比较,更加智能
模式匹配,也就是正则表达式可以更好的使用
#!/bin/bash # using pattern matching if [[ $USER==r* ]] then echo "Hello $USER" else echo "Sorry,I don't know you" fi
(规范编写)
case使用
#!/bin/bash #using the case command case $USER in rich | barbara) echo "Welcome,$USER" ehco "Please enjoy your visit";; testing) echo "Special testing account";; jessica) echo "Don't forget to log off when you're done";; root) echo "Welcome,Manager";; *) echo "Sorry,you're not allowed here";; esac