Shell 的 echo 指令與 PHP 的 echo 指令類似,都是用於字符串的輸出。命令格式:
1.顯示普通字符串:
echo "It is a test"
這里的雙引號完全可以省略,以下命令與上面實例效果一致:
echo It is a test
2.read的使用
- -p 輸入提示文字
- -n 輸入字符長度限制(達到6位,自動結束)
- -t 輸入限時
- -s 隱藏輸入內容
測試文件 test.sh 代碼如下:
read firstStr secondStr
echo "第一個參數:$firstStr; 第二個參數:$secondStr"
執行測試:
$ sh test.sh 一 二 三 四 第一個參數:一; 第二個參數:二 三 四
3.顯示結果寫入至文件
echo "It is a test" >> myfile
#!/bin/bash echo ----------------Example for echo and printf useing ---------------------------- echo `date` read -p "pls enter name & passwod: " -n 50 -t 10 name password echo `date` #把輸入的數據保存到logs.txt中 echo -e "your name:$name password:$password; \n" >> ./logs.txt echo "'this is a string' string's \"ABCDE\""
運行如下:
daokr@DK:~/myfile$ ./inout.sh ----------------Example for echo and printf useing ---------------------------- 2018年 03月 30日 星期五 17:08:45 CST pls enter name & passwod: wangg 123456 2018年 03月 30日 星期五 17:08:49 CST 'this is a string' string's "ABCDE"
logs.txt 文件內容
daokr@DK:~/myfile$ tailf logs.txt
your name:wl password:jj;