zoukankan      html  css  js  c++  java
  • shell编程之输入输出

    1、输入 read命令有以下几种常见形式:

    read  var  :等待用户输入,从标准输入中读取一行并赋值给变量var

    read : 标准输入读取一行,并赋值给内置变量REPLY

    read -a arr :读入一组词,依次赋值给数组arr

    read -p :表示提示符

    read -t:表示超时时间

    read -n: 指定输入的字符长度最大值

    read -s : 可以不显示用户的输入

    [root@tlinux shell]# read var 
    abc
    [root@tlinux shell]# echo $var
    abc
    [root@tlinux shell]# read
    abc 
    [root@tlinux shell]# echo $REPLY
    abc
    [root@tlinux shell]# read -a arr
    math english chinese
    [root@tlinux shell]# echo ${arr[*]}
    math english chinese
    [root@tlinux shell]# read -p "please input 3 digits: " -t 10 -a arr
    please input 3 digits: 1 2 3
    [root@tlinux shell]# echo ${arr[*]}
    1 2 3

    2、输出命令echo

    输出一行文本:echo "hello world"

    输出一个变量:echo $num 或 echo ${num}

    echo -n "hello world"  : -n  表示输出之后 不会换行

    echo -e  :表示可以使用转义字符 

    注意一下,使用转义字符的时候,字符串要加双引号,不加引号不起作用。

    [root@tlinux shell]# echo -e ab
    
    abn
    [root@tlinux shell]# echo -e "ab "
    ab

    [root@tlinux shell]# echo shell会解析 再传给echo t [root@tlinux shell]# echo
    " " shell直接传给echo [root@tlinux shell]# echo -e " "A A

    3、echo输出颜色与光标定位  : 33或33

    33[30m -- 33[37m 设置前景色       如果不行的话   使用 33

    33[40m -- 33[47m 设置背景色

    33[y;xH  设置光标位置

    0:黑色           1:深红色             2:绿色          3:黄色             4:蓝色            5:紫色           6:青色        7:白色

    echo  -e  "33[31mthis is a test"
    
    echo -e     "33[10;5H33[31;46mthis is test"     同时设置前景色与背景色   :前景色红色 背景色青色
    
    echo -e  "33[0m"    取消设置
  • 相关阅读:
    LeetCode两数之和
    Windows端口被占用解决方法
    Vue函数防抖和函数节流
    Elasticsearch入门
    BIO/NIO/AIO对比
    Java日期格式转换不用发愁
    Java 类型转换工具类(持续更新)
    c++ regex 正则类例子及其gcc4.8报错
    c/c++ 编译错误汇总
    Android recover 修改更新字符串显示
  • 原文地址:https://www.cnblogs.com/wsw-seu/p/10815825.html
Copyright © 2011-2022 走看看