one, while the special use of a : always cycle, never quit
While:;d o
...
Done
#!/bin/bash
# showfile.sh
while:
Read-p "Files path:" FILEPATH
[$FILEPATH = = ' quit '] && break
If [-e $FILEPATH];then
echo "$FILEPATH exists."
Else
echo "No $FILEPATH."
Fi
Done
echo "Quit ..."
[Email protected] yum.repos.d]# ./showfile.sh
Files path:/etc
/etc exists.
Files path:/etcc/
no/etcc/.
Files PATH:/YUM/VT
/YUM/VT exists.
Files Path:quit
Quit ...
Second, while the special use of two: process text line by row
while read line; Do
...
Done </path/to/somefile
#!/bin/bash
# showfile.sh
file=/etc/passwd
Let I=1
While Read Line;do
[' Echo $LINE |awk-f: ' {print $NF} ' = = '/bin/bash '] && echo $LINE |awk-f: ' {print $} ' && let i++
[$I-GT 2] && break
Done < $FILE
[Email protected] yum.repos.d]# ./showuser.sh
Root
./showuser.sh:line 6: [: = =: unary operator expected
Oracle
Distinguish between continue and break operation mechanism:
#!/bin/bash
# odd.sh
Let sum=0
Let I=0
Read-p "Keyin I:" I2
Read-p "Keyin SUM:" SUM2
While [$I-lt $I 2];d o
Let i++
If [$[$I%2]-eq 1]; Then
Continue # end this round or this cycle
Fi
If [$SUM-ge $SUM 2]; then
Break # exits this while loop body early
Fi
Let sum+= $I
Done
echo "sum= $SUM; I= $I "
[Email protected] yum.repos.d]# ./odd.sh
Keyin i:100
Keyin sum:2000
sum=2070; i=92
[Email protected] yum.repos.d]#./odd.sh
Keyin i:100
Keyin sum:200
sum=210; I=30
[Email protected] yum.repos.d]#./odd.sh
Keyin i:1000
Keyin sum:20000
sum=20022; i=284
Third, automatically see if there is a problem with the bash script and then go to the script to modify it after displaying the error
#!/bin/bash
# syntax.sh
Until Bash-n $ &>/dev/null; Do
Read-p "Syntax error,[q|q" to Quit,others for editing: "CHOICE
Case $CHOICE in
q| Q)
echo "Something wrong,quiting ..."
Exit 5;;
*)
Bash-n $
Sleep 5
Vim + $;
Esac
Done
[Email protected] yum.repos.d]#/syntax.sh hello.sh
Syntax Error,[q|q] to quit,others for editing:
Hello.sh:line 2:syntax error near unexpected token '; '
Hello.sh:line 2: ' While;d o '
Syntax Error,[q|q] to quit,others for editing:
Hello.sh:line 2:syntax error near unexpected token '; '
Hello.sh:line 2: ' While;d o '
Linux command: Break,continue,while