Explain me how shell script is working?

Discussion in 'General Linux' started by Govinda Sakhare, Mar 17, 2014.

  1. Govinda Sakhare

    Govinda Sakhare New Member

    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    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
  2. unixfish

    unixfish Member

    Messages:
    38
    Likes Received:
    23
    Trophy Points:
    8
    "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.

Share This Page