• 随堂练习 shell脚本(三)


    使用 read 命令来接受输入

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

    格式:

    read [options] [name ...]

    常规选项

    -p  指定要显示的提示

    -s  静默输入,一般用于密码

    -n N  指定输入的字符长度N

    -d ‘字符’   输入结束符

    -t N  TIMEOUT 为 N 秒

    范例

    [root@centos8 ~]# read
    longwang
    [root@centos8 ~]# echo $REPLY
    longwang
    [root@centos8 ~]# read NAME TITLE
    wang cto
    [root@centos8 ~]# echo $NAME
    wang
    [root@centos8 ~]# echo $TITLE
    cto
    [root@centos8 ~]# read -p "Please input your name: " NAME
    Please input your name: wang
    [root@centos8 ~]# echo $NAME
    wang

    范例

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

    范例

    #Pipelines:A pipeline is a sequence of one or more commands separated by one of the control operators | or |&
    [root@centos8 ~]# echo long | read NAME
    [root@centos8 ~]# echo $NAME
    [root@centos8 ~]# echo long | { read NAME; echo $NAME; }
    long

    范例:面试题 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

    范例

    [root@centos8 scripts]# cat read.sh
    #!/bin/bash
    read -p "Are you rich?yes or no: " ANSWER
    [[ $ANSWER =~ ^[Yy]|[Yy][Ee][Ss]$ ]] && echo "You are rich" || echo "Good Good Study,Day Day Up!"

    范例

    read -p “Enter a filename: “ FILE

    范例:鸡兔同笼算法,今有雉兔同笼,上有三十五头,下有九十四足,问雉兔各几何?

    [root@centos8 ~]# cat /data/script40/chook_rabbit.sh
    #!/bin/bash
    read -p "请输入头的数量: " HEAD

    read -p "请输入脚的数量: " FOOT

    RABBIT=$[FOOT/2-HEAD]

    CHOOK=$[HEAD-RABBIT]

    echo "兔子: " $RABBIT

    echo "鸡: " $CHOOK

    范例:实现运维工作菜单

    [root@centos8 scripts]# cat work_menu.sh

    #!/bin/bash

    source /etc/init.d/functions

    echo -en "E[$[RANDOM%7+31];1m"

    cat <<EOF

    请选择:

    1)备份数据库

    2)清理日志

    3)软件升级

    4)软件回滚

    5)删库跑路

    EOF

    echo -en "E[0m"

    read -p "请选择上面项对应的数字1-5: " MENU

    [ $MENU -eq 1 ] && ./backup.sh

    [ $MENU -eq 2 ] && actiop "清理日志"

    [ $MENU -eq 3 ] && actiop "软件升级"

    [ $MENU -eq 4 ] && actiop "软件回滚"

    [ $MENU -eq 5 ] && actiop "删库跑路"

    bash的配置文件

    按生效范围划分两类

    全局配置:

    /etc/profile

    /etc/profile.d/*.sh

    /etc/bashrc

    个人配置

    ~/.bash_profile

    ~/.bashrc

    shell登录两种方式分类

    交互式登录

      直接通过终端输入账号密码登录

      使用su - UserName 切换的用户

    配置文件执行顺序:

    /etc/profile.d/*.sh

    /etc/bashrc

    /etc/profile

    /etc/bashrc   #此文件执行两次

    .bashrc

    .bash_profile

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

    非交互式登录

      su UserName

      图形界面下打开的终端

      执行脚本

      任何其它的bash实例

    执行顺序:

    /etc/profile.d/*.sh

    /etc/bashrc

    .bashrc

    按功能划分分类

    profile 类和 bashrc 类

    Profile类

    profile类为交互式登录的shell提供配置

    全局:/etc/profile, /etc/profile.d/*.sh

    个人:~/.bash_profile

    功用:
    (1) 用于定义环境变量

    (2) 运行命令或脚本

    Bashrc类

    bashrc类:为非交互式和交互式登录的shell提供配置

    全局:/etc/bashrc

    个人:~/.bashrc

    功用:
    (1) 定义命令别名和函数

    (2) 定义本地变量

    编辑配置文件生效

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

    1. 重新启动 shell进程

    2. source|. 配置文件

    范例

    .   ~/.bashrc

    Bash 退出任务

    保存在~/.bash_logout文件中(用户),在退出登录shell时运行功能:

    创建自动备份

    清除临时文件

    练习
    1、让所有用户的PATH环境变量的值多出一个路径,例如:/usr/local/apache/bin

    2、用户 root 登录时,将命令指示符变成红色,并自动启用如下别名: rm=‘rm -i’
    cdnet=‘cd /etc/sysconfig/network-scripts/’
    editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-eth0’
    editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-eno16777736 或 ifcfg-ens33 ’ (如果系统是CentOS7)
    3、任意用户登录系统时,显示红色字体的警示提醒信息“Hi,dangerous!”

    4、编写生成脚本基本格式的脚本,包括作者,联系方式,版本,时间,描述等

  • 相关阅读:
    批量修改Linux文件夹下所有文件大小写
    如何使用malloc申请一个二位数组
    CenOS 配置C/C++语言
    C++类定义,.h文件与.cpp文件之间的关系以及条件编译
    Error while building/deploying project...When executing step "qmake"——Qt Creator快速排错
    Hello World!
    HDU1262:寻找素数对
    NOIP1998复赛:2的幂次方表示
    HDU1002 : A + B Problem II
    Open Judge2748:全排列
  • 原文地址:https://www.cnblogs.com/xuanlv-0413/p/13258053.html
Copyright © 2020-2023  润新知