If-fi
#! /Bin/bash <br/> # delete a file and create a file <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:
(No readme file)
[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 (function parameter 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 (function parameter 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/> # Count the total number of rows in the type file. <br/> type = "*. H *. CPP "<br/> result =" result.txt "<br/> # statistical function <br/> function cal_lines () <br/>{< br/> RM-F $ result # delete a file <br/> for file in 'ls $ type '; DO <br/> WC-L $ file >$ result # create and append the number of statistics rows to the file <br/> done </P> <p> awk '{total + = $1} end {print total} '$ result> $ result # awk accumulates the first column, the sum result is total <br/>}< br/> cal_lines # Call the function <br/> CAT $ result # view the 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
========================================================== ==============
Add two files in Linux (three solutions)
A.txt (10 rows) B .txt (9 rows)
A.txtB .txt
Solution 1
Awk 'nr = FNR {A [Nr] = $1; B [Nr] = $2} <br/> Nr> FNR {print $1 + a [FNR], $2 + B [FNR]} 'a.txt B .txt
Running result:
[Work] $Sh cal_ab1.sh
1 83
1 77
0 128
24 195
1 130
68 227
5 132
197 233
9 146
Bytes --------------------------------------------------------------------------------------
Solution 2
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
Running result:
[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
Bytes --------------------------------------------------------------------------------------
Solution 3
Paste a.txt B .txt | awk '{print $1 + $3 usd/t "$2 + $4 }'
Running result:
[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
Bytes --------------------------------------------------------------------------------------
Evaluation:
Solution 1: The result is inaccurate and only the first nine rows are output.
Solution 2: the results are correct, but not concise.
Solution 3: the results are correct and concise
========================================================== ==============
While Loop
#! /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
Running result:
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
-------------------------------------------------------------------------------
Reference extension:
Linux make (makefile) from simple to deep learning and Example Analysis
Shell if statement example: whether the file or directory exists or has the execution permission
Add two files in Linux
NR and FNR in awk
Baidu interview and Summary 4
Basic shell knowledge
Random example
Use of Shell functions (passing parameters)
========================================================== ==============