#!/bin/bash
# testing the if statement
if date
thenecho "it worked"
fi
(date返回0,執行then語句it worked)
#!/bin/bash
#testing multiple commands in the then section
testuser=jiqing9006
if grep $testuser /etc/passwd
thenecho 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
thenecho The bash files for user $testuser are:ls -a /home/$testuser/.b*
elseecho "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
thenecho The bash files for user $user1 are:ls -a /home/$user1/.b*
elif grep $user2 /etc/passwd
thenecho The bash files for user $user2 are:ls -a /home/$user2/.b*
elseecho "The user name $user1 and $user2 does't exist on this system"
fi
#!/bin/bash
#using numeric test comparisons
val1=10
val2=11if [ $val1 -gt 5 ]
thenecho "The test value $val1 is greater than 5"
fiif [ $val1 -eq $val2 ]
thenecho "The values are equal"elseecho "The values are not equal"
fi
#!/bin/bash
#testing string equality
testuser=root
if [ $USER = $testuser ]
thenecho "Welcome $testuser"
fi
(比較相等)
#!/bin/bash
#testing string equality
testuser=baduser
if [ $USER != $testuser ]
thenecho "This isn't $testuser"elseecho "Welcome $testuser"
fi
(不相等比較,所有標點符號和大小寫都考慮在內)
使用大小于號,需要轉義一下
#!/bin/bash
#mis-using string comparisons
val1=baseball
val2=hockey
val3=book
val4=Book
if [ $val1 \> $val2 ]
thenecho "$val1 is greater than $val2"elseecho "$val1 is less than $val2"
fi
if [ $val1 \> $val3 ]
thenecho "$val1 is greater than $val3"elseecho "$val1 is less than $val3"
fi
if [ $val1 \> $val2 ]
thenecho "$val3 is greater than $val4"elseecho "$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 ]
thenecho "str1 is not null"elseecho "str1 is null"
fiif [ -z $str2 ]
thenecho "str2 is null"elseecho "str2 is not null"
fi
#!/bin/bash
#look before you leapif [ -d $HOME ]
thenecho "Your HOME directory exists"cd $HOMEls -l
elseecho "There is a problem with your HOME directory"
fi
#!/bin/bash
# check if a directory existsif [ -e $HOME ]
thenecho "Your home directory exists"#check if file exists in the directoryif [ -e $HOME/testing ]thenecho "$HOME/tesing exist in the directory"date >>$HOME/testingelseecho "Create a new file"date >$HOME/testingfi
elseecho "Your home directory doesn't exists"
fi
(這里稍微復雜了一點,用了嵌套,并且注釋很清晰,提示很到位) -f檢測文件
#!/bin/bash
#testing if a fileif [ -e $HOME ]
thenecho "The obj exist,if it is a file?"if [ -f $HOME ]thenecho "Yes,it is a file!"elseecho "No,it is not a file!"if [ -f $HOME/testing ]thenecho "But $HOME/testing is a file!"elseecho "$HOME/testing is not a file too!"fifi
elseecho "The obj doesn't exist"
fi
#!/bin/bash
#testing if can read a file
pwfile=/etc/shadow
#first,check if it is a fileif [ -f $pwfile ]
then#now test if you can read itif [ -r $pwfile ]thentail $pwfileelseecho "sorry,the file can be read."fi
elseecho "sorry,it is not a file."
fi
#!/bin/bash
#testing a file is empty
file=testfile
touch $fileif [ -s $file ]
then echo "The $file exists and has data in it"elseecho "The $file doesn't exist or is empty"
fidate >$fileif [ -s $file ]
thenecho "The $file exists and has data in it"elseecho "The $file doesn't exist or is empty"
fi
(真表示有數據,假表示無數據)
檢查是否能夠寫入數據-w
#!/bin/bash
#checking if a file if writeable
logfile=$HOME/logtest
touch $logfilechmod u-w $logfile
now=`date +%Y%m%d-%H%M`if [ -w $logfile ]
thenecho "The program ran at:$now">>$logfileecho "The first attempt succeeded"elseecho "The first attempt failed"
fichmod u+w $logfileif [ -w $logfile ]
thenecho "The program ran at:$now">>$logfileecho "The second attempt succeeded"elseecho "The second attempt failed"
fi
(這里的判斷有點問題,需要再研究)
.. 檢查文件日期
#!/bin/bash
#testing file datesif [ ./test1 -nt ./test10 ]
thenecho "The test1 file is newer than test10"elseecho "The test10 file is newer than test1"
fiif [ ./test1 -ot ./test10 ]
thenecho "The test1 file is older than test10"elseecho "The test10 file is older than test1"
fi
(比較兩個文件的新舊)
復合條件檢查 && ||
#!/bin/bash
#testing compound comparisonsif [ -d $HOME ] && [ -x$HOME/testing ]
thenecho "The file exists and you can execute it"elseecho "You can't execute the file"
fi
#!/bin/bash
#using double parenthesis
val1=10if (($val1**2>90))
then((val2 =$val1**2))echo "The square of $val1 is $val2"
fi
(雙圓括號里面的內容更加智能,會識別很多東西,不用總是空格空格的編寫代碼了)
[[]]雙方括號 可以進行字符串比較,更加智能 模式匹配,也就是正則表達式可以更好的使用
#!/bin/bash
# using pattern matchingif [[ $USER==r* ]]
thenecho "Hello $USER"elseecho "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