(五)shell编程之read,case,流程控制

使用read命令来接受输入

使用read来把输入值分配给一个或多个shell变量,read从标准输入中读取值,给每个单词分配一个变
量,所有剩余单词都被分配给最后一个变量,如果变量名没有指定,默认标准输入的值赋值给系统内置
变量REPLY
格式:

read [options] [name ...]

常见选项:

-p 指定要显示的提示
-s 静默输入,一般用于密码
-n N 指定输入的字符长度N
-d '字符' 输入结束符
-t N TIMEOUT为N秒

范例:

[root@sz-kx-centos8 ~]# read
longxuan
[root@sz-kx-centos8 ~]# echo $REPLY
longxuan

[root@sz-kx-centos8 ~]# read NAME TITLE
long ceo
[root@sz-kx-centos8 ~]# echo $NAME
long
[root@sz-kx-centos8 ~]# echo $TITLE
ceo

[root@sz-kx-centos8 ~]# read -p "Please input your name: " NAME
Please input your name: longwang
[root@sz-kx-centos8 ~]# echo $NAME
longwang

范例:

[root@sz-kx-centos8 ~]# read x y z <<< "I love you"
[root@sz-kx-centos8 ~]# echo $x
I
[root@sz-kx-centos8 ~]# echo $y
love
[root@sz-kx-centos8 ~]# echo $z
you

范例:面试题 read和输入重定向

[root@centos8 scripts]# cat test.txt
1 2
[root@centos8 scripts]# read i j < test.txt ; echo i=$i j=$j
i=1 j=2
[root@centos8 scripts]# echo 1 2 | read x y ; echo x=$x y=$y
x= y=
[root@centos8 ~]# echo 1 2 | ( read x y ; echo x=$x y=$y )
x=1 y=2
[root@centos8 ~]# echo 1 2 | { read x y ; echo x=$x y=$y; }
x=1 y=2
[root@centos8 ~]# man bash

bash shell 的配置文件

bash shell的配置文件很多,可以分成下面类别

全局配置:针对所有用户皆有效

/etc/profile
/etc/profile.d/*.sh
/etc/bashrc

个人配置:只针对特定用户有效

~/.bash_profile
~/.bashrc

交互式登录

配置文件生效和执行顺序:

#放在每个文件最前
/etc/profile
/etc/profile.d/*. sh
/etc/bashrc
~/ .bash_ profile
~/ .bashrc
/etc/bashrc

#放在每个文件最后
/etc/profile.d/*.sh
/etc/bashrc
/etc/profile
/etc/bashrc #此文件执行两次
~/.bashrc
~/.bash_profile

注意:文件之间的调用关系,写在同一个文件的不同位置,将影响文件的执行顺序

非交互式登录

/etc/profile.d/*.sh
/etc/bashrc
~/.bashrc

按功能划分分类

Profile类

全局:/etc/profile, /etc/profile.d/*.sh
个人:~/.bash_profile

功用:
(1) 用于定义环境变量
(2) 运行命令或脚本

Bashrc类

全局:/etc/bashrc
个人:~/.bashrc

功用:
(1) 定义命令别名和函数
(2) 定义本地变量

编辑配置文件生效

修改profile和bashrc文件后需生效两种方法:

  1. 重新启动shell进程
  2. source|. 配置文件

注意:source 会在当前shell中执行脚本,所有一般只用于执行置文件,或在脚本中调用另一个脚本的场景
范例:

. ~/.bashrc

流程控制

选择执行 if 语句

格式:

if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi

单分支

if 判断条件;then
   条件为真的分支代码
fi

双分支

if 判断条件; then
   条件为真的分支代码
else
   条件为假的分支代码
fi

多分支

if 判断条件1; then
   条件1为真的分支代码
elif 判断条件2; then
   条件2为真的分支代码
elif 判断条件3; then
   条件3为真的分支代码
...
else
   以上条件都为假的分支代码
fi

说明:
多个条件时,逐个条件进行判断,第一次遇为“真”条件时,执行其分支,而后结束整个if语句

范例:

#根据命令的退出状态来执行命令
if ping -c1 -W2 station1 &> /dev/null; then
   echo 'station1 is UP'
elif grep -q 'station1' ~/maintenance.txt; then
   echo 'station1 is undergoing maintenance'
else
   echo 'station1 is unexpectedly DOWN!'
   exit 1
fi

条件判断 case 语句

格式:

case WORD in [PATTERN [| PATTERN]...) COMMANDS ;;]... esac

范例:

case 变量引用 in
PAT1)
   分支1
   ;;
PAT2)
   分支2
   ;;
...
*)
   默认分支
   ;;
esac

case支持glob风格的通配符:

* 任意长度任意字符
? 任意单个字符
[] 指定范围内的任意单个字符
| 或者,如: a|b

范例:

#!/bin/bash
# 
#********************************************************************
#Author:        xuanlv
#QQ:            360956175
#Date:          2021-06-10
#FileName:     case_yesorno.sh
#URL:           https://www.cnblogs.com/xuanlv-0413/
#Description:      The test script
#Copyright (C):     2021 All rights reserved
#********************************************************************
read -p "Do you agree(yes/no)?" INPUT
INPUT=`echo $INPUT | tr 'A-Z' 'a-z'`

case $INPUT in
y|yes)
    echo "you input is YES"
    ;;
n|no)
    echo "you input is NO"
    ;;
*)
    echo "Input fales,please input yes or no!"
esac

范例:


#!/bin/bash
# 
#********************************************************************
#Author:        xuanlv
#QQ:            360956175
#Date:          2021-06-10
#FileName:     case_yesorno.sh
#URL:           https://www.cnblogs.com/xuanlv-0413/
#Description:      The test script
#Copyright (C):     2021 All rights reserved
#********************************************************************
read -p "Do you agree(yes/no)?" INPUT
INPUT=`echo $INPUT | tr 'A-Z' 'a-z'`

case $INPUT in
[yY]|[yY][eE][sS])
    echo "you input is YES"
    ;;
[nN]|[Nn][oO])                                                                                                       
    echo "you input is NO"
    ;;
*)
    echo "Input fales,please input yes or no!"
esac
posted @ 2021-06-13 22:32  空白的旋律  阅读(291)  评论(0编辑  收藏  举报