Find the answer to your Linux question:
Results 1 to 8 of 8
I would like to include a validation within a script that displays usage information when an error message is returned by the command: if <error is returned> then //standard error ...
  1. #1
    Just Joined!
    Join Date
    Nov 2007
    Posts
    8

    Scripting help

    I would like to include a validation within a script that displays usage information when an error message is returned by the command:

    if <error is returned> then //standard error
    echo usage command parameter1 | parameter2
    fi


  2. #2
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    slyth,
    Usually most errors result in an exit and return a non-zero value, therefore you cannot wait for error to happen. It's better to detect conditions in which error will happen. Say you are writing a script that expects 1 argument, you can do a test if the condition is met before doing anything else:

    #/bin/bash
    [ $# -eq 1 ] || { echo "usage: `basename $0` para1"; exit 1; }


    This script is equivalent to
    #/bin/bash
    if [ $# -ne 1 ]; then
    echo "usage: `basename $0` para1"
    exit 1
    fi

    which is much easier to understand.

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    If you mean how you can run a program and then report an error if the program errors, consider something like this:

    Code:
    if ! some_command; then
        # Error
    fi
    Virtually every program (I say "virtually", but really, every program) will exit with code 0 if they are successful, and non-zero if there was an error. The above code executes a command, and then if the return code is non-zero, executes the code in the if statement.
    DISTRO=Arch
    Registered Linux User #388732

  4. #4
    Just Joined!
    Join Date
    Nov 2007
    Posts
    8
    The following script doesn't compile somehow, see anything wrong?

    What does $# represent?

    #/bin/bash
    if [$# -eq 1]; then
    echo "usage: test arg";
    exit 1;
    fi

    if [$1 -eq "arg"] then
    exit 0
    fi

    ./test
    [root@localhost bin]# ./test
    ./test: line 2: [0: command not found
    ./test: line 9: syntax error near unexpected token `fi'
    ./test: line 9: `fi'
    [root@localhost bin]#

  5. #5
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    should be

    #/bin/bash
    if [ $# -ne 1 ]; then
    echo "usage: test arg"
    exit 1;
    fi

    if [ "$1" == "arg" ]; then
    exit 0
    fi


    Please note the spaces around the conditional expressions.

  6. #6
    Just Joined!
    Join Date
    Nov 2007
    Posts
    8
    No luck yet

  7. #7
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    Try to use single '=' instead of double in the string comparison:
    if [ "$1" = "arg" ]; then

  8. #8
    Just Joined!
    Join Date
    Oct 2008
    Posts
    44
    Quote Originally Posted by slyth View Post
    ##next line, needs a ! in front, hence the line 2 error
    #/bin/bash
    if [$# -eq 1]; then
    echo "usage: test arg";
    exit 1;
    fi

    if [$1 -eq "arg"] then
    ##next line, try a ; after the 0
    exit 0
    fi

    ./test
    [root@localhost bin]# ./test
    ./test: line 2: [0: command not found
    ./test: line 9: syntax error near unexpected token `fi'
    ./test: line 9: `fi'
    [root@localhost bin]#
    As for the brackets, I'm not sure, but I think I found two of the problems anyway!

Posting Permissions

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