Find the answer to your Linux question:
Results 1 to 2 of 2
I am writing a simple script to test whether a value entered is a directory or not and provide an exit status. This is what I was able to accomplish ...
  1. #1
    Just Joined!
    Join Date
    Nov 2011
    Posts
    1

    Question Testing exit status

    I am writing a simple script to test whether a value entered is a directory or not and provide an exit status.

    This is what I was able to accomplish so far:
    • Display an appropriate error message and exit with status 3 if no parameters are given.

    • display an appropriate error message and exit with status 4 if the source directory does not exist.


    The issue is:
    • display an appropriate error message and exit with status 5 if the source is not a directory.


    Here is the code block:
    Code:
    DIR="$1"
    
    while [ $# -lt 1 ]
    do
            echo "Incorrect Usage"
            exit 3
    done
    
    if
    [ -d "$DIR" ] || echo "Does not exist" && test -d "$DIR" || echo "Not valid Directory"
    then
    exit 4 && exit 5
    fi
    Each time the script is executed, and when I type "echo $?" I only get exit status 4. The script works where it can identify if the directory exists and if in fact it is an actual directory.

    I believe that the "exit 4" statement ends the script, and the "exit 5" portion is never executed. Is there any method I can use so that exit status 5 properly displays if the value is not a valid directory?

    Any advice on this issue would be appreciated.

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    This sounds like a homework question, so I will be closing this thread.

    Two hints:

    1) [ -d "$DIR"] and test -d "$DIR" do the same thing. Nowhere are you checking for existence independently of whether or not it is a directory.

    2) "exit 4 && exit 5" means "exit with status 4, then exist with status 5". Since exiting with status 4 means that you will not run any more commands, of course you cannot exit with another exit status afterwards.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

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