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

Advanced script one

One, the second way of writing for loop:

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

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

< strong>How to write directly:

Two parentheses must be written after #for, also known as double parentheses.
[[emailprotected] ~]# cat for_2.sh < br />#!/bin/bash

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

Second, while Loop

I like to write like this, loop forever and exit with break

[[emailprotected] ~]# 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}"
[[emailprotected] ~]# bash while_sum.sh
sum=5050

Advanced usage of while: read the content of standard input to realize a loop

[[emailprotected] ~]# cat while_2.sh< br />#!/bin/bash
while read line
do
echo $line
done [[email prot ected] ~]# 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 looking at the information of netizens, the above is written in wile uses a redirection mechanism, and the content of the fstab file is redirected to the entire while statement. This feature should be paid attention to.

Note 1 of while: :Echo “abc xyz” | while read line ;do {};done It is well known that the pipeline will open the subshell, and the arrays, variables, and functions in the subshell will 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 loop

until CONDITION; do loop body
done

Enter condition: CONDITION is false
Exit condition : CONDITION is true

Four, continue special usage

continue [N]: End the current loop of the Nth layer early, the most The inner layer is the first layer

The default N is 1, and only one layer is returned to the loop. For example, if there are several layers of complex code, continue 2 can directly end the two-layer loop ahead of time, Go directly to the next round of judgment, and the code of the continue waiting side will not be executed

Five. Break usage

break [N]: End the N-th loop early, and the innermost layer is the first layer

Note:< /strong>This directly ends all the loops of the Nth layer, and continue just ends the current loop of the Nth layer

Six, shift command< /p>

shift [n]

It is very important, and it is often used in scripts. Move the parameter list to the left n times

[[email protected] ~]# cat shift.sh 
#!/bin/bash
#Judge whether the script parameter is 0, not 0 then execute the loop
while [$# -ne 0 ];do
echo $1 #print the first parameter
shift #all the parameters are shifted to the left, the first parameter is squeezed away, the first The two parameters become the first parameter
done
[[email protected] ~]# ./shift.sh abcdf
a
b
c
d
f

Seven, select loop and menu

Select is often used with case; there is also PS3 as a prompt; Yes, it is necessary to cooperate with break or exit to exit the loop

[[email protected] ~]# 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

Effects

[[ email protected] ~]# bash select.sh
1) eating
2) wc
3) sl eep
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

Eight. Function< /p>

Load function:

  • . filename
  • source filename

< strong>Nine. Delete function

You can use unset to delete

For example: a simple function, no problem in execution

< pre>[[email Protected] ~]# cat function.sh
#!/bin/bash
hi(){
echo hi
}
hi
[[email Protected] ~]# bash function.sh
hi

Add a line of unset in the middle, and an error will be reported because the function has been deleted

[[email protected] ~]# cat function.sh
#!/bin/bash
hi(){
echo hi
}
unset hi
hi< br />[[email Protected] ~]# bash function.sh
function.sh: line 6: hi: command not found

It can also be achieved by defining an empty function, this I found it in the script in the system

# 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 () {:; }< br />log_action_end_msg_pre () {:; }
log_action_end_msg_post () {:; }

10. Lifetime of function variables

Environment Variables: valid in the current shell and subshell

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

Local variables: the life cycle of the function, the variable is destroyed when the function ends

p>

The definition of local variables: local AGE=20

11. Function recursion

fork bomb

:(){:|:&};:

Script implementation

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

12. Signal capture trap

Look at another article strong>

Leave a Comment

Your email address will not be published.