var1=test1
setA=testA
if
...
touch $setA/$var1
...
fi
I would like now the repeat the command touch (in this example) for different variables.
So below, the varX should run 3 times (var1, var2, var4). Var3 is skipped in this example since it's not filled in.
var1=test1
var2=test2
var3=
var4=test4
setB=testB
setC
setD=testD
if
...
touch $setX/$varX
...
fi
What's the best way to do this?
I can of course copy the if x-time and change the $setX/$varX. But I suppose there is a better way (since the script is about 30 lines, and the variables are about 10 values)...
set | grep -E '^var[0-9]+=' | while IFS== read VAR VAL
do
if [ ! -z $VAL ]; then
touch $VAL
fi
done
EDIT : Original requirement changed. Still you can modify the script accordingly. Always re-post the updated requirement instead changing the original.
Sorry, I hoped/wished that nobody seen this already. But you guys are fast here.
I'm fighting with your example to get the 2 values in. But for one of the other reason, i can't figure out how to use the second variable in the same script.
Should/can I do that in the same 'set' command? With a kind of 'and' command?
For the moment i've got:
SensorID1='test1'
SensorValue1='watt'
SensorID2='test2'
SensorValue2='liter'
set | grep -E '^SensorID[0-9]+=' | while IFS== read VAR SensorID
do
if [ ! -z $SensorID ]; then
touch $SensorID.$SensorValue
fi
done
set | grep -E '^SensorID[0-9]+=' | while IFS== read VAR SensorID
do
SensorValue=$(eval echo \$${VAR/SensorID/SensorValue})
if [ ! -z $SensorID ] && [ ! -z $SensorValue ] ; then
touch $SensorID.$SensorValue
fi
done
You can remove the AND part in the "if" statement if not required.
In above, you don't touch $SensorValue as it is undefined. On top, you can't make sure that the variables will be listed in the necessary order by set .
You request is not clear. Let me try to paraphrase it: You need to touch m files in n directories, i.e. (n x m) files in total, and directories and files are held in a set of variables each.
Use two nested loops, and check if it's possible to hold the variables in two arrays.