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\n
abn
[root@tlinux shell]# echo -e "ab\n"
ab

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

 

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

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

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

\33[y;xH  设置光标位置

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

echo  -e  "\033[31mthis is a test"

echo -e     "\033[10;5H\033[31;46mthis is test"     同时设置前景色与背景色   :前景色红色 背景色青色

echo -e  "\033[0m"    取消设置

 

posted on 2019-05-05 19:50  wsw_seu  阅读(476)  评论(0编辑  收藏  举报

导航