linux - control statements commonly used in shell for, while, if, case, expect, exit, break, continue

Keywords: Linux vim MySQL SQL

I. for statement

The command grammar is as follows:

for NUM in 1 2 3 
for NUM in {1..3}
for NUM in `seq 1 3`perhapsfor NUM in `seq 1 2 10`
for (( Expression1;Expression2;Expression3))
do
done

for statement demonstration

[root@desktop27 mnt]# vim for.sh 
[root@desktop27 mnt]# cat for.sh 
#!/bin/bash
for NUM in 1 2 3
do
    echo $NUM
done
[root@desktop27 mnt]# sh for.sh 
1
2
3
[root@desktop27 mnt]# vim for.sh 
[root@desktop27 mnt]# cat for.sh 
#!/bin/bash
for NUM in {1..3}
do
    echo $NUM
done
[root@desktop27 mnt]# sh for.sh 
1
2
3
[root@desktop27 mnt]# vim for.sh 
[root@desktop27 mnt]# cat for.sh 
#!/bin/bash
for NUM in `seq 1 3`
do
    echo $NUM
done
[root@desktop27 mnt]# sh for.sh 
1
2
3
[root@desktop27 mnt]#
[root@desktop27 mnt]# vim for.sh 
[root@desktop27 mnt]# cat for.sh 
#!/bin/bash
for NUM in `seq 1 2 5`
do
    echo $NUM
done
[root@desktop27 mnt]# sh for.sh 
1
3
5
[root@desktop27 mnt]# vim for.sh 
[root@desktop27 mnt]# cat for.sh 
#!/bin/bash
for ((NUM=1;NUM<=3;NUM++))
do
    echo $NUM
done
[root@desktop27 mnt]# sh for.sh 
1
2
3
[root@desktop27 mnt]#

for statement - backup database

[root@desktop27 mnt]# yum install mariadb-server -y
[root@desktop27 mnt]# systemctl start mariadb
[root@desktop27 mnt]# mysql -uroot
##Setting up the database
[root@desktop27 mnt]# mysql -uroot -e "show databases;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| linux              |
| mysql              |
| performance_schema |
| test               |
| westos             |
+--------------------+
[root@desktop27 mnt]# mysql -uroot -EN -e "show databases;"
*************************** 1. row ***************************
information_schema
*************************** 2. row ***************************
linux
*************************** 3. row ***************************
mysql
*************************** 4. row ***************************
performance_schema
*************************** 5. row ***************************
test
*************************** 6. row ***************************
westos
[root@desktop27 mnt]# vim dump_mysql.sh
[root@desktop27 mnt]# cat dump_mysql.sh 
#!/bin/bash
DATABASE_MESSAGE=`mysql -uroot -EN -e "show databases;" | grep -E "^\*|schema$" -v`
mkdir -p /mnt/mysql_dump
for DATABASE_NAME in $DATABASE_MESSAGE
do
    mysqldump -uroot $DATABASE_NAME > /mnt/mysql_dump/${DATABASE_NAME}.sql
    [ "$?" -eq "0" ]&&{
        echo -e "\033[32m$DATABASE_NAME is backuped !!\033[0m"
    }
done
[root@desktop27 mnt]# sh dump_mysql.sh 
linux is backuped !!
mysql is backuped !!
test is backuped !!
westos is backuped !!
[root@desktop27 mnt]# ls mysql_dump/
linux.sql  mysql.sql  test.sql  westos.sql
[root@desktop27 mnt]# 

Two, while

The command grammar is as follows:

while test_commod ;do
    user_commods;
done

As long as the test_commod command returns 0, the user_commods command block is executed.
At the end of the while loop, the return value of the entire command block is the return value of the last command of the user_commods command. If the user_commods command block is not executed, the return value is 0.

while statement demonstration

##Two ways of writing have the same effect.
[root@desktop27 mnt]# vim while.sh
[root@desktop27 mnt]# cat while.sh 
#!/bin/bash
while true
do
    echo -n `uptime`
    echo -ne "\r    \r"
    sleep 5     5Second refresh
done
[root@desktop27 mnt]# sh while.sh 
^C:14:09 up 30 min, 2 users, load average: 0.00, 0.01, 0.05
[root@desktop27 mnt]#
[root@desktop27 mnt]# cat while.sh 
#!/bin/bash
while true
do
    echo -ne "\r`uptime`    \r"
    sleep 5
done
[root@desktop27 mnt]# sh while.sh 
^C2:18:52 up 34 min,  2 users,  load average: 0.07, 0.07, 0.05  
[root@desktop27 mnt]#

Three, if

The command grammar is as follows:

if
then
elif
then
.
.
.
elif
then
esle
fi

IF grammar demonstration

[root@desktop27 mnt]# vim if.sh 
[root@desktop27 mnt]# cat if.sh 
#!/bin/bash
if
[ "$1" = "a" ]
then
    echo '$1' is a
elif
[ "$1" = "b" ]
then
    echo '$1' is b
elif
[ "$1" = "c" ]
then
    echo '$1' is c
else
    echo unknown $1
fi
[root@desktop27 mnt]# sh if.sh a
$1 is a
[root@desktop27 mnt]# sh if.sh b
$1 is b
[root@desktop27 mnt]# sh if.sh c
$1 is c
[root@desktop27 mnt]# sh if.sh d
unknown d
[root@desktop27 mnt]# sh if.sh h
unknown h
[root@desktop27 mnt]#

1. String Judgment
str1 = str2. True when two strings have the same content and length
str1!= str2
- n str1) True when the length of the string is greater than 0 (string is not empty)
- z str1) True when the length of the string is 0 (empty string)
str1. True when string str1 is not empty
2. Judgment of Numbers
Int1-eq int2.
In1-ne INT2
int1 -gt int2. It is true that int1 is greater than INT2
int1-ge int2. It is true that int1 is greater than or equal to INT2
int1-lt int2. It is true that int1 is less than INT2
int1-le int2. It is true that int1 is less than or equal to INT2
3. Judgment of documents
Users can read it as true
- w file.
User Execution is True
- f file.
- d file.
- c file. Files are character-specific files are true
-b file. Files are block-specific files are true
File size is true when it is not zero
True when the device specified by the file descriptor (default 1) is the terminal
4. Complex Logic Judgment
- a) and
- o) or
No!

Four, case

The case statement matches a value or a pattern, and if the match is successful, the matching command is executed.

The command grammar is as follows:

case value in
//Pattern1)
    command1
    command2
    command3
;;
//Pattern2)
    command1
    command2
    command3
;;
*)
    command1
    command2
    command3
;;
esac

Case works as shown above. The value must be followed by the keyword in, and each pattern must end with a right bracket. Values can be variables or constants. After the matching discovers that the value conforms to a certain pattern, all commands begin to execute until;;;. It is similar to break in other languages, meaning to jump to the end of the whole case statement.
The value will detect each pattern that matches. Once the pattern matching is completed, the corresponding commands for matching patterns are executed and no other patterns are continued. If there is no matching mode, use asterisk * to capture the value, and then execute the following command.

case Statement Demonstration

##if -- Character matching, in sequence
[root@desktop27 mnt]# vim if.sh 
[root@desktop27 mnt]# cat if.sh 
#!/bin/bash
if
[ "$1" = "dog" ]
then
    echo "cat"
elif
[ "$1" = "cat" ]
then
    echo "dog"
else
    echo -e "\033[31mERROR: unknown $1\033[0m"
fi
[root@desktop27 mnt]# sh -x if.sh cat
+ '[' cat = dog ']'
+ '[' cat = cat ']'
+ echo dog
dog
[root@desktop27 mnt]# sh -x if.sh dog
+ '[' dog = dog ']'
+ echo cat
cat
[root@desktop27 mnt]# sh -x if.sh boy
+ '[' boy = dog ']'
+ '[' boy = cat ']'
+ echo -e '\033[31mERROR: unknown boy\033[0m'
ERROR: unknown boy
[root@desktop27 mnt]# 
##case - Matching Characters, No Sequence, Higher Efficiency uuuuuuuuuuu
[root@desktop27 mnt]# vim case.sh 
[root@desktop27 mnt]# cat case.sh 
#!/bin/bash
case $1 in
    dog)
    echo cat
    ;;
    cat)
    echo dog
    ;;
    *)
    echo error
esac
[root@desktop27 mnt]# sh -x case.sh cat
+ case $1 in
+ echo dog
dog
[root@desktop27 mnt]# sh -x case.sh dog
+ case $1 in
+ echo cat
cat
[root@desktop27 mnt]# sh -x case.sh boy
+ case $1 in
+ echo error
error
[root@desktop27 mnt]#

Five, expect

Yum install expect-y install expect tool
expect is an automatic response command for the automatic execution of interactive commands
Span is a monitor program in expect, which will monitor the interaction problems raised by commands after running
send sends questions and answers to interactive commands
"\r" means meeting a car
exp_continue means to continue answering the following questions when the question does not exist
expect eof means to quit the expect environment after answering the question
interact means to stay on the interactive page after the question has been answered.
Set NAME [lindex $argv n] defines variables

Examples of writing two files. sh and. exp

[root@desktop27 mnt]# vim ask.sh 
[root@desktop27 mnt]# cat ask.sh 
#!/bin/bash
read -p "What's your name: " NAME
read -p "How old are you: " AGE
read -p "Which obj you study: " OBJ
read -p "Are you happy? " FEEL
echo "$NAME is $AGE's old and study $OBJ feel $FEEL"
[root@desktop27 mnt]# sh ask.sh 
What's your name: tutu
How old are you: 18
Which obj you study: linux
Are you happy? happy
tutu is 18's old and study linux feel happy
[root@desktop27 mnt]# vim answer.exp
[root@desktop27 mnt]# cat answer.exp 
#!/usr/bin/expect
set timeout 2
spawn /mnt/ask.sh
expect "name:"
send "tutu\r"
expect "old:"
send "18\r"
expect "study:"
send "linux\r"
expect "happy:"
send "happy\r"
expect eof
[root@desktop27 mnt]# expect answer.exp
spawn /mnt/ask.sh
couldn't execute "/mnt/ask.sh": permission denied
    while executing
"spawn /mnt/ask.sh"
    (file "answer.exp" line 2)
[root@desktop27 mnt]# chmod -x /mnt/ask.sh
[root@desktop27 mnt]# expect answer.exp 
spawn /mnt/ask.sh
What's your name: tutu
How old are you: 18
Which obj you study: linux
Are you happy? happy
tutu is 18's old and study linux feel happy
[root@desktop27 mnt]# vim answer.exp 
[root@desktop27 mnt]# cat answer.exp 
#!/usr/bin/expect
set timeout 2
spawn /mnt/ask.sh
expect { 
     name { send "tutu\r";exp_continue }
     old { send "18\r";exp_continue }
     study { send "linux\r";exp_continue }
     happy { send "happy\r" }
}
expect eof
[root@desktop27 mnt]# chmod +x answer.exp 
[root@desktop27 mnt]# /mnt/answer.exp 
spawn /mnt/ask.sh
What's your name: tutu
How old are you: 18
Which obj you study: linux
Are you happy? happy
tutu is 18's old and study linux feel happy
[root@desktop27 mnt]# vim answer.exp 
[root@desktop27 mnt]# cat answer.exp 
#!/usr/bin/expect
set timeout 2
set NAME [ lindex $argv 0]
set AGE  [ lindex $argv 1]
set OBJ  [ lindex $argv 2]
set FEEL [ lindex $argv 3]
spawn /mnt/ask.sh
expect { 
     name { send "$NAME\r";exp_continue }
     old { send "$AGE\r";exp_continue }
     study { send "$OBJ\r";exp_continue }
     happy { send "$FEEL\r" }
}
expect eof
[root@desktop27 mnt]# /mnt/answer.exp tutu 19 linux happy
spawn /mnt/ask.sh
What's your name: tutu
How old are you: 19
Which obj you study: linux
Are you happy? happy
tutu is 19's old and study linux feel happy
[root@desktop27 mnt]# /mnt/answer.exp butterfly 19 linux bad
spawn /mnt/ask.sh
What's your name: butterfly
How old are you: 19
Which obj you study: linux
Are you happy? bad
butterfly is 19's old and study linux feel bad
[root@desktop27 mnt]#

Write an example of. sh as a file (the same effect as the previous example)

[root@desktop27 mnt]# vim answer.exp 
[root@desktop27 mnt]# mv answer.exp answer.sh
[root@desktop27 mnt]# cat answer.sh 
#!/bin/bash
/usr/bin/expect <<EOF
set timeout 2
spawn /mnt/ask.sh
expect { 
     name { send "$1\r";exp_continue }
     old { send "$2\r";exp_continue }
     study { send "$3\r";exp_continue }
     happy { send "$4\r" }
}
expect eof
EOF
[root@desktop27 mnt]# sh answer.sh tutu 18 linux happy
spawn /mnt/ask.sh
What's your name: tutu
How old are you: 18
Which obj you study: linux
Are you happy? happy
tutu is 18's old and study linux feel happy
[root@desktop27 mnt]#

6. exit, break, continue

Exit script exits with exit value n
break exits the current loop
continue terminates the command inside the loop ahead of time, but does not terminate the loop

[root@desktop27 mnt]# vim test.sh
[root@desktop27 mnt]# cat test.sh 
#!/bin/bash
for NUM in {1..5}
do
    while 
    [ "$NUM" -eq "4" ]
    do  
        continue 4 
    done
    echo $NUM
done
[root@desktop27 mnt]# sh test.sh 
1
2
3
5
[root@desktop27 mnt]#

Posted by portaloo on Tue, 11 Dec 2018 09:03:05 -0800