What is wrong in the Bash script about parameters and Wget?

1

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"
}

Léo Léopold Hertz 준영

Posted 2010-01-20T12:38:06.407

Reputation: 4 828

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 – 2010-02-21T11:47:55.127

Are you joking? You get the desired output from the -h switch... – Bobby – 2010-01-20T12:59:38.590

do we have a [smells-like-homework] tag yet? – quack quixote – 2010-01-20T13:41:53.047

1Simple question: what is actually wrong? What do you expect? – Gnoupi – 2010-01-20T17:01:03.167

Answers

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

Paused until further notice.

Posted 2010-01-20T12:38:06.407

Reputation: 86 075