Find the answer to your Linux question:
Results 1 to 4 of 4
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2004
    Location
    Tokyo
    Posts
    24

    $1 and $LINES Query


    Hi All,

    whilst I enjoy spinning people with the following, "Nah, mate, don't use Windows", which usually gets the suprised response, "oh, what do u use, MAC". Of which I reply, "ah, u all know what I reply with". BUT, I feel I'm not really a Linux person unless I've mastered, Shell Programming, where I believe the real power of Linux World exists. Anyway, enough dribble, and on to my question of the day. I'm following this tut,

    http://www.tldp.org/LDP/abs/html/sha-bang.html

    and in the code on that page, there are variables, $1 and $LINES. Um, what are they..?

    if [ -n "$1" ]
    # Test if command line argument present (non-empty).
    then
    lines=$1
    else lines=$LINES # Default, if not specified on command line.
    fi
    and then further down, I see this use of one of them,

    tail -$lines messages > mesg.temp # Saves last section of message log file.
    mv mesg.temp messages # Becomes new log directory.


    Can anyone explain this in plain English.? I understand that a portion of messages is being written to mesg.temp,
    and that the rest is deleted. Just don't get where $1 and $LINES is being generated from. Cheers.

  2. #2
    Linux Newbie
    Join Date
    Jul 2004
    Location
    Calcutta, India
    Posts
    220
    $1 is a command line argument to the bash script ...
    For eg., if your bash script is named as say, first-script.sh ,

    you'll execute the script by :
    $ ./first-script.sh

    , rite ??

    Now , what if you want your script to take 1 or more arguments ??
    like, say :
    $ ./first-script.sh arg1 arg2 arg3

    In this case, $1 = arg1 , $2 = arg2 , $3 = arg3 and so on.
    Note that $0 = ./first-script.sh

    I hope you have had experience with C command-line arguments, this is similar to that, though there are some differences.

    about your $LINES, you'll notice that at the beginning of the script there are these lines present :
    Code:
    LOG_DIR=/var/log
    ROOT_UID=0     # Only users with $UID 0 have root privileges.
    LINES=50       # Default number of lines saved.
    E_XCD=66       # Can't change directory?
    E_NOTROOT=67   # Non-root exit error.
    notice the LINES=50 . here LINES is another shell variable defined by the script, and it has been initialized to 50 by default. it may be changed later by the script.

    Shell variables when called have to have $ in front. For eg.,
    Code:
    echo $LINES
    But when assigning shell variables, you shud NOT append $ in front., i.e.,
    Code:
    $LINES=78
    is wrong !!!
    Instead you shud use
    Code:
    LINES=78

  3. #3
    Just Joined!
    Join Date
    Nov 2004
    Location
    Tokyo
    Posts
    24
    Hi All,

    mate, that was beautiful. I'm sure I get it now. It's saying, if there are no argumnents being read, either $1, $2 etc, then use $LINES as the argument, which in this case, is $LINES=50, which you pointed me to. This means that 50 lines are to be read into mesg.temp. I appreciate the extra in-depth stuff you went in to too. Cheers.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Newbie
    Join Date
    Jul 2004
    Location
    Calcutta, India
    Posts
    220
    ya .. exactly !!! if you run it like this :
    Code:
    ./cleanup 25
    where cleanup is the name of the shell script, it will only keep the
    last 25 lines of the log, and erase or clean the rest.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •