Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux Shell脚本-从用户输入4行_Linux_Bash_Shell - Fatal编程技术网

Linux Shell脚本-从用户输入4行

Linux Shell脚本-从用户输入4行,linux,bash,shell,Linux,Bash,Shell,我有个问题。我需要读取用户输入,但我需要用户输入4行。每次用户按enter键时,它都会终止读取命令,所以我确实读取了4次命令(不确定这是否正确)-我需要这方面的帮助。 另外,如何将用户的输入输出到文件中?这个输出也需要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 "

我有个问题。我需要读取用户输入,但我需要用户输入4行。每次用户按enter键时,它都会终止读取命令,所以我确实读取了4次命令(不确定这是否正确)-我需要这方面的帮助。 另外,如何将用户的输入输出到文件中?这个输出也需要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""

要读取4行,可以使用循环并将其存储在数组中:

#!/bin/bash

lines=()
echo  "Please enter 4 lines of text:  "
for ((i=1; i<=4; i++)); do
   IFS= read -p "Enter line $i: " -r line && lines+=("$line")
done

我不确定a是否能得到你想要的。。。 但看到这和平的代码吗

#!/bin/bash

myfile="file"

echo "a line im my file" > $myfile
echo "now a erased my file and create another with the same name" > $myfile
echo "now I'm writing at the end of my file" >> $myfile
echo "



now I put some lines in the end of my file" >> $myfile
如果您想在文件中放入一些行,可以执行以下操作:

echo $line1 > lines.txt
echo "



" + $line2 >> lines.txt
echo "



" + $line3 >> lines.txt
echo "



" + $line4 >> lines.txt

如果只是这样的话,这个就可以了;)

重复的
read
命令正常。要写入文件,请使用
echo-e“$line1\n$line2\n…”lines.txt
(不带空格)或重复
echo$1>lines.txt
echo$line2>>lines.txt
等。当我尝试运行此操作时,它会告诉我语法错误:“(
lines=()
您可能没有使用
bash
来执行它。请确保您使用
bash./script.sh
来运行此脚本,这样我就不能像这样运行它了
/script.txt
?因为这是我的教授想要的。是的,您也可以这样做,只需在脚本顶部使用shebang
!/bin/bash
来确保它与bash一起运行即可只有在没有发生任何事情的情况下,我才发现
语法错误:“(“意外的

echo $line1 > lines.txt
echo "



" + $line2 >> lines.txt
echo "



" + $line3 >> lines.txt
echo "



" + $line4 >> lines.txt