Linux_ Summary of Chapter 13 of Linux command line and shell script programming

Time:2021-10-24

Time: Wednesday, September 13, 2017
Note: some of the contents of this article are taken from the book “complete collection of Linux command line and shell script programming”, and the copyright belongs to the original author. Summary of Chapter 13 of Linux command line and shell script programming (Third Edition)

Chapter 13: more structured commands

Contents of this chapter

For loop statement
The until iteration statement uses the while statement
loop
重定向loop的输出

13.1 for command

Command format

for var in list
do
    commands
done

Command description: the variable VaR is the iterative value in the list. The commands entered between the do and done statements can be one or more standard bash shell commands.

13.1.1 reading the values in the list

The basic use of the for command is to traverse a series of values defined by the for command itself

Write test1.sh script

#!/bin/bash
for test in One The Three Four Five Six
do
    echo "The next number is $test"
done

Write test1b.sh script

#!/bin/bash

for test in One The Three Four Five Six
do
    echo "The next number is $test"
done

echo "The last number is $test"
test=Seven
echo "Now, Test number is $test"

13.1.2 reading complex values in the list

The for loop assumes that each value is separated by spaces
When a value has special characters, it needs to be escaped
You can use escape characters (backslashes) and double quotes
Use double quotes when a value contains spaces

Write test2.sh script

#!/bin/bash
for test in I don\'t know "this'll" work
do
    echo "word:$test"
done

Write test3.sh script

#!/bin/bash
for test in Nevada "New Hampshire" "New Mexico" "New York"
do
    echo "Now going to $test"
done

13.1.3 read list from variable

Write test4.sh script

#!/bin/bash
list="One Two Three"
list=$list" Four"
for number in $list
do
    echo "The number is $number"
done

13.1.4 reading values from commands

Another way to generate the desired values in the list is to use the output of the command
You can replace any command that produces output with a command
Then use the output of the command in the for command

Write Number text

One
Two
Three

Write test5.sh script

#!/bin/bash
file="number"
for number in $(cat $file)
do
    echo "The number is $number"
done

13.1.5 change field separator

The bash shell treats spaces / tabs / newlines as field separators

Use newline as field separator IFS = $'\ n'
The bash shell ignores spaces and tabs
You can also customize the use of specific characters as field separators

Write the test5b.sh script

#!/bin/bash
file="number"
IFS=$'\n'
for number in $(cat $file)
do
    echo "The number is $number"
done

13.1.6 read directory with wildcard

You can use the for command to automatically traverse files in a directory

Write test6.sh script

#!/bin/bash
for file in /home/zc/test/unit13/*
do
    if [ -d "$file" ]
    then
        echo "$file is a directory"
    elif [ -f "$file" ]
    then
        echo "$file is a file"
    fi
done

Write test7.sh script

#!/bin/bash
for file in /home/zc/test/unit13/*.sh /home/zc/test/unit12
do
    if [ -d "$file" ]
    then
        echo "$file is a directory"
    elif [ -f "$file" ]
    then
        echo "$file is a file"
    fi
done

13.2 for command in C language style

13.2.1 for command in C language

C language for format

for (i=0;i<10;i++)
{
    printlf("The next number is $d\n",i);
}

C language style for loop format in Bash

for (( variable assignment ; condition ; iteration process ))
Variable assignment can have spaces
Variables in a condition cannot begin with a dollar sign
The formula of the iterative process does not use the expr command format

Write test8.sh script

#!/bin/bash
for (( i=1; i<=10;i++ ))
do
    echo "The next number is $i"
done

13.2.2 using multiple variables

Write the test9.sh script

#!/bin/bash
for(( a=1,b=10;a<=10;a++,b-- ))
do
    echo "$a - $b"
done

13.3 while command

13.3.1 basic format of while

Command format

while test command
do
    other commands
done

Command description

As like as two peas in the while command, the test command and if-then statements are identical in format.
The key to the while command is that the exit status code of the specified test command must change with the command running in the loop
If the exit status code does not change, the while loop will continue

Write test10.sh script

#!/bin/bash
var1=10
while [ $var1 -gt 0 ]
do
    echo $var1
    var1=$[ $var1 - 1 ]
done

13.3.2 using multiple test commands

The while command allows you to define multiple test conditions in a while statement
Only the exit state of the last test condition will be used to determine when to end the loop
Notice that each test command appears on a separate line

Write test11.sh script

#!/bin/bash
var1=10
while echo $var1
      [ $var1 -ge 0 ]
do
    echo "The is inside the loop"
    var1=$[ $var1 - 1 ]
done

13.4 until command

Command format

until test commands
do
    other commands
done

Command description

The until command and the while command work in exactly the opposite way
The until command requires you to specify a test command that usually returns a non-zero exit status code
The bash shell will execute the commands listed in the loop only if the exit status code of the test command is not 0
Once the test command returns the exit status code 0, the loop ends

Write test12.sh script

#!/bin/bash
var1=100
until [ $var1 -eq 0 ]
do
    echo $var1
    var1=$[ $var1-25 ]
done

Write test13.sh script

#!/bin/bash
var1=100
until echo $var1
      [ $var1 -eq 0 ]
do
    echo Inside the loop: $var1
    var1=$[ $var1-25 ]
done

13.5 nested loops

Loop statements can use any type of command within a loop, including other loop commands

Write test14.sh script

#!/bin/bash

for (( a=1;a<=3;a++))
do
    echo "Starting loop $a:"
    for (( b=1;b<=3;b++))
    do
        echo "  Inside loop:$b"
    done
done

Write test15.sh script

#!/bin/bash

var1=5

while [ $var1 -ge 0 ]
do
    echo  "Outer loog:$var1"
    for (( var2=1;$var2<3;var2++ ))
    do
        var3=$[ $var1 * $var2 ]
        echo "  Inner loop:$var1*$var2=$var3"
    done
    var1=$[ $var1 - 1 ]
done

Write test16.sh script

#!/bin/bash

var1=3

until [ $var1 -eq 0 ]
do
    echo "Outer loop:$var1"
    var2=1
    while [ $var2 -lt 5 ]
    do
        var3=$(echo "scale=4;$var1 / $var2" | bc)
        echo "  Inner loop:$var1 / $var2 = $var3"
        var2=$[ $var2+1 ]
    done
    var1=$[ $var1 - 1 ]
done

13.6 cyclic processing of file data

Generally, the data stored in the file must be traversed, which needs to be implemented in combination with the following technologies

Use nested loop: use the loop again to read the data in each row
Modify IFS environment variables: force the for command to treat each line in the file as a separate entry

13.7 control cycle

13.7.1 break command

Use the break command to exit any type of loop, including while and until loops

1. Jump out of a single cycle

Write test17.sh script

#!/bin/bash
for var1 in 1 2 3 4 5 6 7 8 9 10
do
    if [ $var1 -eq 5 ]
    then
        break
    fi
    echo "Iteration number:$var1"
done
echo "The for loop is completed"

Write test18.sh script

#!/bin/bash
var1=1
while [ $var1 -lt 10 ]
do
    if [ $var1 -eq 5  ]
    then
        break
    fi
    echo "Iteration:$var1"
    var1=$[ $var1+1 ]
done
echo "The while loop is completed"

2. Jump out of the internal loop

Write test19.sh script

#!/bin/bash
for (( a=1;a<4;a++ ))
do
    echo "Outer loop:$a"
    for (( b=1;b<100;b++ ))
    do
        if [ $b -eq 5 ]
        then
            break
        fi
        echo "  Inner loop:$b"
    done
done

3. Jump out of the external loop

Write the test20.sh script

#!/bin/bash

for (( a=1;a<4;a++ ))
do
    echo "Outer loop:$a"
    for(( b=1;b<100;b++ ))
    do
        if [ $b -gt 4 ]
        then
            break 2
        fi
        echo "  Inner loop:$b"
    done
done

13.7.2 continue command

Write test21.sh script

#!/bin/bash
for (( var1=1;var1<15;var1++ ))
do
    if [ $var1 -gt 5 ] && [ $var1 -lt 10 ]
    then
        continue
    fi
    echo "Iteration number:$var1"
done

Write test22.sh script

#!/bin/bash
for (( a=1;a<=5;a++ ))
do
    echo "Iteration $a:"
    for (( b=1;b<3;b++ ))
    do
        if [ $a -gt 2 ] && [ $a -lt 4 ]
        then
            continue 2
        fi
        var3=$[ $a * $b ]
        echo "  The result of $a * $b = $var3"
    done
done

13.8 output of processing cycle

The output of the loop is piped or redirected by adding a processing command after the done command

Write test23.sh script

#!/bin/bash
for (( a=1;a<10;a++ ))
do
    echo "The number is $a"
done > test23.txt
echo "The command is finished"

Write the test24.sh script

#!/bin/bash
for state in "North Dakota" Connecticut Illinois Alabama Tennessee
do
    echo "$state is the next plae to go"
done | sort
echo "This completes our travels"

13.9 examples

13.9.1 finding executable files

Write the test25.sh script

#!/bin/bash

IFS=:
for folder in $PATH
do
    echo "$folder:"
    for file in $folder/*
    do
        if [ -x $file ]
        then
            echo "  $file"
        fi
    done
done

13.9.2 create multiple user accounts

Write the test26.sh script

#!/bin/bash
input="users.csv"
while IFS=',' read -r userid name
do
    echo "adding $userid"
    useradd -c "$name" -m $userid
done < "$input"

Write the users.csv file

rich,Richard Blum
tim,Timothy Bresnahan

13.10 summary

The bash shell provides three loop commands that can be used in scripts

The for command allows you to traverse a series of values
The while command provides a loop based on command conditions
The until command is based on the non-zero exit status code generated by the command or condition

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]