Linux Basics (vi)
Three loops and function knowledge points in shell scripts
First, the process control if structure
1. Simple If instance:
#!/bin/bashVar='/ETC/INIT.D'#var= '/DEV/SDA 'if[ -d $var] then echo"$var is directory"elif[ -b $var] then echo"$var is block"elif[ -F $var] then echo"$var is regular file"ElseEcho'Unknow'fi
View Code
Execution effect:
2. Passing parameters to a script
eg
#3.shEcho $0echo $1echo $2echo $3Echo ${11}echo'$$'$ $echo'$*'$*Echo'[email protected]'[Email Protected]echo'$#'$#Echo'$?'$?
View Code
Test results:
[[Email protected] ~] # ./3.sh 1 2 3 4 5 6 7 8 9./3. SH1238755$* 1 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 9$# ten$? 0
View Code
Two. While of the cyclic structure
while (condition)
Do
Action
Done
When an infinite loop is required we choose the while:
Write a script to let the user input, enter the number through, output error re-enter
#!/bin/bashlogin=0 while[$login! = 1]do Read-P'Please input your name:'name Read-P'Please input your password:'pwdif[$name = ='Egon'] && [$PWD = ='123'] then echo'Login sucessful'Login=1Fidone
View Code
Run and view results:
[[Email protected] ~] # ./4.sh 123login Sucessful[[email protected] Gandong]#
View Code
Third, the circular structure for
Shell format for
for inch {1..10}doecho $idone
View Code
[[Email protected] ~] # 12345678910
View Code
An example of applying a loop and conditional judgment write:
Vim 6. SH while:d o Read-P'Please input your name:'name Read-P'Please input your password:'PSDif[-Z $name] | | [-z $PSD] ThenContinuefiif[$name ='Alex'-A $psd ='alex3714'] then echo'login successful, welcome da SB' while: Do read-P'Please input your cmd:'cmdif[$cmd ='quit'] Then Breakfi $cmd doneElseEcho'Username or password is error'Fidone Echo'========================================================'~
View Code
Four, the function knowledge point
Functions in the interactive shell
eg
Vim 7'aaa'bbbb';} ABC
View Code
Operation Result:
[[Email protected] ~] # Vim 7.sh [[Email protected] ~] # ./7.sh aaabbbb
View Code
Linux Basics (6)