Find the answer to your Linux question:
Results 1 to 6 of 6
I am trying to learn shell scripting without any book, only with man pages and some documentation. This is the script i wrote to check if a certain user has ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2003
    Posts
    70

    need help with a shell script


    I am trying to learn shell scripting without any book, only with man pages and some documentation. This is the script i wrote to check if a certain user has logged in or not...

    read name
    user='who | grep -w $name'
    if[ -z $user ]
    then
    echo "The user has not logged in"
    else
    ehco "THe user has logged in"
    fi

    now this is giving me th foll errors:
    1: -z command not found
    2: unexpected syntax near 'else'

    could someone hint me as to what is wrong with this script?
    Thank you

  2. #2
    Linux Guru
    Join Date
    Apr 2003
    Location
    London, UK
    Posts
    3,284
    do you have a #!/bin/sh line at the top of the script?

  3. #3
    Linux Guru fingal's Avatar
    Join Date
    Jul 2003
    Location
    Birmingham - UK
    Posts
    1,539
    Also you may need to make the script executable by using chmod. There are slightly different approaches to this: I'm not that advanced so I just:

    chmod u+x filename.sh Then type ./filename.sh to try it out.

    Could you also do me a favour? If you get your script to work could you post it to the forum as other people might find it useful. (Me in particular).
    I am always doing that which I can not do, in order that I may learn how to do it. - Pablo Picasso

  4. #4
    Just Joined!
    Join Date
    Dec 2003
    Posts
    70
    thank you for ur advice, but jason, i have included the line #!/bin/bash.
    with respect to the permissions, i have made the file executable, otherwise it wouldnt have run. Is something wrong with the syntax or something..( personally i dont think so)...

  5. #5
    Linux Enthusiast Opnosforatou's Avatar
    Join Date
    Dec 2003
    Location
    Vleuten, The Netherlands
    Posts
    552
    Code:
    #!/bin/sh -xv
    if test "$1" = "" ; then
            echo "Usage&#58; test <username>"
            exit
    fi
    user=`who | grep -w $1`
    if &#91; -z $user &#93;  ; then
            echo "The user has not logged in"
    else
            echo "THe user has logged in"
    fi
    Try this... should work. (I used the filename test for this script..)

    oh, the -xv at the end of #!/bin/sh let you see the processing of the script. Use it for debug purposes of you bash scripts.

    Script disection:

    Code:
    #!/bin/sh -xv
    We are starting a bash script and show us all output

    Code:
    if test "$1" = "" ; then
            echo "Usage&#58; test <username>"
            exit
    fi
    Check if an argument was passed; if empty return the usage and exit program.

    Code:
    user=`who | grep -w $1`
    Fill the var with the output of this two applications. hence the ` NOT '

    Code:
    if &#91; -z $user &#93;  ; then
            echo "The user has not logged in"
    else
            echo "THe user has logged in"
    fi
    Check if there is output if so, user is logged in else not logged in.
    Note: when making an if statement be sure to include ; before the then that was the 2: unexpected syntax near 'else' error you got.
    The 1: -z command not found was due to not having a space between if and the first [

    I hope this helped you a bit...
    For further scripting info please check :

    here and here
    ---[ MS09-99896 - Vulnerability in All MS Windows OS ; Using Windows Could Allow Remote Code Execution. ]---
    Hardware: Asus P4P800, 1GB, P4-3Ghz, Asus V9950, Maxtor ATA HD\'s, 3Com GBit lan, Audigy ZS Plat.

  6. #6
    Linux Newbie
    Join Date
    Mar 2003
    Location
    Istanbul/Türkiye
    Posts
    246
    put a blank between if and [ -z $user] and try
    ` instead of ' for the commands you give in your script.
    Just a Newbie....Looking 4 Info....

Posting Permissions

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