One, if statement, while, for loop a variety of small examples
1. User authentication
[[email protected] ~]# cat buer.sh #!/bin/bash#user= ' buer ' #password = ' 1234.com ' read-p ' please your name: ' Userread-p ' pas Sword: ' passwdif [$user = ' Buer '-a $passwd = ' 1234.com '];then echo ' login successful ' fi
2. Guess the Age
#!/bin/bashage=57read-p ' num: ' numif [$num-eq $age];then echo ' Get it ' elif [$num-gt $age];then Echo ' to o Big ' elif [$num-lt $age];then echo ' too samll ' fi
3. Search results
[email protected] ~]# cat score.sh #!/bin/bashread-p ' Please your score: ' Scoreif [$score-ge];then Echo ' very good! ' elif [$score-ge 70-a $score-lt];then echo ' good! ' elif [$score-ge 60-a $score-lt];then echo ' Just like ' else echo ' poor ' fi
4.for Cycle Printing 1235
[[email protected] ~]# cat for.sh #!/bin/bashfor i in {1..5}do if ((i = = 4)] Then continue #break Fi
echo ' ======= ' $idone
Execution results for script 4:
[Email protected] ~]#/for.sh ======= 1======= 2======= 3======= 5
5. Improved version of guessing age
[email protected] ~]# cat oldboy.sh oldboy=57while:d o read-p ' input your Age: ' Age if [-Z $age];then cont Inue fi if [$age-eq $oldboy];then echo "Bingo" break elif [$age-lt $oldboy];then echo "Littl E " elif [$age-gt $oldboy];then echo" Big " else echo" Bye " Fidone
Script 5 results:
6. View File types
[email protected] ~]# cat aa.sh #!/bin/bashwhile:d o read-p ' input your file: ' File if [-Z $file];then con Tinue elif [$file = ' quit '];then break fi If [-B $file];then echo "$file is block file" elif [ -D $file];then echo "$file is directory file" Elif [-F $file];then echo "$file is regular file" Els E echo "$file unkown" fidone
Script 6 Results:
[Email protected] ~]# sh aa.sh input your file:/etc/ etc is directory fileinput your file:/pm/pm unkowninput your fi Le:quit
7. Printing multiplication Tables
[[email protected] ~]# cat cheng.sh #!/bin/bashfor ((i=1;i<=9;i++)) do for ((j=1;j<=i;j++)) do Echo-n "$i * $j =$[$i * $j]" done Echodone
Script 7 results:
[Email protected] ~]#/cheng.sh 1*1=12*1=22*2=43*1=33*2=63*3=94*1=44*2=84*3=124*4=165*1=55*2=105*3=155*4=205*5= 256*1=66*2=126*3=186*4=246*5=306*6=367*1=77*2=147*3=217*4=287*5=357*6=427*7=498*1=88*2=168*3=248*4=328*5=408*6 =488*7=568*8=649*1=99*2=189*3=279*4=369*5=459*6=549*7=639*8=729*9=81
8. Create users in batches
[[email protected] ~]# cat add.sh #!/bin/bashfor i in {1..5};d o useradd user$i && echo "user$i create SUCCESSF UL "Done
Script 8 Execution Results:
[[Email protected] ~]# sh add.sh user1 create successfuluser2 Create Successfuluser3 create successfuluser4 Create success FULUSER5 Create successful
9. Delete users in bulk
[[email protected] ~]# cat del.sh #!/bin/bashfor i in {1..5};d o userdel-r user$i && echo "user$i Delete Succe Ssful "Done
Script 9 Execution Results:
[[Email protected] ~]# sh del.sh user1 delete successfuluser2 Delete successfuluser3 Delete successfuluser4 Delete success FULUSER5 Delete Successful
10. Loop overlay to print the numbers and sleep 1s
[[email protected] ~]# cat conut.sh #!/bin/bashcount=1while (Count <) do echo ' ==========> ' $count Sleep 1 ((count+=1)) done
Execution Result:
[[Email protected] ~]# sh conut.sh ==========> 1==========> 2==========> 3==========> 4==========> 5===== =====> 6==========> 7==========> 8==========> 9
11. Batch Ping IP
[email protected] ~]# cat ping.sh #!/bin/bashfor i in {1..100};d o ping-c1 192.168.16. $i &>/dev/null If [ $? -eq 0];then echo "192.168.16. $i OK" echo "192.168.16. $i" >>/tmp/ip.txt Fidone
12. Impersonate a login user gadget
[email protected] ~]# cat login.sh #!/bin/bashuser= ' buer ' passwd= ' 1234.com ' while $tagdo read-p ' input your name: ' NA Me read-p ' input your passwd: ' pas if [[$name = $user]] && [[$pas = $passwd]];then echo ' login su Ccessful ' while $tag do read-p ' >>: ' cmd if [[$cmd = ' quit ']];then tag=false Else $cmd fi done fidone
Execution Result:
[Email protected] ~]# sh login.sh input your name:buerinput your passwd:1234.comlogin successful>>:lsaa.sh A.txt del.sh oldboy.sh template download add.sh buer.sh for.sh ping.sh video Music age.sh cheng.sh initial-setup-ks.cfg score.sh picture desktop anaconda-ks.cfg conut.sh login.sh Public
Shell's Process Control