[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Exit application with two function calls
From: |
Linda Walsh |
Subject: |
Re: Exit application with two function calls |
Date: |
Sun, 27 Jan 2008 16:25:36 -0800 |
User-agent: |
Thunderbird 2.0.0.9 (Windows/20071031) |
In the spirit of there being more than one solution to every problem,
I made the solution "more complicated" :-) in case you wanted
to check more error conditions...
I included some "Notes" about why I did some things they way I
did, since it may not be immediately obvious. Some of them are
probably "personal taste"....
------
#!/bin/bash -e
# *1
Error () # *2
{ # *3
echo -e "\n\terror: ${*}\n"
return 2
}
Check_file ()
{
if [[ -d "$1" ]]; then echo "$1" # *4
else return Error "invalid directory: \"$1\""; fi
return 0
}
if [[ "$#" -ne 1 ]]; then
echo "$0 called with wrong number of params" && exit 3
else
chkFile=$(Check_file "$1") || { echo "do not print"; exit 4; } # *5 & *6
fi
#---------------------------
# Notes:
# *1 - using "-e" stops your script immediately on any error; useful
# during debugging (maybe production if for yourself only);
# Using "-ue" while slightly more work will also catch unexpectedly
# empty shell-variables; to trivially catch undef vars where
# you expect possiblity of undef'ed vars, use: ${maybeundef:-}
# *2 - Capitalizing func names usually helps to not confuse function
# names with system commands
# *3 - if you want a blank line for readability after a function def,
# just put the curly brace on the next line (slightly more readable
# in my opinion, but personal pref
# *4 - test with "-d" if you are expecting a directory
# *5 - note -- quotes around param, "$1", in case there is a space
# embedded in the argument
# *6 - prefer construct "$(cmd) over `cmd`; avoides some quoting probs
# or cockpit errors