1. IMPORTANT - 11/13/2015: In light of a possible breach we have scrubbed all passwords. You will need to request a password reset through the site. Click here for more information.

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

    Mar 14, 2014
    Likes Received:
    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


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

    exit 0
  2. unixfish

    unixfish Member

    Jan 8, 2014
    Likes Received:
    "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