我有一个问题。我需要阅读用户输入,但我需要用户输入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