Shell--for in Loop __shell

Source: Internet
Author: User
For in format
For no $variable in string
Do
$variable
Done
A simple string enumeration calendar, using for in format to cut strings by spaces
SERVICES="80   22   25   110   8000   23   20   21   3306   "
for   x   in   $SERVICES
Do
iptables   -A   INPUT   -p   tcp   --dport   $x   -m   state   --state   NEW   -j   ACCEPT  
Done
For variable in values
/bin/sh!
For I in a B C string list a B C
Strings are separated by spaces, no parentheses, no commas, and are assigned to variables I in turn
Variable does not$
Do
echo "i is $i"
Done
[macg@machome ~]$ sh test.sh
I is a
I is b
I is C
In for in, variable and * are not equivalent
/bin/bash!
for i in *.h ;
Do
Cat ${i}.h
Done
[macg@vm test]$ ./tip.sh
cat: *.h.h: No such file or directory
$I represents the entire path, not the part before. H in *. H
correction
/bin/bash!
For I in *.h
Do
Cat $i
Done
[macg@vm test]$ echo hahaha >>1.h
[macg@vm test]$ echo ha >>2.h
[macg@vm test]$ ./tip.sh
Hahaha
Ha
Example 2:
for i in /etc/profile.d/*.sh
Do
$i
Done
$I stands for / etc / profile.d/color.sh,
/etc/profile.d/alias.sh, /etc/profile.d/default.sh
For in traverses (command line, function) parameters
Test ()
{
Local I
for i in $* ; do
echo "i is $i"
Done
}
$* is a string: save all parameters as "Parameter1 parameter2..."
$I is the applied representation of variable I
[macg@machome ~]$ sh test.sh p1 p2 p3 p4
I is P1
I is P2
I is P3
I is P4
For in statement is used with wildcard * to batch process files
Batch change file name
[root@vm testtip]# ls
aaa.txt  ccc.txt  eee.txt  ggg.txt  hhh.txt  jjj.txt  lll.txt  nnn.txt
bbb.txt  ddd.txt  fff.txt  go.sh    iii.txt  kkk.txt  mmm.txt  ooo.txt
[root@vm testtip]# cat go.sh
For I in *. TXT *. TXT is equivalent to an array of strings, which are assigned to I in turn
Do
mv "$i" "$i.bak"
Done
[root@vm testtip]# sh go.sh
[root@vm testtip]# ls
aaa.txt.bak  ccc.txt.bak  eee.txt.bak  ggg.txt.bak  hhh.txt.bak  jjj.txt.bak  lll.txt.bak  nnn.txt.bak bbb.txt.bak  ddd.txt.bak  fff.txt.bak  go.sh        iii.txt.bak  kkk.txt.bak  mmm.txt.bak  ooo.txt.bak
The for in statement is used with ` ` and $() to combine multiple lines into one line, which is actually a string array
for i in $(ls *.txt)
Do
Echo $i
Done
[macg@machome ~]$ sh test
111-tmp.txt
111.txt
22.txt
33.txt
In other words, use for in to overcome the defect of combining multiple lines of ` ` and $() into one line
Using for in to automatically traverse strings by spaces, traverse multiple directories
LIST="rootfs usr data data2"
for d in $LIST; do
mount /backup/$d
rsync -ax --exclude fstab --delete /$d/ /backup/$d/
umount /backup/$d
Done
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.