write a script named print_lines.sh that uses head and tail together to print out a specific set of lines from a file. The script should take three arguments: the line number to start at, the line number to stop at, and the file to use. Here's an example run:
[user@localhost ~]$ print_lines.sh 7 10 /etc/passwd
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[user@localhost ~]$
In this example, the script prints line 7 through 10 (inclusive) of the /etc/passwd file. Your script must do error checking. Specifically, you need to check all the following things:
1. You get the right number of arguments (3).
2. The file specified exists and is a normal file.
3. The first line number specified is less than or equal to the last line number specified.
4. The actual number of lines in the file is greater than the last line to be printed.
If any of those conditions are not true, you should print an appropriate error message to the user and stop. If they are all met, then you'll need to do a bit of arithmetic and use head and tail together to print out only the lines requested.
Once you've got it tested and working, TAKE A SCREENSHOT (3) of the output of the script using some example arguments.
TAKE A SCREENSHOT (4 – print_lines.sh) your script. I can’t grade if your screenshot is not readable. Bigger font please! If you can’t control font size, please submit the file as text format.
SCRIPT
#!/bin/bash
usage()
{
echo ".print_lines.sh <start line number>
<end line number> <filename>"
}
f=$3
if [ $# -ne 3 ]
then
echo "Please provide 3 arguments"
usage
exit
elif [ -f $3 ] && [ -e $3 ]
then
lines=`cat $3 | wc -l`
tail +$1 $f | head -$2
else
echo "failed"
fi
If the code works for you please UPVOTE my answer or if you face any problem comment bellow.
Get Answers For Free
Most questions answered within 1 hours.