Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Bash - if [ -n "$@" ]

  1. #1
    Join Date
    Nov 2005
    Location
    Sweden
    Beans
    257
    Distro
    Kubuntu 9.10 Karmic Koala

    Bash - if [ -n "$@" ]

    My problem is quite simple, this script of mine isn't working.
    Code:
    if [ -n "$@" ]
    then
            VICTIM="$@"
            echo "if?"
    else
            VICTIM="127.0.0.1"
            echo "else?"
    fi
    What I want is that if i run "./myScript 192.168.0.1" if should make VICTIM=192.168.0.1 and this works fine. But when I run "./myScript" without anything else it should use a default 127.0.0.1 instead.
    As far as I know $@ is empty, I've even echoed it to see that it is. But it still doesn't want to run else. What am I doing wrong?

  2. #2
    Join Date
    Aug 2007
    Beans
    Hidden!

    Re: Bash - if [ -n "$@" ]

    #!/bin/bash
    if [ -n "$1" ]
    then
    VICTIM="$1"
    echo "if?"
    else
    VICTIM="127.0.0.1"
    echo "else?"
    fi
    Linux, feel the beauty of the source. See it, change it and use it

  3. #3
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Bash - if [ -n "$@" ]

    try using
    Code:
    [ "$1" != "" ]
    instead.

  4. #4
    Join Date
    Nov 2005
    Location
    Sweden
    Beans
    257
    Distro
    Kubuntu 9.10 Karmic Koala

    Re: Bash - if [ -n "$@" ]

    Okay thanks! Using $1 instead of $@ works great, anyone cares to explain what the difference is?

  5. #5
    Join Date
    Aug 2007
    Beans
    Hidden!

    Re: Bash - if [ -n "$@" ]

    $1 = First agument supplied after the program/function on execution.

    http://subsignal.org/doc/AliensBashTutorial.html
    Linux, feel the beauty of the source. See it, change it and use it

  6. #6
    Join Date
    Nov 2005
    Location
    Sweden
    Beans
    257
    Distro
    Kubuntu 9.10 Karmic Koala

    Re: Bash - if [ -n "$@" ]

    How come $@ doesn't work when $1 does? If $1 is emtpy, $@ must be empty as well right?

  7. #7
    Join Date
    May 2006
    Location
    BH, Brazil
    Beans
    338

    Re: Bash - if [ -n "$@" ]

    Quote Originally Posted by Rizado View Post
    How come $@ doesn't work when $1 does? If $1 is emtpy, $@ must be empty as well right?
    From the bash manpage:
    "When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed)."


    So, when you pass no arguments, bash interprets it as:
    if [ -n ]; then...

    Which for some reason evaluates to TRUE.


    To get your desired behviour, use:
    if [ -n ""$@"" ]; then..., which will evaluate to if [ -n "" ]; then... in case of no parameters.


    Somebody smoked before programming this!

  8. #8
    Join Date
    Aug 2006
    Location
    Boston
    Beans
    216
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Bash - if [ -n "$@" ]

    So its not just your beans that are roasted

  9. #9
    Join Date
    Nov 2005
    Location
    Sweden
    Beans
    257
    Distro
    Kubuntu 9.10 Karmic Koala

    Re: Bash - if [ -n "$@" ]

    Quote Originally Posted by volanin View Post
    From the bash manpage:
    "When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed)."


    So, when you pass no arguments, bash interprets it as:
    if [ -n ]; then...

    Which for some reason evaluates to TRUE.


    To get your desired behviour, use:
    if [ -n ""$@"" ]; then..., which will evaluate to if [ -n "" ]; then... in case of no parameters.


    Somebody smoked before programming this!
    Oh my
    That's just stupid... Glad it isn't me

  10. #10
    Join Date
    Jun 2007
    Location
    North London; England
    Beans
    697

    Re: Bash - if [ -n "$@" ]

    found this thread when googleing.

    this seemed to work for me:


    Code:
    if [ $# -eq 0 ] ; then
        printf "%s\n" "no args given, exiting" 
        exit 1
    fi
    from what i understand $# is read as the number of args, so it reads 0 if there are none.
    Desktop:i7 875k|4gb OCZ platinum ddr3 2000|Evga P55 LE mobo|OCZ RevoDrive 50gb|ATI 5850 Black Edition|Silverstone FT02|corsair tx650
    Portable: 13" Macbook Pro 2.8ghz i7 16gb RAM | Asus EEE TF101 | Samsung Galaxy S2

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

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