Shell script Programming-structured Command 2-for command

Source: Internet
Author: User

1. For command (1) syntax
    for Val in list; Do        commands    done

The list parameter provides some values for the iteration, and the Val value is assigned to the value in list, knowing that the list polling ends.

Commands can be one or more shell commands, echo $val can view the value of the current loop (2) Read the value in the list
    $cat Test    #!/bin/bash    # Basic for command for    test in A B C; do        echo the next Val was $test    done    $. /test the next Val is A next Val was B the next Val is    C
Each time the for command facilitates the list of values provided, the next value in the list is assigned to the $test variable.    In the last iteration, the value of $test will remain in effect for the remainder of the shell script. When the value of the iteration contains spaces, single quotes, and double quotation marks, the for command does not recognize it as a worthwhile part and can be handled in two ways: When a space, single quotation mark appears in the traversed value, you can differentiate by adding double quotation marks around the value, and when single or double quotation marks appear in the traversed value, you can (3) Reading a value from a variable or command typically, a shell script encounters a situation where a series of values is stored in a variable, and then the entire list of variables is traversed.
$cat Test    #!/bin/bash    # using a variable to hold the list    list= "a B C"    list= $list "D" for    test in $lis T Do echo the next Val are $test done $./test the next Val are A next Val is        B the    next Val is c< C11/>the Next Val is D
Adding a value to a known variable is implemented by list= $list "D", a common way to add text to the tail. Another way to generate a traversal list is to use the output of the command, which can be reversed to execute any command that produces output
    $cat Test    #!/bin/bash    # reading value from a file    file= "alphabet" for    test in ' cat $file ';        Ho the next val is $test    do    $./test The    next Val is A    next Val was B the    next Val is C
(4) Changing the field delimiter bash defines the special environment variable ifs, called the internal field delimiter.    By default, bash takes spaces, tabs, and line breaks as field separators.    When we are in use, we can modify the value of IFS to meet different situations. Modify the IFS value format, such as: ifs=$ ' \ n ', ifs=: general situation when you download a long script, you need to save the value of the IFS in a temporary variable before recovering it after use:
    Ifs. old= $IFS    ifs=$ ' \ n '    <use the new IFS value in Code>    ifs= $IFS. Old
If you need more than one IFS character, simply string them up at the time of assignment: ifs=$ ' \ n:; ', so that line breaks, colons, good points, and double quotes are treated as characters by delimiter. (5) using wildcard characters to traverse a directory
    $cat test    #!/bin/bash    #iterate through all of the files in a directory for    file in/home/test/*, do        if [-D "$file"]; Then            echo "$file is a directory"        Elif [-F "$file"]; then            echo "$file was a file"        fi    Done
Files or directories in Linux can contain spaces, so file needs to use double quotation marks "$file".

Shell script Programming-structured Command 2-for command

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.