简体   繁体   中英

Assign value to a variable in a for loop via KornShell

I am having problem when trying to assign a value to a variable in a loop. It will be like using variable inside a variable. So I created an array to add the values still something is not right.

Please take a look at the following KornShell (ksh) script and let me know where did I mess up

#!/usr/bin/ksh
set -A array $1 $2 $3
set -A values
typeset -i a

a=0;
for files in ${array[@]}
do
cd ~/shell_lib
ls | grep $files
${values[$a]}="$(cksum $files)" 
a=$a+1
done

echo ${values[@]}

OUTPUT


$ ./intarray.sh forall.sh name.sh 
forall.sh
./intarray.sh[12]: =3311936491 251 forall.sh:  not found
name.sh
./intarray.sh[12]: =3294813710 338 name.sh:  not found

The file is there and it has done the cksum, but still it says not found! I can not understand what is wrong! Any insight?

${values[$a]}="$(cksum $files)"

This is almost certainly not what you want. Try:

values[$a]="$(cksum $files)" 

${array[index]} refers to the value of the (still empty) array entry, so the original line results in the shell trying to execute ="$(cksum $files)" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM