Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

For/while/until Loop Command in shell programming

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

1. For command

In shell programming, sometimes we need to repeat the command until a particular condition is reached. In bash shell, the for command is provided, which allows you to create a loop that iterates through a series of values, and each iteration executes a set of predefined commands through a value in the series.

Basic format of for:

For var in list

Do

Commands

Done

In list, you provide a series of values to be used in the iteration. In each iteration, the variable var contains the current value in the list, the first iteration applies the first value in the list, the second iteration uses the second value, and so on until all the values in the list are passed.

1.1 read values in the list

[root@sh shell] # cat for 1. Sh for1.sh the next state is aaathe next state is bbbthe next state is cccthe next state is ddd sh for1.sh the next state is aaathe next state is bbbthe next state is cccthe next state is ddd for test in aaa bbb ccc ddddo echo the next state is $testdone [root@sh shell]

The value of the $test variable remains valid for the last iteration of the shell script unless you modify it

[root@sh shell] # cat for1.sh #! / bin/bashfor test in aaa bbb ccc ddddo echo the next state is $testdoneecho "the last state we visited was $test" test=fffecho "wait. Now we're visiting $test" [root@sh shell] # sh for1.sh the next state is aaathe next state is bbbthe next state is cccthe next state is dddthe last state we visited was dddwait. Now we're visiting fff

1.2 read complex values in the list

In shell scripts, you will encounter difficult numbers to deal with. Here is a classic example of causing trouble for shell scripting programmers:

[root@sh shell] # cat for2.sh #! / bin/bashfor test in I don't know if this'll workdo echo "word:$test" done [root@sh shell] # sh for2.sh word:Iword:dont know if thisllword:work

Solution: use escape characters or double quotation marks

[root@sh shell] # cat for2.sh #! / bin/bashfor test in I don\'t know if "this'll" workdo echo "word:$test" done [root@sh shell] # sh for2.sh word:Iword:don'tword:knowword:ifword:this'llword:work

Remember: the for loop assumes that each value is separated by spaces, and if there are spaces in separate numeric values, you must use double quotation marks to enclose those values.

1.3 read the list from variables

[root@sh shell] # cat for3.sh # #! / bin/bashlist= "aaa bbb ccc ddd eee" list=$list "Connecticut" for state in $listdo echo "Have you ever visited $state" done [root@sh shell] # sh for3.sh Have you ever visited aaaHave you ever visited bbbHave you ever visited cccHave you ever visited dddHave you ever visited eeeHave you ever visited Connecticut

1.4 read values from a command

[root@sh shell] # cat for4.sh #! / bin/bashfile= "/ root/shell/states" # if you do not need an absolute path currently, you can for state in `cat $file`do echo "Visit beautiful $state" done [root@sh shell] # sh for4.sh Visit beautiful shanghaiVisit beautiful beijingVisit beautiful hangzhouVisit beautiful nanjingVisit beautiful guangzhou [root@sh shell] # cat states shanghaibeijinghangzhounanjingguangzhou

1.5 change the field delimiter

Blank space

Tabs:

Newline character

If bash shell sees any of these characters in the data, it assumes that you have started a new segment in the list.

To solve this problem, you can temporarily change the value of the IFS environment variable in your shell script to limit the characters that bash shell uses as a field delimiter. But this is a bit strange, for example, if you line the value of IFS so that it can only recognize newline characters, you must do this:

IFS=$'\ n'

Add this statement to the script and tell bash shell to ignore spaces and tabs in the data values.

[root@sh shell] # cat for5.sh #! / bin/bashfile= "states" IFS=$'\ n'for state in `cat $file`do echo "Visit beautiful $state" done [root@sh shell] # sh for5.sh Visit beautiful shanghaiVisit beautiful beijingVisit beautiful hangzhouVisit beautiful nanjingVisit beautiful guang zhouVisit beautiful nan ningVisit beautiful jiang nan

In dealing with long scripts, you may need to change the value of IFS in one place, and then forget that it restores the default values elsewhere in the script.

For example:

IFS.OLD=$IFS

IFS=$'\ n'

IFS=$IFS.OLD

Other IFS values, such as: may be used in / etc/passwd

IFS=:

You can also assign multiple IFS:

IFS=$'\ nPart; "'

1.6 read the directory with wildcard characters

[root@sh shell] # cat for6.sh #! / bin/bashfor file in/ home/*do if [- d "$file"]; then echo "$file is a directory" elif [- f "$file"] Then echo "$file is a file" fidone [root@sh shell] # sh for6.sh / home/apache-tomcat-8.0.28.tar.gz is a file/home/dir1 is a directory/home/dir2 is a directory/home/fie1 is a file/home/fie2 is a file/home/fie22 is a file [root@sh shell] # cat for7.sh #! / bin/bashfor file in/ home/* / home/badtestdo if [- d "$file"] Then echo "$file is a directory" elif [- f "$file"]; then echo "$file is a file" else echo "$file doesn't exist" fidone [root@sh shell] # sh for7.sh / home/apache-tomcat-8.0.28.tar.gz is a file/home/dir1 is a directory/home/dir2 is a directory/home/fie1 is a file/home/fie2 is a file/home/fie22 is a file/home/badtest doesn't exist

II. For commands in C language style

2.1C-style for commands

The C language for command has a special way to specify variables, a condition that must remain true to continue iterations, and another way to change variables for each iteration. When the specified condition does not hold, the for loop stops. Conditional equations are defined by standard numeric symbols.

For (iTuno; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report