Make a bash script to do the following. Given an input and an output file specif
ID: 3779193 • 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
You can use the above as part of your solution. You can append to a file in a similar manner.
echo "output text" >> $outfile
You do not need to append to a file for this homework, but it is good to know.
Be sure to include appropriate comments throughout.
In the directions below, replace "XX" with the name corresponding to your account. Replace "Y" with the homework number.
Explanation / Answer
I have written the bash file for the problem.
#!/bin/bash
echo Enter the input filename
read file1
if [[ ! -f $file1 ]] ; then
echo 'File' $file1 'is not there, aborting.'
exit
fi
echo Enter the output filename
read file2
if [[ ! -f $file2 ]] ; then
echo 'File' $file2 'is not there, aborting.'
exit
fi
sed '$!N;s/ / /' $file1 > $file2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.