Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote