1

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"
}
4
  • Are you joking? You get the desired output from the -h switch...
    – Bobby
    Jan 20, 2010 at 12:59
  • do we have a [smells-like-homework] tag yet? Jan 20, 2010 at 13:41
  • 1
    Simple question: what is actually wrong? What do you expect?
    – Gnoupi
    Jan 20, 2010 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, 2010 at 11:47

1 Answer 1

2

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

You must log in to answer this question.

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