Write a Bash shell script of the \"head\" command. We may use temporary files as
ID: 3769751 • Letter: W
Question
Write a Bash shell script of the "head" command. We may use temporary files as part of these scripts.
Write a simple version of the "head" command (with a "while" loop). Write a script called "head1" that takes one argument: the filename to print out.
#At the start of the script 1st check that we have the correct number of arguments, and 2nd, check if the file is readable. If there is an error, print out an appropriate error message and exit.
#The body of the script reads one line at a time from the specified file and prints it to standard output.
#After 10 lines it stops and exits.
#Use a "while" loop to do the actual work. We will need to pipe the input file into the loop. Test it with one file that has < 10 lines and another that has > 10 lines.
Syntax: head1 filename
*****Hint: We have 2 end-of-loop conditions: end-of-file and 10 lines. If we use "read" in the test part of the loop, it automatically checks for EOF. The line count can be checked either in the "test" part of the loop or in the body of the loop. *******
Explanation / Answer
YOU DON"T NEED WHILE LOOP FOR THAT, THE HEAD COMMAND ALONE CAN DO THAT
#1/bin/bash
head $1
LET ME KNOW IF YOU HAVE ANY DOUBT. SAVE THE ABOVE AS head1. THEN RUN IT AS head1 <filename>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.