zoukankan      html  css  js  c++  java
  • linux系统中 read命令

    1、linux系统中read命令用于从标准输入中读取数据,进而给变量赋值。

     简单用法:

    [root@linuxprobe test]# echo $var1  ## 首先查看未赋值前变量,发现是空值
    
    [root@linuxprobe test]# read var1  ## read命令直接跟想要赋值的变量名称,就可以给变量赋值了(直接通过键盘输入)
    001
    [root@linuxprobe test]# echo $var1  ##查看通过键盘输入001后变量的值,发现变量var1已经是001.
    001

    2、直接使用read,不加变量名称,变量赋值给REPLY。

    [root@linuxprobe test]# read  ##直接使用read命令,不加变量名称
    002
    [root@linuxprobe test]# echo $REPLY ## linux默认将变量命名为REPLY
    002

    3、通过-p参数加入提示语

    [root@linuxprobe test]# read -p "please input an number:" var1 ## -p参数在输入变量值前起到提示的作用
    please input an number:100
    [root@linuxprobe test]# echo $var1
    100
    [root@linuxprobe test]# read -p "请输入变量值:" var2 ##同上
    请输入变量值:xxxyyyzzz
    [root@linuxprobe test]# echo $var2
    xxxyyyzzz

    4、通过-t参数限制输入变量值的时间

    [root@linuxprobe test]# time read -t 3 var1  ## 通过-t设置输入时间上限3秒,如果3秒内未输入,变量为空值
    
    real    0m3.001s
    user    0m0.000s
    sys     0m0.000s
    [root@linuxprobe test]# echo $var1
    
    [root@linuxprobe test]# time read -t 3 var1  ## 3秒内输入变量,可给变量赋值
    11
    
    real    0m1.480s
    user    0m0.000s
    sys     0m0.000s
    [root@linuxprobe test]# echo $var1
    11

    5、通过-s 选项隐藏输入的变量值,比如在屏幕输入密码

    [root@linuxprobe test]# echo $PASSWD
    
    [root@linuxprobe test]# read -s -p "please input your passwd:" PASSWD  ## 通过-s参数,可以在给变量PASSWD赋值的时候不在屏幕上显示
    please input your passwd:[root@linuxprobe test]#
    [root@linuxprobe test]# echo $PASSWD
    123456

    6、通过-n参数实现限定输入变量的长度

    [root@linuxprobe test]# read -n 1 var1 ##限定变量长度为1
    u[root@linuxprobe test]#
    [root@linuxprobe test]# echo $var1
    u
    [root@linuxprobe test]# read -n 5 var1 ## 限定变量长度为5,可以输入少于和等于5的变量值,但不能大于5
    123
    [root@linuxprobe test]# echo $var1
    123
    [root@linuxprobe test]# read -n 5 var1
    12345[root@linuxprobe test]#
    [root@linuxprobe test]# echo $var1
    12345

    7、使用-r参数限制为原始字符串

    [root@linuxprobe test]# read var1
    abc\defg\hi
    [root@linuxprobe test]# echo $var1
    abcdefghi
    [root@linuxprobe test]# read -r var1  ## 加-r参数实现不对\进行转义
    abc\defg\hi
    [root@linuxprobe test]# echo $var1
    abc\defg\hi

    8、从文件中读取变量值

    [root@linuxprobe test]# seq -f test%g 5 > a.txt ##测试文件
    [root@linuxprobe test]# cat a.txt
    test1
    test2
    test3
    test4
    test5
    [root@linuxprobe test]# ls
    a.txt
    [root@linuxprobe test]# cat a.txt | while read i;do mkdir $i;done  ##读取a.txt的每一行作为变量,以变量名创建目录
    [root@linuxprobe test]# ls
    a.txt  test1  test2  test3  test4  test5
  • 相关阅读:
    做一个终端发送和协调器接收实验
    Tensorflow快餐教程(1)
    tensorflow 镜像
    Python3 import tensorflow 出现FutureWarning: Passing (type, 1) or '1type' 问题
    在windows 10 64位系统下安装TensorFlow
    AttributeError: module 'tensorflow' has no attribute 'Session'
    I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
    解决vue多次提交
    解决pip安装时速度慢的问题
    mysql mariadb的VC客户端遇到的问题
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/13810344.html
Copyright © 2011-2022 走看看