我有一個問題。我需要閱讀用戶輸入,但我需要用戶輸入4行。每次用戶按下輸入它終止讀取命令,所以我沒有讀取命令4次(不知道這是否正確) - 我需要幫助。 另外我如何輸出用戶的輸入到文件?而且輸出也需要4條線。請看我的劇本。 我感謝任何幫助。Shell腳本 - 從用戶輸入4行
1 #!/bin/bash
2 #Displaying user name, host name, time, and date
3 Time=`date +%r`
4 Date=`date +%m/%d/%Y`
5 echo "$USER is running this script on $HOST at $Time on $Date:"
6 echo""
7 echo "Please enter 4 lines of text: "
8 read line1
9 read line2
10 read line3
11 read line4
12 echo "I'm now putting the four lines you entered into a text file called \"mylines.txt\"..."
13 echo $line1\n $line2\n $line3 \n$line4 > lines.txt
14 echo""
15 echo "The lines you entered were:\n$line1\n$line2\n$line3\n$line4"
16 echo""
重複'read'命令就可以了。對於寫入文件,使用'echo -e「$ line1 \ n $ line2 \ n ...」> lines.txt'(不含空格)或重複'echo $ 1> lines.txt','echo $ line2 >>行.txt等 – jofel