1. 程式人生 > >shell編程之輸入輸出

shell編程之輸入輸出

轉義 賦值 tex 等待 code put echo -e -a 字符串

1、輸入 read命令有以下幾種常見形式:

read var :等待用戶輸入,從標準輸入中讀取一行並賦值給變量var

read : 標準輸入讀取一行,並賦值給內置變量REPLY

read -a arr :讀入一組詞,依次賦值給數組arr

read -p :表示提示符

read -t:表示超時時間

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

2、輸出命令echo

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

輸出一個變量:echo $num 或 echo ${num}

echo -n "hello world" : -n 表示輸出之後 不會換行

echo -e :表示可以使用轉義字符

註意一下,使用轉義字符的時候,字符串要加雙引號,不加引號不起作用。

[[email protected] shell]# echo -e ab\n
abn
[[email protected] shell]# echo -e "ab\n"
ab

[[email protected] shell]# echo \t shell會解析\t 再傳給echo t [[email protected] shell]# echo
"\t" shell直接傳給echo \t [[email protected] 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"    取消設置

shell編程之輸入輸出