Shell Script Programming Learning notes-for loops

Source: Internet
Author: User
Tags rsyslog

1.FOR Loop structure 1.1 for loop structure syntax

Grammar:

For variable name in variable Value list

Do

Instructions...

Done

Tip: In this structure, "in variable Value list" can be omitted, when omitted is equivalent to in "[email protected]", using for I is equivalent to I in "[email protected]".

1.2 C-language for loop structure

Grammar:

For ((EXP1;EXP2;EXP3))

Do

Instructions...

Done

Example: Comparison of While loops and for loops

[[email protected] jiaobenlianxi]# cat while4.sh #!/bin/bashi=1while((i<=10))do    echo $i    ((i++))done[[email protected] jiaobenlianxi]# cat for1.sh #!/bin/bashfor((i=1;i<=10;i++))do    echo $idone

Description

(1) The program continues to run multi-use while, including daemons and with read read into the loop processing.

(2) The finite-cycle multi-use for, the work for use more.

How to learn Memory:

For men in the world

Do

If [with room] && [car] && [deposit] && [will do housework] && [handsome] && [considerate] && [shopping] ; then

echo "I Like You"

Else

Rm–f Man

Fi

Done

1.3 For Loop Basic Example 1.3.1 Example 1: List all elements of a variable directly, print 1,2,3,4,5

Method 1:

[[email protected] jiaobenlianxi]#  cat for3.sh for n in 1 2 3 4 5 doecho $ndone

Method 2: Use the curly braces method

[[email protected] jiaobenlianxi]# cat for4.sh for n in {1..10}doecho 192.168.1.$ndone

Method 3: Use SEQ

[[email protected] jiaobenlianxi]# cat for5.sh for n in  `seq 5`doecho 192.168.1.$ndone

Prints the file name of the current directory with A For loop, implemented with SEQ

Tip: A variable Value list can also be used with command output

[[email protected] jiaobenlianxi]# cat for5.sh for n in  `ls`doecho $ndone
1.3.2 Example 2: Random number Generation file

Batch generated file, file name random MD5 processing, select eight bits.

[[email protected] jiaobenlianxi]# cat suijiwnjian.sh for((i=1;i<=10;i++))do    mkdir -p ./test    touch ./test/` echo $RANDOM|md5sum|cut -c 1-8`_finished.htmldone[[email protected] jiaobenlianxi]# ls test/0ac2453a_finished.html  8b90c52b_finished.html  c4eb22c9_finished.html2c1f5e87_finished.html  a22717d3_finished.html  db258218_finished.html6d6bc69f_finished.html  a4aca3b0_finished.html6d7c516a_finished.html  c21f3d55_finished.html
1.3.3 Example 3: Batch modification of file names

Modify the resulting 10 file ending in. html to end with. jpg. And remove the finished and use the For loop only.

Method One:

[r[email protected] jiaobenlianxi]# cat piliangxiugai1.sh for f in `ls test/*.html`do    mv $f `echo $f|sed ‘s#_finished.html#.jpg#g‘`done[[email protected] jiaobenlianxi]# ls test/0ac2453a.jpg  6d6bc69f.jpg  8b90c52b.jpg  a4aca3b0.jpg  c4eb22c9.jpg2c1f5e87.jpg  6d7c516a.jpg  a22717d3.jpg  c21f3d55.jpg  db258218.jpg

Method two: For loop plus variable partial intercept method

[[email protected] jiaobenlianxi]# cat piliangxiugai3.sh for file in `ls test/*.html`do    /bin/mv $file "${file%_finished*}.jpg"done

Method Three: LS combined with awk implementation

[[email protected] test]# ls0513d6f0_finished.html  6ab3573c_finished.html  8abfa660_finished.html1a9335f3_finished.html  70018e1c_finished.html  bb6763ab_finished.html559d16bc_finished.html  74e206b5_finished.html58fc15d7_finished.html  7878f84a_finished.html[[email protected] test]# ls |awk -F ‘[_]‘ ‘{print "mv " $0,$1".jpg"}‘|bash[[email protected] test]# ls0513d6f0.jpg  559d16bc.jpg  6ab3573c.jpg  74e206b5.jpg  8abfa660.jpg1a9335f3.jpg  58fc15d7.jpg  70018e1c.jpg  7878f84a.jpg  bb6763ab.jpg

Method Four: Realize by rename

[[email protected] test]# ls16d72ca3_finished.html  673c62da_finished.html  b9328787_finished.html37690b31_finished.html  72ebc17d_finished.html  e37aeed5_finished.html37e66161_finished.html  a5050e54_finished.html4efe1f4c_finished.html  b2553039_finished.html[[email protected] test]# rename "_finished.html" ".jpg" *[[email protected] test]# ls16d72ca3.jpg  37e66161.jpg  673c62da.jpg  a5050e54.jpg  b9328787.jpg37690b31.jpg  4efe1f4c.jpg  72ebc17d.jpg  b2553039.jpg  e37aeed5.jpg
1.3.4 Example 4:

Actual Case: Development script implementation only set SSHD Crond rsyslog network boot from

[[email protected] jiaobenlianxi]# cat chkconfigoff.sh LANG=enchkconfig --list|grep 3:on|awk ‘{print $1}‘ >b.logfor name in `chkconfig --list|grep 3:on|awk ‘{print $1}‘|egrep -v "sshd|crond|rsyslog|network"`do  chkconfig $name offdone[[email protected] jiaobenlianxi]# cat chkconfigon.sh LANG=enfor name in `cat b.log`do    chkconfig $name ondone
1.3.5 Example 5: Implementing the sum of 1-100 with a for loop

Method One:

[[email protected] jiaobenlianxi]# cat for1-100.sh for((i=1;i<=100;i++))do    let sum+=idoneecho $sum[[email protected] jiaobenlianxi]# sh for1-100.sh 5050

Method Two:

[[email protected] jiaobenlianxi]# cat for1-1000.sh for n in `seq 100`do    ((sum+=n))doneecho $sum

Method Three:

[[email protected] jiaobenlianxi]# cat for1-100_3.sh for n in `seq 100`do   a="$(($n*($n+1)/2))"doneecho $a

Shell Script Programming Learning notes-for loops

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.