For loop of shell scripts

Source: Internet
Author: User

There are many types of for loops in shell scripts, and a brief summary of the following is done today.

1. Cyclic Digital
#!/bin/bash for((i=1; i<=; i++));   do     Echo $ (expr31);  Done
 #!/bin/bash   for  i in  $ (seq  1  10  )   do  echo  $ (expr  $i \* 3  + 1   done  
#!/bin/bashfor in {1.. Ten }do    Echo $ (expr31);  Done
#!/bin/awk'begin{for (i=1; i<=10; i++) Print i}'

Results:

4 7 Ten  -  -  +  A  -  -  to
2. Loop string
#!/bin/bashfor in 'ls';   do     Echo file name\!  ;  Done

Result (output current file name):

for. SH
#!/bin/bashfor in $* ;   do   echo $i is input chart\!  ;  Done

Result (all current input parameters are output):

192 SH for. SH param1 param2 param3param1 is input chart! param2 is input chart! param3 is input chart!
#!/bin/bashfor in  F1 F2 F3;   do    Echo $i is appoint;  Done

Results (circular spaces separated by the character list):

192 SH for. SH F1 is APPOINTF2 are APPOINTF3 is appoint
#!/bin/bashlist="rootfs usr data data2"for in  $list;  do    echo  $i is appoint;  Done

Results (circular list):

192 SH for. SH Rootfs is appointusr is Appointdata are Appointdata2 is appoint
3. Path Lookup
#!/bin/forfile in/volumes/work/*; Do    echo $file is file path \!;d One

Result (loop to find current directory):

192: Shell-home xxx$SH  for.SH/volumes/work/bak_0_media isfilePath!/volumes/work/bak_0_text isfilePath!/volumes/work/apache-maven-3.5.3IsfilePath!/volumes/work/apache-tomcat-9.0.8IsfilePath!/volumes/work/chromedriver isfilePath!/volumes/work/git-repository isfilePath!/volumes/work/intellij-setting-files isfilePath!/volumes/work/justfortest isfilePath!/volumes/work/mavenrepository isfilePath!/volumes/work/pathfordownload isfilePath!/volumes/work/selenium-server-standalone-3.0.0. Jar isfilePath!/volumes/work/shell-home isfilePath!
#!/bin/bashforfile in $ (ls /volumes/work)do     echo $filefile path \! ;  Done

Result (the result of cyclic LS removal):

192: Shell-home xxx$SH  for.SHBak_0_media isfilePath!Bak_0_text isfilePath!Apache-maven-3.5.3IsfilePath!Apache-tomcat-9.0.8IsfilePath!Chromedriver isfilePath!git-repository isfilePath!IntelliJ-setting-files isfilePath!Justfortest isfilePath!Mavenrepository isfilePath!Pathfordownload isfilePath!Selenium-server-standalone-3.0.0. Jar isfilePath!Shell-home isfilePath!

For loop of shell scripts

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.