if-fi
#! /bin/bash<br /># 刪除檔案 和 建立檔案<br />file=readme<br />function delFile(){<br /> if [ -e ./$file ];then<br /> rm -f ./$file<br /> echo "del $file ..."<br /> fi<br />}<br />function addFile(){<br /> if [ ! -f ./$file ];then<br /> touch $file<br /> echo "add $file ..."<br /> fi<br />}<br />delFile<br />addFile<br />
Result:
(沒有readme檔案)
[work shell]$ sh if_e.sh
add readme ...
[workshell]$ sh if_e.sh
del readme ...
add readme ...
===================================================
if-else-fi
#! /bin/bash<br />echo "press y hello"<br />read str<br />if [ "$str" = "y" ] || [ "$str" = "Y" ];then<br /> echo "hello"<br />else<br /> echo "bye.."<br />fi;
Result:
[work shell]$ sh if.sh
press y hello
y
hello
[work shell]$ sh if.sh
press y hello
n
bye..
===================================================
if-elif-else-if(函數傳參1)
# !/bin/sh</p><p>type=1 # 1, 2, 3(a,abc,123)</p><p>function getVal(){<br /> echo "$1"<br />}</p><p>if [ "$type" == "1" ]; then<br /> for((i=0;i<4;i++))<br /> do<br /> eval getVal $i<br /> done<br />elif [ "$type" == "2" ]; then<br /> echo "type"<br />else<br /> echo "none"<br />fi<br />Result:
yanggang@barry$ ./param.sh
0
1
2
3
===================================================
if-elif-else-if(函數傳參2)
# !/bin/sh</p><p>#type=1 # 1, 2, 3(a,abc,123)</p><p>function getVal(){<br /> echo "$1"<br />}</p><p>function inputVal(){<br /> if [ "$1" == "1" ]; then<br /> for((i=0;i<4;i++))<br /> do<br /> eval getVal $i<br /> done<br /> elif [ "$1" == "2" ]; then<br /> echo "type"<br /> else<br /> echo "none"<br /> fi<br />}</p><p>inputVal 1 # 1 is a param
Result:
yanggang@barry$ ./param.sh
0
1
2
3
===================================================
awk
#! /bin/bash<br /># 統計type類型檔案,總共含有多少行<br />type="*.h *.cpp"<br />result="result.txt"<br /># 統計函數<br />function cal_lines()<br />{<br /> rm -f $result #刪除檔案<br /> for file in `ls $type`; do<br /> wc -l $file >> $result #建立並追加統計行數到檔案<br /> done </p><p> awk '{total+=$1} END {print total}' $result >> $result #awk累加第一列,相加結果為total<br />}<br />cal_lines #調用函數<br />cat $result #查看結果<br />
Result:
[work]$ sh cal_lines.sh
ls: *.h: No such file or directory
91 test_performance_server.cpp
178 test_performance_ubclient1.cpp
230 test_performance_ubclient2_common_async.cpp
204 test_performance_ubclient2_common_block.cpp
206 test_performance_ubclient2_common_nonblock.cpp
191 test_performance_ubclient2_common_single_block.cpp
193 test_performance_ubclient2_common_single_nonblock.cpp
237 test_performance_ubclient2_nshead_async.cpp
220 test_performance_ubclient2_nshead_block.cpp
218 test_performance_ubclient2_nshead_nonblock.cpp
192 test_performance_ubclient2_nshead_single_block.cpp
192 test_performance_ubclient2_nshead_single_nonblock.cpp
2352
===================================================
linux實現兩個檔案內容相加(3種解法)
a.txt(10行) b.txt(9行)
a.txt b.txt
解法一
awk 'NR==FNR{a[NR]=$1; b[NR]=$2}<br />NR > FNR{print $1 + a[FNR], $2+b[FNR]}' a.txt b.txt
運行結果:
[work]$ sh cal_ab1.sh
1 83
1 77
0 128
24 195
1 130
68 227
5 132
197 233
9 146
--------------------------------------------------------------------------------------
解法二
paste a.txt b.txt > c.txt<br />while read a b c d; do<br /> echo $((a+c)) $((b+d))<br />done < c.txt
運行結果:
[work]$ sh cal_ab2.sh
1 83
1 77
0 128
24 195
1 130
68 227
5 132
197 233
9 146
0 8
--------------------------------------------------------------------------------------
解法三
paste a.txt b.txt | awk '{print $1+$3 "/t" $2+$4}'
運行結果:
[work]$ sh cal_ab3.sh
1 83
1 77
0 128
24 195
1 130
68 227
5 132
197 233
9 146
0 8
--------------------------------------------------------------------------------------
評析:
解法一,結果不準確,只輸出了前9行
解法二,結果正確,但不夠簡潔
解法三,結果正確,簡潔
===================================================
while迴圈
# !/bin/sh</p><p>TOP_NUM=800<br />index=0</p><p>function url_down(){</p><p>while [ $index -le $TOP_NUM ]<br /> do<br /> echo $index<br /> index=`expr $index + 24`<br />done</p><p>}</p><p>url_down
運行結果:
yanggang@barry$ ./tmp.sh
0
24
48
72
96
120
144
168
192
216
240
264
288
312
336
360
384
408
432
456
480
504
528
552
576
600
624
648
672
696
720
744
768
792
-------------------------------------------------------------------------------
參考拓展:
Linux Make(Makefile)由淺入深的學習與樣本剖析
shell if語句 樣本:檔案或目錄是否存在或有執行許可權
linux實現兩個檔案內容相加
awk中NR與FNR
百度面試及總結4
shell基礎知識
關於RANDOM 的例子
shell函數(傳參)的使用
===================================================