Here is the code
#file is named getpack2
count=0
while [ -n "$*" ] ; do
ARRAY[${count}]=$1
shift
count=`expr $count + 1`
done
for t in "${ARRAY[@]}"; do
mkdir $t
cd $t
touch hello
cd ..
done
the line im using to run this is:
getpack2 vocals-doo flute-wood
this creates the desired directories and files
this will also work:
./getpack2 vocals-doo flute-wood
however, when I prefix the command with sudo:
sudo ./getpack2 vocals-doo flute-wood
it gives me the following errors
./getpack2: 7: ARRAY[0]=vocals-doo: not found
./getpack2: 7: ARRAY[1]=flute-wood: not found
./getpack2: 15: Bad Substitution
I’m very new to shell scripting. Just started learning it today. Is there some sort of scoping error?
Your superuser (root) may have a different shell that does not support arrays (for instance
dashor some other small shell.) Try to program using only standard bourne shell constructs. Or include in the header of your script the interpreter to use: