Make a bash script to do the following. Given an input and an output file specif
ID: 3779869 • Letter: M
Question
Make a bash script to do the following. Given an input and an output file specified from the command line, join every two input lines together, writing them to the output. For example, given a file with contents:
one two
three
fish hat
cat hop
the program should write the following to the output file.
one two three
fish hat cat hop
Make sure to check that the input file exists. If not, write an error message and quit. Also make sure that the output file does NOT exist. If it does, write an error message and quit. You can use the "echo" command to write the error message to the screen.
A bash script can read a file in a couple of ways. One is shown below, where "myfilename" is defined before the loop.
while read thisline
do
echo " $thisline"
done < $myfilename
Writing to a file is easy, as in the following.
echo "output text" > $outfile
Explanation / Answer
#!/bin/bash //Script should always be run with bash
while read line1 //read the first line
do read line2 //read the second line
echo "$line1 $line2"; //Printing both the lines with space as delimiter
done < input.txt //input.txt is the input file, if it does not exist it will throw error
while executing the script, Please redirect to the other file as shown below which by defaults creates new file with the name specified by you.
./script.sh > output.txt
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.