Arrays in Shell Script, not Bash

13,087

Solution 1

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

Solution 2

a="abc 123 def"

set -- $a
while [ -n "$1" ]; do
    echo $1
    shift
done

Output via busybox 1.27.2 ash:

abc
123
def

Solution 3

It's best not to use eval unless there is no other alternative. (The recent spate of bash exploits is due to the shell internally evaling the contents of environment variables without verifying their contents first). In this case, you seem to be in complete control for the variables involved, but you can iterate over the variable values without using eval.

#!/bin/sh

enable0=1
enable1=1

for port_enabled in "$enable0" "$enable1"; do
    if [ "$port_enabled" -eq 1 ]; then
        # do some stuff
    fi
done

Solution 4

One could use positional parameters for that...

http://pubs.opengroup.org/onlinepubs/009696799/utilities/set.html

#!/bin/sh

enable0=0
enable1=1

set -- $enable0 $enable1

for index in 0 1; do
    [ "$1" -eq 1 ] && echo "$1 is enabled." || echo "$1 is disabled."
    shift
done

Running on busybox:

~ $ ./test.sh 
0 is disabled.
1 is enabled.
Share:
13,087
Admin
Author by

Admin

Updated on June 17, 2022

Comments

  • Admin
    Admin almost 2 years

    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!