SHELL SCRIPT BASH PROGRAMMING Write a shell script called getlines.sh that will
ID: 3790738 • Letter: S
Question
SHELL SCRIPT BASH PROGRAMMING
Write a shell script called getlines.sh that will requires three command-line arguments, a starting line number, s, a line count n, and the name of a file to operate on (to read). The script will then produce, on the standard output, n lines starting with line s from the specified file. The usage pattern for the script is therefore
getlines <starting line number> <count> <file name>
You MUST use a pipeline and the head(1) and tail(1) commands in your script
For example:
./getlines.sh 5 10 file.txt will produce an output starting at line 5th line through 10 lines (end at line 15th) NOT starting at line 5th and end at line 10th.
NOTE THAT: tail -n +$firstline "$filename" | head -n $(lastline - $firstline + 1) is NOT a correct solution for this question.
Explanation / Answer
script:
#!/bin/bash
filename=$3
startline=$1
count=$2
tot=`expr $startline + $count - 1 `
echo $filename, $tot;
head -n $tot $filename|tail -n $count
Output:
$ ./getline.sh 2 6 baby_data_small.txt
baby_data_small.txt, 7
Emma 3561
Ava 2918
Sophia 3765
Mia 2204
Sophia 2749
Lourdes 3981
$ cat baby_data_small.txt
Sophia 4435
Emma 3561
Ava 2918
Sophia 3765
Mia 2204
Sophia 2749
Lourdes 3981
Sophia 2617
Olivia 4352
Emma 840
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.