I have a question. I need to read the user input, but I need the user enter 4 lines. Everytime time user presses enter it terminates the read command, so I did read commands 4 times (not sure if this is correct) - I need help with this. Also how do I output user's input into file? and that output needs to me 4 lines as well. Please see my script. I appreciate any help.
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""
To read 4 lines you can use a loop and store it in an array:
#!/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
Then to print and redirect it:
printf "%s\n" "${lines[@]}" > lines.txt
I'm not sure if a get what you want... but see this peace of code
#!/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
if what you want is to put some lines into your file you may do:
echo $line1 > lines.txt
echo "
" + $line2 >> lines.txt
echo "
" + $line3 >> lines.txt
echo "
" + $line4 >> lines.txt
if it's only that, this sould do ;)