5-31shell编程实战入门

case结构条件句的语法格式为:

case "变量" in

    值1)

        指令1...

        ;;

    值2)

        指令2...

        ;;

    *)

        指令3...

esac

相当于if多分支语句。

if [ "变量"  ="值1"  ]

then

    指令1...

elif  [ "变量"  ="值2"  ]

then

    指令2...

else

    指令3...

fi

将下面语句改成case实现。

[root@web01 /server/scripts]# cat like_meinv_fi.sh

cat <<END

  1.panxiaoting

  2.gongli

  3.fanbinbing

END

read -p "Which do you like?Pls input the num:" a

if [ "$a" = "1" ]

then

    echo "I guess,you like panxiaoting"  #<==根据用户选择的结果,回应应用输出。

    exit 0          #<==退出脚本,不在向下执行。

elif [ "$a" = "2" ]

then

    echo "I guess,you like gongli"

    exit 0          #<==退出脚本,不在向下执行。

elif [ "$a" = "3" ]

then

    echo "I guess,you like fangbingbing"

    exit 0          #<==退出脚本,不在向下执行。

else

    echo "I guess,you are not man."

fi

河南  贾顺平

#!/bin/bash

function menu () {

    cat << END

    1.panxiaoting

    2.gongli

    3.fanbinbing

END

}

function chose () {

read -p "Which do you like?Pls input the num:" number

case "$number" in

    1)

        echo "I guess,you like panxiaoting"

        ;;

    2)

        echo "I guess,you like gongli"

        ;;

    3)

        echo "I guess,you like fangbingbing"

        ;;

    *)

        echo "请输入正确的选项"

        exit 1

esac

}

function main () {

    menu

    chose

}

main

焦向博 河南南阳

cat <<END

  1.panxiaoting

  2.gongli

  3.fanbinbing

END

read -p "Which do you like?Pls input the num:" a

case $a in

    1)

    echo "I guess,you like panxiaoting"

    ;;

    2)

    echo "I guess,you like gongli"

    ;;   

    3)

    echo "I guess,you like fangbingbing"

    ;;   

    *)

    echo "I guess,you are not man."

esac

谢堂圣 重庆 2019/5/31 8:50:10

#!/bin/bash

cat<<EOF

欢迎来到老男孩教育!现有以下专业选择。

1. Linux云计算运维(老男孩王牌课程)

2. Python全栈开发

3. 新媒体运营

EOF

read -p "请选择你喜欢的专业:" a

case "$a" in

  1)

    echo "你选择的专业是:${a}. Linux云计算运维(老男孩王牌课程)"

    ;;

  2)

    echo "你选择的专业是:${a}. Python全栈开发"

    ;;

  3)

    echo "你选择的专业是:${a}. 新媒体运营"

    ;;

  *)

    echo "对不起,目前只有3个专业供你选择!"

    exit;

esac         

Linux命令行给字体加颜色命令为:

[root@oldboy scripts]# echo -e "\E[1;31m红色字oldboy\E[0m"

红色字oldboy

[root@oldboy scripts]# echo -e "\033[31m红色字oldboy \033[0m"

红色字oldboy

在上述命令中:

 echo -e可以识别转义字符,这里将识别特殊字符的含义,并输出。

 \E也可以使用\033替代。

 [1数字1表示加粗显示(这个位置可以加不同的数字代表不同的意思,详细信息可man console_codes获得)。

 31m表示为红色字体,这个位置可以换不同的数字,以代表不同的意思。

 “红色字oldboy”表示待设置的内容。

 [0m表示关闭所有属性,这个位置可以换不同的数字,以代表不同的意思。

[root@web01 /server/scripts]# cat plus_color.sh

#!/bin/bash

##############################################################

# File Name: plus_color.sh

# Version: V1.0

# Author: oldboy

# Organization: www.oldboyedu.com

##############################################################

#!/bin/sh

RED_COLOR='\E[1;31m'

GREEN_COLOR='\E[1;32m'

YELLOW_COLOR='\E[1;33m'

BLUE_COLOR='\E[1;34m'

RES='\E[0m'

echo -e "${RED_COLOR}oldboy$RES"  #<==变量中间就是待加颜色的字符串。

echo -e "${YELLOW_COLOR}oldgirl$RES"

[root@web01 /server/scripts]# cat 9_2_1.sh

#!/bin/bash

##############################################################

# File Name: 9_2_1.sh

# Version: V1.0

# Author: oldboy

# Organization: www.oldboyedu.com

##############################################################

RED_COLOR='\E[1;31m'

GREEN_COLOR='\E[1;32m'

YELLOW_COLOR='\E[1;33m'

BLUE_COLOR='\E[1;34m'

RES='\E[0m'

echo '=====================

1.apple

2.pear

3.banana

4.cherry

====================='

read -p "pls select a num:" num

case "$num" in

    1)                         

        echo -e "${RED_COLOR}apple${RES}"

        ;;

    2)

        echo -e "${GREEN_COLOR}pear${RES}"

        ;;

    3)

        echo -e "${YELLOW_COLOR}banana${RES}"

        ;;

    4)

        echo -e "${BLUE_COLOR}cherry${RES}"

        ;;

    *)

        echo "muse be {1|2|3|4}"

esac

[root@web01 /server/scripts]# cat 9_2_3.sh

#!/bin/bash

##############################################################

# File Name: 9_2_3.sh

# Version: V1.0

# Author: oldboy

# Organization: www.oldboyedu.com

##############################################################

RED_COLOR='\E[1;31m'

GREEN_COLOR='\E[1;32m'

YELLOW_COLOR='\E[1;33m'

BLUE_COLOR='\E[1;34m'

RES='\E[0m'

function usage(){

    echo "must be {1|2|3}"

    exit 1

}

function menu(){

    cat <<END

    1.apple

    2.pear

    3.banana

END

}

function chose(){

read -p "pls input your choice:" fruit

case "$fruit" in

    1)

        echo -e "${RED_COLOR}apple${RES}"

        ;;

    2)

        echo -e "${GREEN_COLOR}pear${RES}"

        ;;

    3)

        echo -e "${YELLOW_COLOR}banana${RES}"

        ;;

    *)

        usage

esac

}

function main(){

    menu

    chose

}

main

function AddColor(){

RED_COLOR='\E[1;31m'

GREEN_COLOR='\E[1;32m'

YELLOW_COLOR='\E[1;33m'

BLUE_COLOR='\E[1;34m'

PINK='\E[1;35m'

RES='\E[0m'

if [ $# -ne 2 ];then

    echo "Usage $0 content {red|yellow|blue|green}"

    exit

fi

case "$2" in

    red|RED)

        echo -e  "${RED_COLOR}$1${RES}"

        ;;

    yellow|YELLOW)

        echo -e  "${YELLOW_COLOR}$1${RES}"

        ;;

    green|GREEN)

        echo -e  "${GREEN_COLOR}$1${RES}"

        ;;

    blue|BLUE)

        echo -e  "${BLUE_COLOR}$1${RES}"

        ;;

    pink|PINK)

        echo -e  "${PINK_COLOR}$1${RES}"

        ;;

    *)

        echo "Usage $0 content {red|yellow|blue|green}"

        exit

esac

}

function main(){

    AddColor $1 $2 

}

main $*

[root@web01 /etc/init.d]# cat rsyncd2

#!/bin/bash

# chkconfig: 2345 21 81

# description: startup rsync scripts

PID=/var/run/rsyncd.pid

start(){

if [ -f $PID -a -s $PID ]

  then

      :

  else

      rsync --daemon

fi

    return $?

}

stop(){

    if [ -f $PID -a -s $PID ]

    then

        kill `cat $PID`

    fi

    return $?

}

case "$1" in

    start)

        start

        retval=$?

        ;;

    stop)

        stop

        retval=$?

        ;;

    restart)

        stop

        sleep 2

        start

        retval=$?

        ;;

    *)

        echo "Usage;$0 {start|stop|restart}"

esac

exit $retval

最专业脚本

https://blog.51cto.com/oldboy/2155931

范例9_7:实现通过传参的方式往/etc/openvpn_authfile.conf里添加用户,具体要求如下。

1)命令用法为:

USAGE: sh adduser {-add|-del|-search} username

2)传参要求为:

如果参数为-add时,表示添加后面接的用户名。

如果参数为-del时,表示删除后面接的用户名。

如果参数为-search时,表示查找后面接的用户名。

3)如果有同名的用户则不能添加,没有对应用户则无需删除,查找到用户以及没有用户时给出明确提示。

4)/etc/openvpn_authfile.conf不能被所有外部用户直接删除及修改

范例9_8:已知Nginx Web服务的管理命令如下——

启动服务命令为/application/nginx/sbin/nginx

停止服务命令为/application/nginx/sbin/nginx -s stop

请用case语句开发脚本实现Nginx服务启动及关闭功能,具体脚本命令为/etc/init.d/nginx {start|stop|restart},并实现可通过chkconfig进行开机自启动管理(CentOS6),

同时实现通过systemctl进行开机自启动管理(CentOS7)。

[root@web01 /server/scripts]# cat while1.sh

#!/bin/bash

##############################################################

# File Name: while1.sh

# Version: V1.0

# Author: oldboy

# Organization: www.oldboyedu.com

##############################################################

while true

do

    uptime

    usleep 1000000

done

[root@web01 /server/scripts]# cat like_meinv_case_while.sh

while true

do

    sleep 2

    clear

    cat <<END

      ==============================

      1.panxiaoting

      2.gongli

      3.fanbinbing

      ==============================

END

    read -p "Which do you like?Pls input the num:" a

    if [ "$a" = "1" ]

    then

        echo "I guess,you like panxiaoting"  #<==根据用户选择的结果,回应应用输出。

    elif [ "$a" = "2" ]

    then

        echo "I guess,you like gongli"

    elif [ "$a" = "3" ]

    then

        echo "I guess,you like fangbingbing"

    else

        echo "I guess,you are not man."

    fi

done

[root@web01 /server/scripts]# cat check_url_while.sh

#!/bin/sh

[ -f /etc/init.d/functions ] && . /etc/init.d/functions

usage(){

    if [[ ! $1 =~ http://www.*com ]]

    then

        echo "Usage:$0 http://www.xx.com"

        exit 1

    fi

}

check_url(){

wget -q $1 &>/dev/null

retval=$?

if [ $retval -eq 0 ]

then

    action  "url is ok." /bin/true

else

    action "url is no." /bin/false

fi

}

main(){

    while true

    do

        usage $1

        check_url $1

        sleep 5

    done

}

main $*

Linux系统计算从1加到100之和

http://oldboy.blog.51cto.com/2561410/767862

作业:

范例10_4:猜数字游戏。首先让系统随机生成一个数字,给这个数字定一个范围(1-60),让用户输入猜的数字,对输入进行判断,如果不符合要求,就给予高或低的提示,猜对后则给出猜对用的次数,请用while语句实现。

超越了全国多少用户。

范例10_5:手机充值10元,每发一次短信(输出当前余额)花费1角5分钱,当余额低于1角5分钱时不能发短信,提示“余额不足,请充值”(允许用户充值后继续发短信),请用while语句实现。

范例10_6:使用while守护进程方式监控网站,每隔10秒确定网站是否正常。

方式1:采用exec读取文件后,然后进入while循环处理。

exec<FILE

sum=0

while read line

do

    cmd

done

方式2:使用cat读取文件内容,然后通过管道进入while循环处理。

cat FILE_PATH|while read line

do

    cmd

done

方式3:在while循环结尾done通过输入重定向指定读取的文件。

while read line

do

    cmd

done<FILE

[root@web01 /server/scripts]# cat while2.sh

#!/bin/bash

##############################################################

# File Name: while2.sh

# Version: V1.0

# Author: oldboy

# Organization: www.oldboyedu.com

##############################################################

sum=0

while read line

do

    i=`echo $line|awk '{print $NF}'`

    let sum=sum+i

done<./stu.txt

echo $sum

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容