Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Runnning the command gives

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

The code

# example wget_exam2 java http://www.example.com/ex_1
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;;
    esac
    done
    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
1  
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

1 Answer 1

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 $?
0
$ testfunc 0
$ echo $?
0
$ testfunc 1
$ echo $?
1
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.