shell script 9: application of case structure conditional sentence

Posted by Dethman on Wed, 29 Apr 2020 18:14:47 +0200

9, Application of case structure conditional sentence

(1) case syntax structure

case structure condition sentence is equivalent to multi branch if condition sentence, but it looks more standard and neat than these condition sentences, and is often used in application scenarios such as system service script.

Syntax structure of case statement:

case "variable" in
      Value 1)
          Instruction 1
          ;;
       Value 2)
          Instruction 2
          ;;
       Value 3)
          Instruction 3
          ;;
        *)
          Instruction 4
 esac

(2) Example, case application:

[root@centos6-kvm3 scripts]# cat 09-03.sh
#!/bin/bash
cat <<EOF
1.install lnmp
2.install lamp
3.exit
EOF
read -p "Please enter a number{1|2|3}: " num
expr $num + 2 &>/dev/null
if [ $? -ne 0 ]
then
  echo "usage: $0{1|2|3}"
  exit 1
fi
case $num in
     1)
     echo "install lnmp"
     ;;
     2)
     echo "install lamp"
     ;;
     3)
     echo "exit"
     exit
     ;;
     *)
     echo "usage:$0{1|2|3}"
     exit 1
esac

(3) For example, different branch font colors are different:

When the user input the corresponding number to select the fruit, tell him what the fruit is, and add a color (at will) to the fruit word, requiring the use of case statements.

The color of the content is represented by a number in the range of 30-37,Each number represents a color.
echo -e "\033[30m Black lettering oldboy trainning \033[0m" #< = = 30m means black words.
echo -e "\033[31m Red character oldboy trainning \033[0m" #< = = 31m means red words.
echo -e "\033[32m Green word oldboy trainning \033[0m" #< = = 32m for green words.
echo -e "\033[33m Brown word oldboy trainning \033[0m" #< = = 33m means brown, similar to yellow.
echo -e "\033[34m Blue words oldboy trainning \033[0m" #< = = 34m means blue words.
echo -e "\033[35m Scarlet Letter oldboy trainning \033[0m" #< = = 35m indicates magenta, which is similar to purple.
echo -e "\033[36m Turquoise oldboy trainning \033[0m" #< = = 36m indicates cyan, which is similar to light blue.
echo -e "\033[37m White words oldboy trainning \033[0m" #< = = 37m for white words.

1. Basic script 1:

[root@centos6-kvm3 scripts]# cat 09-04.sh
#!/bin/bash
cat <<EOF
1.apple
2.pear
3.banana
4.cherry
EOF
read -p "Please enter a number{1|2|3|4}: " num
expr $num + 2 &>/dev/null
if [ $? -ne 0 ]
then
   echo "usage:$0 {1|2|3|4}"
   exit 1
fi
case $num in
   1)
   echo -e "\033[31m apple \033[0m"
   ;;
   2)
   echo -e "\033[32m pear \033[0m"
   ;;
   3)
   echo -e "\033[33m banana \033[0m"
   ;;
   4)
   echo -e "\033[34m cherry \033[0m"
   ;;
   *)
   echo "usage:$0 {1|2|3|4}"
   exit
esac
[root@centos6-kvm3 scripts]# 

2. Advanced script 2 (colored):

Color function:
[root@centos6-kvm3 scripts]# cat color.sh
#!/bin/bash
red="\033[31m"
green="\033[32m"
yellow="\033[33m"
blue="\033[34m"
tail="\033[0m"
color(){
case $1 in
     red)
     echo -e "${red}$2${tail}"
     ;;
     green)
     echo -e "${green}$2${tail}"
     ;;
     yellow)
     echo -e "${yellow}$2${tail}"
     ;;
     blue)
     echo -e "${blue}$2${tail}"
     ;;
     *)
     echo "usage:$0 please input right content"
esac

}
color $*
[root@centos6-kvm3 scripts]# 
//Function call color function:
[root@centos6-kvm3 scripts]# cat  09-04.sh 
#!/bin/bash
. ./color.sh
cat <<EOF
1.apple
2.pear
3.banana
4.cherry
EOF
read -p "Please enter a number{1|2|3|4}: " num
expr $num + 2 &>/dev/null
if [ $? -ne 0 ]
then
   echo "usage:$0 {1|2|3|4}"
   exit 1
fi
case $num in
   1)
   color red apple
   ;;
   2)
   color green pear
   ;;
   3)
   color yellow banana
   ;;
   4)
   color blue cheryy
   ;;
   *)
   echo "usage:$0 {1|2|3|4}"
   exit
esac
[root@centos6-kvm3 scripts]# 

Font background color

The background color of a word corresponds to a number range of 40-47,The code is as follows.
echo -e "\033[40;37m White characters on black background oldboy\033[0m"   #< = = 40m indicates a black background.
echo -e "\033[41;37m White characters on red background oldboy\033[0m"   #< = = 41m indicates a red background.
echo -e "\033[42;37m White characters on green background oldboy\033[0m"   #< = = 42m indicates a green background.
echo -e "\033[43;37m White characters on brown background oldboy\033[0m"   #< = = 43M indicates brown background, which is similar to yellow background.
echo -e "\033[44;37m White on blue oldboy\033[0m"   #< = = 44m for a blue background.
echo -e "\033[45;37m White characters on magenta background oldboy\033[0m"  #< = = 45m indicates magenta background, which is similar to purple background.
echo -e "\033[46;37m White characters on blue-green background oldboy\033[0m"   #< = = 46m indicates cyan background, which is similar to light blue background.
echo -e "\033[47;30m Black characters on white background oldboy\033[0m"    #< = = 47m for a white background.

(4) rsync start basic script instance:

[root@centos6-kvm3 scripts]# cat rsync.sh
#!/bin/bash
case $1 in
     start)
     rsync --daemon
     if [ $? -eq 0 ]
     then
        echo "rsync $1 ok"
     else
        echo "rsync $1 fail"
     fi
     ;;
     stop)
     killall rsync
     if [ $? -eq 0 ]
     then
        echo "rsync $1 ok"
     else
        echo "rsync $1 fail"
     fi
     ;;
     restart)
     killall rsync && sleep 1 && rsync --daemon
     if [ $? -eq 0 ]
     then
        echo "rsync $1 ok"
     else
        echo "rsync $1 fail"
     fi
     ;;
     *)
     echo "usage:$0 {start|stop|restart}"
esac

View process: lsof -i:873

rsync start advanced script:

cp rsyncd.sh /etc/init.d/rsyncd

chkconfig --list rsyncd

chkconfig --add rsyncd

chmod +x /etc/init.d/rsyncd

(5) The high level of rsync startup script

[root@centos6-kvm3 scripts]# cat rsyncd.sh 
# chkconfig: 2345 20 80
# description: rsync start stop
#!/bin/bash
. /etc/init.d/functions
start(){
    rsync --daemon
    retval=$?
    if [ $retval -eq 0 ]
     then
        action  "rsync start ok" /bin/true
        return $retval
    else
        action "rsync start fail" /bin/false
        return $retval
    fi
}
stop(){
     killall rsync &>/dev/null
     retval=$?
     if [ $retval -eq 0 ]
     then
        action "rsync stop ok" /bin/true
        return $retval
     else
        action "rsync stop fail" /bin/false
        return $retval
     fi
}
case $1 in
     start)
     start
     retval=$?
     ;;
     stop)
     stop
     retval=$?
     ;;
     restart)
     stop 
     sleep 2
     start
     retval=$?
     ;;
     *)
     echo "usage:$0 {start|stop|restart}"
esac
exit $retval

Topics: Linux rsync lsof