Write a bash script that does the following: 1 Takes as its single parameter the
ID: 3574929 • Letter: W
Question
Write a bash script that does the following: 1 Takes as its single parameter the path to, and name of a file. 2 Inside the script, use the cat command and a for line in… construct to display the lines in the specified file, preceding each line with the line number and a colon (“:”). Put the whole line inside single quotes. 3. Do not use the cat flag that would do this trivially; rather, keep track of which line you are on inside the for loop, and have your code generate and display the line numbers.
Explanation / Answer
Run it as ./<script-name> <file-path>
#!/bin/bash
COUNTER=0
FILEPATH=$1 #takes filepath from input provided
cat $FILEPATH | #redirecting output to next while loop
while read line; do #reading line by line from above output, i.e output of cat <file-name>
echo $COUNTER':' $line #echoing required matter
let COUNTER=COUNTER+1 #incrementing line counter
done
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.