Explain me how shell script is working?

G

Govinda Sakhare

Guest
i know below shell script will print each line of provided file.but how it is working? what is "read l" and "done <$1"

PS : i am new to shell scripting


#!/bin/sh

echo "Lines of file $1 are ::"
while read l
do echo -e "$l"
done < $1

exit 0
 


"while read l": "l" is a local environment variable name, probably short for line. This reads lines of data in a while loop into the local environment variable "l".

"done < $1": $1 is the first parameter passed to the script, i.e. the file name. The "<" directs the while loop to read line content from $1.

All together, this takes the first parameter, and passes it as input to a while loop that reads each line and echoes it to standard out (usually a terminal if you are logged on to run the script).

The issue with this script is leading whitespace is trimmed; if you are printing out the contents of a program's source code, you will lose your formatting.

The cat command also does the same thing; it make me wonder why this was written, unless it is a stub to insert more "stuff" into the loop.
 

Members online


Top