(創(chuàng)建于2018/1/31)
條件語句
shell中的條件語句必須以fi結(jié)尾,否則會(huì)報(bào)錯(cuò)syntax error: unexpected end of file
if else then 這里的test命令意思就是test后的條件如果成立,則它就是0(真),否則就是非零(假)
#!/bin/bash
2
3 var=""
4 if test var
5 then
6 echo "success"
7 else
8 echo "failed"
9 fi
if then elif then fi(相當(dāng)于if else if語句)
1 #!/bin/bash
2
3 if test $var
4 then echo "success"
5 elif test haha
6 then echo "haha"
7 else echo "failed"
8 fi
條件語句進(jìn)行數(shù)字大小比較
-gt表示數(shù)學(xué)中的大于號(hào)>
其他符號(hào)的對(duì)應(yīng)關(guān)系為
-eq:等于
-lt : 小于
-ne :不等于
-le :小于等于
1 #!/bin/bash
2
3 a=10
4 b=5
5
6 if [ $a -gt $b ] //注意,[]中間一定有空格,否則會(huì)報(bào)錯(cuò) command not found
7 then
8 echo "$a is greater than $b"
9 else
10 echo "$a is smaller than $b"
11 fi
字符串的比較
str1 == str2
str1 != str2
str1 < str2
-n str1 長度是否非零
-z str2 長度是否為0
1 #!/bin/bash
2
3 str1="ren"
4 str2="ming"
5 str3=""
6 str4="ren"
7
8 if [ $str1 == $str4 ]
9 then
10 echo " str1 == str2"
11 else
12 echo " str1 != str2"
13 fi
檢查文件或者目錄
-d : 檢查目錄是否存在
-f : 檢查文件是否存在
-e : 檢查文件或者目錄是否存在
-r : 檢查是否存在并且可讀
-w : 檢查是否存在并且可寫
-x : 檢查是否存在并且可執(zhí)行
file1 -nt file2 file1比file2新(new than)
file1 -ot file2 file1比file2舊 (old than)
查看一個(gè)目錄是否存在,如果存在則遍歷這個(gè)目錄
1 #!/bin/bash
2
3 dir=/usr/ndk/temp
4
5 if [ -d $dir ]
6 then
7 echo "$dir exist"
8 cd $dir
9 ls
10 else
11 echo "$dir not exist"
12 fi
多個(gè)條件組合
如果目錄存在和文件ren.sh可執(zhí)行滿足靠抑,則打印,遍歷dir并創(chuàng)建一個(gè)文件b.sh并授權(quán)可執(zhí)行然后再次遍歷
1 #!/bin/bash
2
3 dir=/usr/ndk/temp
4
5 dir2=/usr/ndk/temp/ren.sh
6
7 if [ -d $dir ] && [ -x $dir2 ]
8 then
9 echo "find $dir and $dir2,$dir2 can be execute"
10 ls $dir
11 touch b.sh
12 chmod u+x b.sh
13 ls -la
14 else
15 echo "not exist"
16 fi
tabstop=8root@iZuf67sz57humoriy3o6oiZ:/usr/ndk/temp# ./a.sh
find /usr/ndk/temp and /usr/ndk/temp/ren.sh,/usr/ndk/temp/ren.sh can be execute
a.sh ren.sh
total 16
d--------- 2 root root 4096 Sep 13 07:57 .
drwxrwxrwx 7 root root 4096 Sep 7 07:47 ..
-rwxr--r-- 1 root root 215 Sep 13 07:57 a.sh
-rwxr--r-- 1 root root 0 Sep 13 07:57 b.sh
-rwxr--r-- 1 root root 115 Sep 13 07:34 ren.sh
shell中的case命令(類似switch)
格式如下:
case 變量 in
pattern1) 命令;;
pattern2) 命令;;
*) 默認(rèn)命令;;
esac
1 #!/bin/bash
2
3 user=zhen
4
5 case $user in
6
7 zhen)
8 echo "zhen is here";;
9 ming)
10 echo "ming is here";;
11 *)
12 echo "not found";;
13 esac
shell中的for命令
1 #!/bin/bash
2
3 list="Monday,Friday,Sonday"
4 IFS=$, //域分隔符
5 for item in $list
6 do
7 echo $item
8 done
9
打印結(jié)果去掉了括號(hào)
tabstop=8root@iZuf67sz57humoriy3o6oiZ:/usr/ndk/temp# ./c.sh
Monday
Friday
Sonday
shell中的while命令
1 #!/bin/bash
2
3 a=10
4 while [ $a -gt 0 ]
5
6 do
7 echo "num:$a"
8 a=$[ $a - 1 ]
9 done
tabstop=8root@iZuf67sz57humoriy3o6oiZ:/usr/ndk/temp# ./d.sh
num:10
num:9
num:8
num:7
num:6
num:5
num:4
num:3
num:2
num:1
root@iZuf67sz57humoriy3o6oiZ:/usr/ndk/temp#
循環(huán)語句和條件語句嵌套
1 #!/bin/bash
2
3 a=10
4 while [ $a -gt 0 ]
5
6 do
7 echo "num:$a"
8 a=$[ $a - 1 ]
9 if [ $a -eq 5 ]
10 then
11 echo "break"
12 break
13 fi
14 done
tabstop=8root@iZuf67sz57humoriy3o6oiZ:/usr/ndk/temp# ./d.sh
num:10
num:9
num:8
num:7
num:6
break
root@iZuf67sz57humoriy3o6oiZ:/usr/ndk/temp#
重定向深入理解undefined
一般情況下适掰,每一個(gè)Linux 命令運(yùn)行時(shí)都會(huì)打開三個(gè)文件:
標(biāo)準(zhǔn)輸入文件(stdin): stdin的文件描述符為0颂碧,linux程序默認(rèn)從stdin讀取數(shù)據(jù)。
標(biāo)準(zhǔn)輸出文件(stdout): stdout 的文件描述為1类浪,linux程序默認(rèn)向stdout輸出數(shù)據(jù)载城。
標(biāo)準(zhǔn)錯(cuò)誤文件(stderr): stderr 的文件描述符為2,linux程序會(huì)向stderr流中寫入錯(cuò)誤信息戚宦。
以后打開文件后个曙。新增文件綁定描述符 可以依次增加。 一條shell命令執(zhí)行受楼,都會(huì)繼承父進(jìn)程的文件描述符垦搬。因此,所有運(yùn)行的shell命令艳汽,都會(huì)有默認(rèn)3個(gè)文件描述符猴贰。
正常情況下,command > file 將stdout重定向file河狐,command < file 將stdin 重定向到 file米绕。
一個(gè)命令執(zhí)行了:
先有一個(gè)輸入:輸入可以從鍵盤,也可以從文件得到
命令執(zhí)行完成:成功了馋艺,會(huì)把成功結(jié)果輸出到屏幕:standard output默認(rèn)是屏幕
命令執(zhí)行有錯(cuò)誤:會(huì)把錯(cuò)誤也輸出到屏幕上面:standard error默認(rèn)也是指的屏幕
如果希望stderr 重定向 到file 可以這樣寫:
command 2> file
1
如果希望stderr追加到file 文件末尾栅干,可以這樣寫:
command 2>> file
1.輸出內(nèi)容到文件中,(如果文件不存在則自動(dòng)創(chuàng)建)
#!/bin/bash
file=test111
echo "input into test111" > $file
echo "input into test111" >> $file (追加到文件末尾)
打開文件test111會(huì)發(fā)現(xiàn)結(jié)果捐祠,輸入了兩行
input into test111
input into test111
2.輸出到屏幕上(控制臺(tái))
#!/bin/bash
file=test111
//0 STDIN
//1 STDOUT 標(biāo)準(zhǔn)輸出
//2 STDERR
echo "input into test111" >&2 //注意碱鳞,>與&2之間沒有空格
echo "input into test111" >&2 //>>無法輸出到屏幕
3.在命令行中控制標(biāo)準(zhǔn)輸出重定向到文件中
command > file 將輸出重定向到 file。
command < file 將輸入重定向到 file踱蛀。
command >> file 將輸出以追加的方式重定向到 file窿给。
n > file 將文件描述符為 n 的文件重定向到 file。
n >> file 將文件描述符為 n 的文件以追加的方式重定向到 file率拒。
n >& m 將輸出文件 m 和 n 合并崩泡。
n <& m 將輸入文件 m 和 n 合并。
<< tag 將開始標(biāo)記 tag 和結(jié)束標(biāo)記 tag 之間的內(nèi)容作為輸入猬膨。
需要注意的是文件描述符 0 通常是標(biāo)準(zhǔn)輸入(STDIN)角撞,1 是標(biāo)準(zhǔn)輸出(STDOUT),2 是標(biāo)準(zhǔn)錯(cuò)誤輸出(STDERR)勃痴。
command1 < infile > outfile //執(zhí)行command1谒所,從文件infile讀取內(nèi)容,然后將輸出寫入到outfile中
#!/bin/bash
file=test111
echo "input into test111"
echo "input into test111"
//./14.sh &> test222 也可以輸出到文件中召耘,搞不懂
執(zhí)行腳本
tabstop=8root@iZbp11v3y27wpf6mglp2glZ:/user/renzhenming/shell# ./14.sh 1> test222 //(將文件描述符為 n 的文件重定向到 file百炬。)會(huì)將標(biāo)準(zhǔn)輸出符為1的文件14.sh中的標(biāo)準(zhǔn)輸出內(nèi)容重定向到test222文件中,注意任何test222內(nèi)的已經(jīng)存在的內(nèi)容將被新內(nèi)容替代污它。如果要將新的內(nèi)容添加到文件的末尾剖踊,則使用>>操作符。
root@iZbp11v3y27wpf6mglp2glZ:/user/renzhenming/shell# ls
10.sh 12.sh 14.sh 3.sh 5.sh 7.sh 9.sh ren.txt test222
11.sh 13.sh 2.sh 4.sh 6.sh 8.sh copy test111
root@iZbp11v3y27wpf6mglp2glZ:/user/renzhenming/shell# cat test222
input into test111
input into test111
4.在腳本中設(shè)置標(biāo)準(zhǔn)輸出重定向?qū)懭胛募猩辣幔琫xec 1>文件名
#!/bin/bash
exec 1>test333
echo "input into test333"
echo "input into test333"
輸出結(jié)果:
./14.sh
root@iZbp11v3y27wpf6mglp2glZ:/user/renzhenming/shell# ls
10.sh 12.sh 14.sh 3.sh 5.sh 7.sh 9.sh ren.txt test222
11.sh 13.sh 2.sh 4.sh 6.sh 8.sh copy test111 test333
root@iZbp11v3y27wpf6mglp2glZ:/user/renzhenming/shell# cat test333
1 #!/bin/bash
2
//將標(biāo)準(zhǔn)輸出重定向到output.txt文件中德澈,這樣一來這個(gè)腳本中的所有輸出內(nèi)容會(huì)
//被打印到output.txt文件
3 exec 1>output.txt
//將標(biāo)準(zhǔn)錯(cuò)誤輸出重定向到error.txt文件,所以這個(gè)文件中發(fā)生的錯(cuò)誤信息會(huì)被打印
//到這里
4 exec 2>error.txt
5
6 echo "this is output"
7 ls -la ffmpeg
5.自定義輸出
#!/bin/bash
//#0 STDIN 標(biāo)準(zhǔn)輸入
//#1 STDOUT 標(biāo)準(zhǔn)輸出
//#STDERR 標(biāo)準(zhǔn)錯(cuò)誤
exec 1>file1 //將腳本執(zhí)行過程中的標(biāo)準(zhǔn)輸出寫入到文件file1
exec 2>file2 //將腳本執(zhí)行中的標(biāo)準(zhǔn)錯(cuò)誤輸出到文件file2
exec 3>file3 //自定義輸出將標(biāo)準(zhǔn)輸出輸入到文件file3
echo "hello" >&3
echo "byebye"
ls -a ./hehe //沒有這個(gè)文件固惯,發(fā)生錯(cuò)誤梆造,會(huì)將錯(cuò)誤信息打印到file2中
結(jié)果:
tabstop=8root@iZbp11v3y27wpf6mglp2glZ:/user/renzhenming/shell# ./15.sh
root@iZbp11v3y27wpf6mglp2glZ:/user/renzhenming/shell# ls
10.sh 12.sh 14.sh 2.sh 4.sh 6.sh 8.sh copy ren.txt STDOUT
11.sh 13.sh 15.sh 3.sh 5.sh 7.sh 9.sh CUSTOME_STD STDERR
root@iZbp11v3y27wpf6mglp2glZ:/user/renzhenming/shell# cat STDOUT
byebye
root@iZbp11v3y27wpf6mglp2glZ:/user/renzhenming/shell# cat STDERR
ls: cannot access './hehe': No such file or directory
root@iZbp11v3y27wpf6mglp2glZ:/user/renzhenming/shell# cat CUSTOME_STD
hello