Reputation: 21
I've inherited a bash script (aren't legacies wonderful!) and I'm trying to figure out what's going wrong.
The offending line is this:
while [ "${\(\#\)}" -gt 0 ] ; do
and I get a "bad substitution" error message when I try to run it. I'm pretty sure it's trying to ask if the number of args is greater than zero, but you know that too.
I don't know if it ever worked. I imagine there are not enough backslashes, or maybe too many, or maybe the parentheses don't belong there. I tried several of these, but no luck.
Any enlightenment appreciated.
Upvotes: 2
Views: 886
Reputation: 8701
Although the original line produces the error message you mentioned (Bad substitution), just removing the parentheses should be enough to fix the problem. For example, with the following in file while-num
#!/bin/bash
while [ "${#}" -gt 0 ] ; do
echo $1
shift
done
and invoked with (eg)
./while-num 18173.msg 18179.msg 18201.msg
produces
18173.msg
18179.msg
18201.msg
On my Ubuntu-12 system, the same output results when the first line is #!/bin/sh
. (On Ubuntu the default shell is dash instead of bash.) It also works fine when the while
line is changed to while[ $# -gt 0 ] ; do
(ie the quotes and braces are not needed because $#
returns a number).
Upvotes: 2