Script:
#!/bin/bash
for SID in db1 db2 db3 db4
do
"$SID"_fs=$(
df -k
| grep "$SID\/data"
| tr -s ' '
| cut -d' ' -f6
| awk '{print substr($0,length($0),1)}'
| sort
| tail -1);
echo "$SID"_fs
done
./test_sm.sh: line 11: db1_fs=7: command not found
db1_fs
./test_sm.sh: line 11: db2_fs=7: command not found
db2_fs
./test_sm.sh: line 11: db3_fs=3: command not found
db3_fs
./test_sm.sh: line 11: db4_fs=2: command not found
db4_fs
The variables are set to the correct value but the final "echo" does not give me the value of the variable (which is what I require). It instead gives me the variable name.
Use
declare
. The following shows both how to set the variable name (usingdeclare
) and how to retrieve the value (using indirect parameter expansion).bash
4.3 introduces namerefs which simplify this task.You might consider using an associative array as well, instead of individual parameters.