shell script step one (for, while, continue, break, select, etc.)

Posted by neclord81 on Tue, 27 Aug 2019 07:11:27 +0200

Script step one

The second way of writing for cycle:

As we all know, for has two ways of writing

  • The first is: for I in k8s-node {1.3}; do setenforce 0;
  • The Second Writing Style: C Language Style

Write directly how to use:

#After for, you must write two parentheses, also known as double parentheses.
[root@linux1 ~]# cat for_2.sh 
#!/bin/bash

for ((i=1,sum=0;i<=100;i++));do
    let sum+=i
done
echo "sum=${sum}"
[root@linux1 ~]# bash for_2.sh
sum=5050

II. while Cycle

I like to write like this, keep looping and break out.

[root@linux1 ~]# cat while_sum.sh
#!/bin/bash
i=1
sum=0
while true;do
    let sum+=i
    let i++
    if [ $i -gt 100 ];then
        break
    fi
done
echo "sum=${sum}"
[root@linux1 ~]# bash  while_sum.sh 
sum=5050

Advanced Use of while: Read Standard Input for Circulation

[root@linux1 ~]# cat while_2.sh
#!/bin/bash
while read line
do
    echo $line
done < /etc/fstab
[root@linux1 ~]# bash while_2.sh

#
# /etc/fstab
# Created by anaconda on Thu Aug 8 19:04:39 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=3778e6e0-8f51-4843-8b8f-239c8b5e826b /boot xfs defaults 0 0
/dev/mapper/centos-home /home xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0

And according to the information of netizens, wile uses redirection mechanism in the above writing. The content of fstab file is redirected to the whole while statement. This feature should be noted below.

Notice 1: Pipeline delivery: echo "abc xyz" | while reading line; do {}; Don is well known that pipes open sub-shells, arrays within sub-shells, variables, functions are not valid outside functions

#!/bin/bash
echo "abc xyz" | while read line
do
    new_var=$line
done
echo new_var is null: $new_var?

III. until cycle

until CONDITION; do loop 
done 

Entry condition: CONDITION is false 
Exit condition: CONDITION is true 

IV. continue Special Usage

continue [N]: End the cycle of Layer N ahead of time, with the innermost layer being Layer 1

The default N is 1, only one layer of loops is returned. For example, if the complex code has several layers, continue 2 can terminate the two layers of loop in advance and go directly to the next round of judgment. The code of the continue waiting surface does not execute.

V. Use of break

Brea [N]: End the N-tier cycle ahead of time, with the innermost layer being Layer 1

Note: This directly ends all the loops in Layer N, while continue just ends the loops in Layer N.

6. shift command

shift [n]

It's important. It's often used in scripts. Move the parameter list left n times

[root@linux1 ~]# cat shift.sh 
#!/bin/bash
#Determine whether the script parameter is 0 or not, and execute a loop if it is not 0
while [ $# -ne 0 ];do
    echo $1  #Print the first parameter
    shift       #All parameters move left, the first parameter is squeezed out, and the second parameter becomes the first parameter.
done
[root@linux1 ~]# ./shift.sh a b c d f
a
b
c
d
f

Seventh, select Loop and Menu

select is often used with case; there's PS3 as a prompt; and there's break or exit to exit the loop

[root@linux1 ~]# cat select.sh 
#!/bin/bash
PS3="What do you want to do?"
select choice in eating wc sleep quit
do
    case $choice in
        eating)
            echo "you can eat some food now."
            ;;
        wc)
            echo "you can go go to wc now."
            ;;
        sleep)
            echo "you can go to sleep now."
            ;;
        quit)
            exit 0
    esac
done

Effect

[root@linux1 ~]# bash select.sh
1) eating
2) wc
3) sleep
4) quit
 What do you want to do: 1
you can eat some food now.
What do you want to do: 2
you can go go to wc now.
What do you want to do: 3
you can go to sleep now.
What do you want to do: 4

VIII. FUNCTIONS

Load function:

  • . filename
  • source filename

9. Delete function

You can use unset to delete

For example: a simple function, no problem with execution

[root@linux1 ~]# cat function.sh
#!/bin/bash
hi(){
    echo hi
}
hi
[root@linux1 ~]# bash function.sh
hi

Adding an unset line in the middle will cause an error because the function has been deleted.

[root@linux1 ~]# cat function.sh
#!/bin/bash
hi(){
    echo hi
}
unset hi
hi
[root@linux1 ~]# bash function.sh
function.sh: line 6: hi: command not found

It can also be implemented by defining empty functions, which I found in the scripts in the system.

# ubuntu's / lib/lsb/init-functions
# Pre&Post empty function declaration, to be overriden from /lib/lsb/init-functions.d/*
log_daemon_msg_pre () { :; } 
log_daemon_msg_post () { :; }
log_begin_msg_pre () { :; }
log_begin_msg_post () { :; }
log_end_msg_pre () { :; }
log_end_msg_post () { :; }
log_action_msg_pre () { :; }
log_action_msg_post () { :; }
log_action_begin_msg_pre () { :; }
log_action_begin_msg_post () { :; }
log_action_end_msg_pre () { :; }
log_action_end_msg_post () { :; }

10. Survival Time of Functional Variables

Environment variables: Current shells and child shells are valid

Local variables: valid only in the current shell process, including script functions

Local variables: the life cycle of a function and the destruction of variables at the end of the function

Definition of local variables: local AGE=20

11. Recursion of Functions

Associate with fork bomb

:(){:|:&};:

Script implementation

cat bomb.sh
#!/bin/bash
./$0|./$0&

12. Signal capture trap

Look at another article

Topics: Linux CentOS Anaconda Ubuntu shell