Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Runnning the command gives

wget_exam -h
Usage: wget_exam2 <fileType> <source>
exit     // then immediately Terminal shut dows

The code

# example wget_exam2 java
function wget_exam2 {
    while [[ $1 == -* ]]; do
    case "$1" in 
        -h|--help|-\? ) echo "Usage: wget_exam2 <fileType> <source>"; exit;;
            --) shift; break;;
        -*) echo "invalid option: $1"; echo "Usage: wget_exam2 <fileType> <source>"; exit;;
    wget --random-wait -nd -r -p -A "$1" -e robots=off -U mozilla "$2"
share|improve this question
Are you joking? You get the desired output from the -h switch... – Bobby Jan 20 '10 at 12:59
do we have a [smells-like-homework] tag yet? – quack quixote Jan 20 '10 at 13:41
Simple question: what is actually wrong? What do you expect? – Gnoupi Jan 20 '10 at 17:01
Questioner is expecting invocations of the function not to result in exits from their shell. Dennis' answer below explains why it does, and how to fix it. – dubiousjim Feb 21 '10 at 11:47
up vote 2 down vote accepted

If you are executing this function from a shell prompt, the exit command is telling the shell to exit rather than the function. You should probably use return instead.

You can use a return value with return and test for that in the script that calls the function and use the exit there to exit the script (or not depending on the return value).

$ testfunc(){ return ${1:-0}; }
$ testfunc
$ echo $?
$ testfunc 0
$ echo $?
$ testfunc 1
$ echo $?
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .