26、脚本编程之while语句,read,echo命令

26.1 脚本编程结构简介

顺序结构

选择结构

if

case

循环结构

for

while

until

26.2 while循环

while循环:适用于循环次数未知或循环过多的场景,要有退出条件

语法:

while CONDITION; do

  statement

  ...

done

计算100以内所有正整数的和

#!/bin/bash

#

declare -i I=1

declare -i SUM=0

while [ $I -le 100 ];do

        let SUM+=I

        let I++

done

echo $SUM

26.3 用户交互(read)

read 读取用户输入(可一次读取多个输入分给不同的用户)

-p 显示提示

-t 指定超时时间

[root@localhost ~]# read a b c

how ae^H you

[root@localhost ~]# echo $a

how

[root@localhost ~]# echo $b

ae

[root@localhost ~]# echo $c

you

[root@localhost ~]# read a b c

how are

[root@localhost ~]# echo $a

how

[root@localhost ~]# echo $c

[root@localhost ~]# read a b c

how lo^H are you

[root@localhost ~]# read $a

abcd

[root@localhost ~]# read a b c

how old are you

[root@localhost ~]# echo $a

how

[root@localhost ~]# echo $c

are you

[root@localhost ~]# 

[root@localhost ~]# read -p "please enter your name:" -t 5 name

please enter your name:[root@localhost ~]# read -p "please enter your name:" -t 5 name

please enter your name:bob

[root@localhost ~]# echo $name

bob

[root@localhost ~]# 

26.4 bash脚本调试

bash

-n scripts.sh 检测脚本是否有错误

-x scripts.sh 分步执行脚本

练习:转换用户输入的字符为大写,除了quit:

[root@Daniel-R480 ~]# ./translate.sh 

Input something:ech

ECH

Input something:quit

[root@Daniel-R480 ~]# 

#!/bin/bash

#

read -p "Input something: " STRING

while [ $STRING != 'quit' ]; do

  echo $STRING | tr 'a-z' 'A-Z'

  read -p "Input something: " STRING

done

练习:每隔5秒查看hadoop用户是否登录,如果登录,显示其登录并退出;否则,显示当前时间,并说明hadoop尚未登录:

#!/bin/bash

#

who | grep "hadoop" &> /dev/null

RETVAL=$?

while [ $RETVAL -ne 0 ]; do

        echo "`date`,hadoop is not log."

        sleep 5

        who | grep "hadoop" &> /dev/null

        RETVAL=$?

done

echo "hadoop is logged in"

写一个脚本:

1) 显示一个菜单给用户:

d|D) show disk usages.

m|M) show memory usages.

s|S) show swap usages.

*) quit.

2) 当用户给定选项后显示相应的内容;

[root@Daniel-R480 ~]# ./showdisk.sh 

d|D) show disk usages.

m|M)show memory usages.

s|S)show swap usages.

*)quit

Your choice:s

Swap usage:

Swap:         30291           0       30291

[root@Daniel-R480 ~]# ./showdisk.sh 

d|D) show disk usages.

m|M)show memory usages.

s|S)show swap usages.

*)quit

Your choice:q

Unknown..

[root@Daniel-R480 ~]# echo $?

9

#!/bin/bash

#

cat << EOF

d|D) show disk usages.

m|M)show memory usages.

s|S)show swap usages.

*)quit

EOF

read -p "Your choice:" CHOICE

case $CHOICE in

d|D)

        echo "Disk usage:"

        df -Ph;;

m|M)

        echo "Memory usage:"

        free -m | grep "Mem";;

s|S)

        echo "Swap usage:"

        free -m | grep "Swap";;

*)

        echo "Unknown.."

        exit 9;;

esac

扩展:

当用户选择完成,显示相应信息后,不退出;而让用户再一次选择,再次显示相应内容;除了用户使用quit;

#!/bin/bash

#

cat << EOF

d|D) show disk usages.

m|M)show memory usages.

s|S)show swap usages.

*)quit

EOF

read -p "Your choice:" CHOICE

while [ $CHOICE != 'quit' ];do

{

case $CHOICE in

d|D)

        echo "Disk usage:"

        df -Ph;;

m|M)

        echo "Memory usage:"

        free -m | grep "Mem";;

s|S)

        echo "Swap usage:"

        free -m | grep "Swap";;

*)

        echo "Unknown.."

        exit 9;;

esac

read -p "Your choice:" CHOICE

echo输出不同格式的文本

[root@Daniel-R480 ~]# echo -e "\033[1mHello\033[0m,world."

Hello,world.

[root@Daniel-R480 ~]# echo -e "\033[31mHello\033[0m,world."

Hello,world.

[root@Daniel-R480 ~]# echo -e "\033[32mHello\033[0m,world."

Hello,world.

[root@Daniel-R480 ~]# echo -e "\033[41mHello\033[0m,world."

Hello,world.

[root@Daniel-R480 ~]# echo -e "\033[42mHello\033[0m,world."

Hello,world.

[root@Daniel-R480 ~]# echo -e "\033[1,31,43mHello\033[0m,world."

Hello,world.

[root@Daniel-R480 ~]# echo -e "\033[1;31;43mHello\033[0m,world."

Hello,world.

[root@Daniel-R480 ~]#

评论关闭。