Question

write a script named print_lines.sh that uses head and tail together to print out a specific...

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.

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Finally, write a script named word_counts.sh that prints out how many words are on each line...
Finally, write a script named word_counts.sh that prints out how many words are on each line of standard input, as well as the total number of words at the end. The script should take no arguments. Instead, the script must work by reading every line of standard input (using a while loop) and counting the number of words on each line separately. The script is intended to work with standard input coming from a pipe, which will most often come...
The first script you need to write is login.sh, a simple script that you might run...
The first script you need to write is login.sh, a simple script that you might run when you first log in to a machine. We'll expand this script later, but for now it should include your name, username, hostname, current directory, and the current time. Here is some sample output to help guide you. Note that the bolded lines that start with "[user@localhost ~]$" are the terminal prompts where you type in a command, not part of the script. Of...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in the file contains seven numbers,which are the sales number for one week. The numbers are separated by comma.The following line is an example from the file 2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36. The program should display the following: . The total sales for each week . The average daily sales for each week . The total sales for all of the weeks .The average weekly sales .The week number...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how the firms resources incompetencies support the given pressures regarding costs and local responsiveness. Describe entry modes have they usually used, and whether they are appropriate for the given strategy. Any key issues in their global strategy? casestudy: Atlanta, June 17, 2014. Sea of Delta employees and their families swarmed between food trucks, amusement park booths, and entertainment venues that were scattered throughout what would...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT