Reputation: 3175
I have a script test.sh
:
OUTPUT_FILE="$1"; shift || { echo "require arg1: output file path"; exit 1; }
But when i execute ./test.sh
without arguments, it does not output require arg1: output file path
The output is shift: nothing to shift
Who can tell me why?
My ash environment: Busybox Android 2.2~4.4
Upvotes: 0
Views: 841
Reputation: 2541
Even though I can't reproduce your problem, the cause seems clear: your shift doesn't raise error as exit value, to proceed with script execution, when not enough parameters were specified, but otherwise ignores that condition silently. Instead it outputs an error of its own, and terminates further script execution.
Rather than relying on shift to produce that exit value - which yours doesn't seem to - you could think of testing command line for any parameters left to shift:
echo "${#@}"
shows the length of remaining (yet unshifted) command line. If it's 0, you want your warning message. Note that while bash produces the number of command line parameters, busybox ash counts the number of characters remaining on command line.
Alternatively. test $1 for emptyness:
[ -z "$1" ] && echo "no args"
Upvotes: 1