Advanced a shell script (for, while, continue, break, select, etc.)

Advanced a script

A, for the second cycle of writing:

As we all know, for there are two ways for writing

  • The first: for i in k8s-node {1..3}; do setenforce 0; done
  • The second written: C language style

How to use the direct write:

#for后必须写两个括号,又称双小括号写法
[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

Two, while circulation

I like to write, and then exit with a break has been circulating

[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

while advanced usage: read standard input content to achieve the cycle

[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 information users see that the above wording the wile use redirection mechanism, fstab file content is redirected to the entire while statement, this feature should pay attention to the next

while the Note 1: piped contents: echo "abc xyz" | while read line; do {}; done is well known that the pipeline will open sub-shell, the array within the sub-shell, variables, functions not take effect outside the function

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

Three, until the cycle

until CONDITION; do  循环体 
done 

进入条件: CONDITION 为false 
退出条件: CONDITION 为true 

Four, continue special usage

continue [N]: N layer ahead of the end of the round cycle , innermost layer a first layer

The default is N 1, back only one cycle. Complex code such as a set of several layers, then, continue 2 may be directly ahead of the end of two cycles, directly to the next one is determined , continue codes designate the face is not performed

Five, break usage

break [N]: N layer ahead of the end of the cycle, the innermost layer a first layer

Note: This ends the cycle of all the N layer, and continue just the end of the round cycle of layer N

Six, shift command

shift [n]

It is important, frequently used scripts. The parameter list to the left n times

[root@linux1 ~]# cat shift.sh 
#!/bin/bash
#判断脚本参数是否为0,不为0则执行循环
while [ $# -ne 0 ];do
    echo $1  #打印第一个参数
    shift       #所有参数左移,第一个参数被挤走,第二个参数变成第一个参数
done
[root@linux1 ~]# ./shift.sh a b c d f
a
b
c
d
f

Seven, select the menu cycle

select frequently used together with the case; there is the PS3 as prompt; there is the exit to tie break or exit the loop

[root@linux1 ~]# cat select.sh 
#!/bin/bash
PS3="你想干啥:"
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
你想干啥:1
you can eat some food now.
你想干啥:2
you can go go to wc now.
你想干啥:3
you can go to sleep now.
你想干啥:4

Eight function

Load function:

  • . filename
  • source filename

Nine, delete function

You can use unset deleted

For example: a simple function, we do not question

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

The middle of his party unset, on the 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 achieved by defining an empty function, which I found in the system script

# ubuntu的/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 () { :; }

Ten, the survival function of time variable

Environment variables: the current shell sub-shell and effective

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

Local variables: life cycle functions, the end of the function variables destruction

Define local variables: local AGE = 20

XI recursive function

Lenovo to fork bomb

:(){:|:&};:

Script to achieve

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

XII capture trap signal

See another article

Guess you like

Origin blog.51cto.com/14012942/2432892