Discussion:
bash-like arrays with busybox / ash
Guenter
2009-03-12 12:09:19 UTC
Permalink
Hi all,
I just suprisingly found that latest busybox 1.13.3 seems not yet
support arrays like bash does, f.e. something like:

#!/bin/ash
url='http://....'
files=(`wget -q -O - $url`)
for ((i=0; i<${#files[*]}; i++)); do
echo "$i - ${files[$i]}"
done

returns:
./getlist: line 3: syntax error: "(" unexpected

are there any plans to implement arrays soon?

thanks, G?n.
Roy Marples
2009-03-12 13:18:08 UTC
Permalink
Post by Guenter
Hi all,
I just suprisingly found that latest busybox 1.13.3 seems not yet
#!/bin/ash
url='http://....'
files=(`wget -q -O - $url`)
for ((i=0; i<${#files[*]}; i++)); do
echo "$i - ${files[$i]}"
done
./getlist: line 3: syntax error: "(" unexpected
You can use arrays with any shell with a little thought

#!/bin/ash
url='http://....'
set -- `wget -q -O - $url`
i = 0
for file; do
echo "$i - $file"
i=$(($i + 1))
done

DISCLAIMER: The above was typed directly into an email and not actually
tested.
Post by Guenter
are there any plans to implement arrays soon?
Hopefully there are no plans to implement any bash specific features :)

Thanks

Roy
Guenter
2009-03-12 15:43:36 UTC
Permalink
Hi Roy,
Post by Roy Marples
You can use arrays with any shell with a little thought
#!/bin/ash
url='http://....'
set -- `wget -q -O - $url`
i = 0
for file; do
echo "$i - $file"
i=$(($i + 1))
done
DISCLAIMER: The above was typed directly into an email and not actually
tested.
thank you very much! And sorry that I'm not yet more familar with shell
scripting, but still learning ....

Here's what I have now, and this works basically, though still need to
understand the set construct:

#!/bin/ash
url='http://www.busybox.net/downloads/snapshots/?F=0'
dst='/tmp'
echo -e "*** File list from ${url%/*} ***\n"
set -- `wget -q -O - $url | grep '^<li>' | cut -d\" -f2`
i=0
for file; do
i=$(($i + 1))
printf "%3d - %s\n" $i $file
done
echo ""
read -p "Select file # for download: " n
until test 0 -lt "$n" -a "$n" -le "$i" ; do
echo "Value is not in range (1 - $i) - try again!"
read -p "Select file # for download: " n
done
i=0
for file; do
i=$(($i + 1))
test "$n" -eq "$i" && break
done
echo ""
echo "File: $file"
url="${url%/*}/$file"
echo wget -nv -P $dst -O $file $url

one thing I didnt find yet in any docu: how can I catch if a string is
entered instead of a number with read?
Also if there's still a better / shorter way to do the same I would love
to hear about!

thanks again for your help!

Guen.
Roy Marples
2009-03-12 15:56:54 UTC
Permalink
Post by Guenter
Here's what I have now, and this works basically, though still need to
set -- one two three
Sets the position parameters $1 $2 and $3 to one two three respectively

set -- "hello world" foo bar
$1="hello world"
$2=foo
$3=bar

somefunc()
{
echo $1
echo $2
echo $3
}

somefunc "hello world" foo bar

HTH
Post by Guenter
one thing I didnt find yet in any docu: how can I catch if a string is
entered instead of a number with read?
if ! printf "%d" $n >/dev/null 2>&1; then
echo "You didn't enter a number"
exit 1
fi
Post by Guenter
Also if there's still a better / shorter way to do the same I would love
to hear about!
Many ways to skin a cat. If it works for you then great!

Thanks

Roy
Paul Smith
2009-03-12 17:15:29 UTC
Permalink
Post by Roy Marples
Post by Guenter
one thing I didnt find yet in any docu: how can I catch if a string is
entered instead of a number with read?
if ! printf "%d" $n >/dev/null 2>&1; then
echo "You didn't enter a number"
exit 1
fi
Alternatively:

case $n in
*[^0-9]*) echo "You didn't enter a number"; exit 1 ;;
esac

(checks to see if any non-numeric character exists in $n).
Denys Vlasenko
2009-03-12 15:57:22 UTC
Permalink
Post by Guenter
Hi all,
I just suprisingly found that latest busybox 1.13.3 seems not yet
#!/bin/ash
url='http://....'
files=(`wget -q -O - $url`)
for ((i=0; i<${#files[*]}; i++)); do
echo "$i - ${files[$i]}"
done
./getlist: line 3: syntax error: "(" unexpected
are there any plans to implement arrays soon?
Plans to implement it eventually for ASH_BASH_COMPAT, yes.
Need to have enough free time for that.
(volunteers much welcomed)
--
vda
walter harms
2009-03-14 11:23:40 UTC
Permalink
Post by Guenter
Hi all,
I just suprisingly found that latest busybox 1.13.3 seems not yet
#!/bin/ash
url='http://....'
files=(`wget -q -O - $url`)
for ((i=0; i<${#files[*]}; i++)); do
echo "$i - ${files[$i]}"
done
./getlist: line 3: syntax error: "(" unexpected
are there any plans to implement arrays soon?
no, except you are doing it.

For your priblem it will be more easy to rewrite it, like (untested!):


for FILE in $( wget -q -O - $url )
do
echo $FILE
done

re,
wh

Loading...