Writing a For loop and a while loop case with the shell

Source: Internet
Author: User
Tags loop case

(1) difference between single and double quotes in the shell

?? A= "Www.jfedu.net"

?? b= ' $A '

? Echo $B

Comments:?

Assign the www.jfedu.net to variable A, assign the contents of variable A to the variable B, print out the result of the variable B, and print the result as $ A

?????? Because the variable A is a weak reference, the result of the reference is just one character


A= "Www.jfedu.net"

b= "$A"

Echo $B

Comments:

? ? ? ? ? ?? Assign www.jfedu.net to variable A, assign the contents of variable A to the variable B, print out the result of the variable B, and print the result as www.jfedu.net

? ? ? ? ? ? Because variable A is a strong reference, the reference result of variable B is www.jfedu.net, not a character


(2) for loop syntax format

? ? For? Var? In (an expression)

? ? Do

???????????? Statement

? ? Done


(3) Print 1 to 100 with for loop

? ? For I??? ' SEQ 1 100 '

? ? Do

? ? ? ? Echo $i

? ? Done


Note: Single and double quotation marks only apply to variable references, calculations are not possible, single and double quotes are not used

??

Expand

1 to 100 other two ways to print with a for loop

? For? I?? in? $ (SEQ 1 100)

? Do

??? Echo $i

? Done


? For I? In? ' Echo? {1..100} '

? Do

??? Echo $i

? Done

Note: Do not forget to add a space after echo, do not add space, will error


(4) Read the file contents with a For loop and print to the screen

? For I? In? ' Cat List.txt '

? do

??? Echo $i

Done


(5) Batch to remote host with for loop execution command

? Command= "$*"

? For? I???? ' Seq 1 100 '

? Do

???? ssh-l? Root? 192.168.1. $i? "$command"

? Done


(6) Bulk transfer of files to a remote host with a For loop

? files= "$*"

? For? I?? in? ' SEQ 1 100 '

? do

? scp-r $files [email protected] $i:/tmp

Done


(7) Output 1 to 100 with a For loop and

? j=0

For I? In? ' SEQ 1 100 '

Do

?? j= ' expr $i + $j '

Done

Echo $j


(8) While loop format

? While? (expression)

? Do

? ? ? Statement

? done


(9) Use while loop to read the contents of the file list and print the results on the screen

? while read line

? do

? ? ? Echo $line

? done<list.txt


(10) using the while loop to output hello on the screen every second? Word

? While? Sleep 1

? Do

? ? ? echo "Hello word"

? Done


(11) Use while loop to print 1 to 100

? I=0

? while (i<=100)

? do

??? Echo $i

? ? ? i= ' expr $i +1 '

? done


The while loop prints 1 to 100 of the and

? J=0

? I=1

? while (i<=100)

? Do

????? j= ' expr $i + $j '

? ? ? ? ((i++))

? done

? echo $j????

?


Writing a For loop and a while loop case with the shell

Related Article

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.