This assignment involves the creation of two shell scripts on the COE machines.
ID: 3846856 • Letter: T
Question
This assignment involves the creation of two shell scripts on the COE machines. Please be ready to show a working demo of these scripts to the Teaching Assistants. Attach the scripts to your report submission with the file names specified below. a) Write a shell script named CombineFiles.sh that takes three file names as arguments: , , and . The script should read the content of files and , and combine them into a new file called . For example, running $ ./CombineFiles.sh new.txt/etc/passwd message.txt will combine the content of files/etc/passwd and message.txt -file message.txt must exist- into a new file named new.txt. Attach script CombineFiles.sh in your lab submission on Blackboard. b) Write a shell script named wc2.sh that takes two file names as arguments. The script should count the number of lines, words, and characters adding up both files. You can invoke script CombineFiles.sh from here, or you can find an alternative strategy. Attach script wc2.sh in your lab submission on Blackboard.Explanation / Answer
Part-a:-
Code:
#!/bin/bash
# program to combine two files into a new file
# script name: CombineFiles.sh
if [ $# -ne 3 ]
then
echo "You must pass three arguments. Usage: $0 <dest> <src1> <src2>"
exit
fi
# putting the file names into variables
new_file=$1
file1=$2
file2=$3
# flushing out the file $new_file if it already exists
test -e $new_file && cat /dev/null>$new_file
# writing content from file1 to new_file
while read line
do
echo $line>>$new_file
done<$file1
# writing content from file2 to new_file
while read line
do
echo $line>>$new_file
done<$file2
Execution and output:
Unix Terminal> cat a
12345678910
10987654321
Unix Terminal> cat b
abcdefghijkl
mnopqrstuvw
Unix Terminal> ./CombineFiles.sh c a b
Unix Terminal> cat c
12345678910
10987654321
abcdefghijkl
mnopqrstuvw
Part-b:-
Code:
#!/bin/bash
# progam to merge two given files and report number of lines, characters and words
# script name: wc2.sh
# validating if the arguments passed are 3
if [ $# -ne 3 ]
then
echo "you must pass three arguments. Usage: $0 <dest> <src1> <src2>"
exit
fi
# fetching given file names into the variables
new_file=$1
file1=$2
file2=$3
# calling CombineFiles.sh script here
./CombineFiles.sh $new_file $file1 $file2
# calculating number of lines, words and characters in the new file
no_of_lines=`wc -l $new_file|awk '{print $1}'`
no_of_words=`wc -w $new_file|awk '{print $1}'`
no_of_chars=`wc -c $new_file|awk '{print $1}'`
echo "File '$new_file' has $no_of_lines lines, $no_of_words words and $no_of_chars characters"
Execution and output:
Unix Terminal> cat b
abcdefghijkl
mnopqrstuvw
Unix Terminal> cat c
12345678910
10987654321
abcdefghijkl
mnopqrstuvw
Unix Terminal> ./wc2.sh
you must pass three arguments. Usage: ./wc2.sh <dest> <src1> <src2>
Unix Terminal> ./wc2.sh a b c
File 'a' has 6 lines, 6 words and 74 characters
Unix Terminal> cat a
abcdefghijkl
mnopqrstuvw
12345678910
10987654321
abcdefghijkl
mnopqrstuvw
Unix Terminal>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.