Linux command: Break,continue,while

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.