I am probably just having a brain fart, but I can not for the life of me figure out how to loop through an array in shell script, not bash. Im sure the answer is on stackoverflow somewhere already, but I can not find a method of doing so without using bash. For my embedded target system bash is not currently an option. Here is an example of what I am attempting to do and the error that is returned.
#!/bin/sh enable0=1 enable1=1 port=0 while [ ${port} -lt 2 ]; do if [ ${enable${port}} -eq 1 ] then # do some stuff fi port=$((port + 1)) done
Whenever I run this script the error “Bad substitution” is returned for line with the if statement. If you guys have any ideas I would greatly appreciate it. Thanks!
Advertisement
Answer
BusyBox provides ash
which does not directly provide array support. You could use eval
and something like,
#!/bin/busybox sh enable0=0 enable1=1 for index in 0 1 ; do eval assign="$enable$index" if [ $assign == 1 ]; then echo "enable$index is enabled" else echo "enable$index is disabled" fi done