Hello All,
I am novice on Shell Scripting. Any help on this is highly appreciated.
I have a variable
$VARIABLE="$some1|$some2|$some3"
I need sub variables $SUBVAR1,$SUBVAR2,$SUBVAR3 which must be equal to $some1 , $some2 and $some3 respectively.
It works fine with
$SUBVAR1 = `echo $VARIABLE | AWK -F"|" '{print $1}'`
$SUBVAR2 = `echo $VARIABLE | AWK -F"|" '{print $2}'`
$SUBVAR3 = `echo $VARIABLE | AWK -F"|" '{print $3}'`
I needed this in a loop.
But I can't give print$i in the AWK.
In future the $VARIABLE may be expanded to $some4|$some5 and in that case I must have two new sub variables $SUBVAR4 and $SUBVAR5.
Any help on this is appreciated.
Thanks
jingi1234
shell != perl
#!/bin/ksh
VARIABLE="some1|some2|some3"
echo "${VARIABLE}" | while IFS='|' read SUBVAR1 SUBVAR2 SUBVAR3 junk
do
echo"SUBVAR1->[${SUBVAR1}] SUBVAR2->[${SUBVAR2}] ...."
done
Hi vgersh99,
The VARIABLE IS COMING FROM CONFIG FILE AND AT ANY STAGE I DON'T KNOW HOW MANY VARIABLES I CAN DECLARE IN THE READ STATEMENT. THIS MUST BE DONE DYNAMICALLY.
JINGI
Pls don't shout 
#!/bin/ksh
nawk -F'|' '{ for(i=1; i <= NF; i++) printf("subvar%d=%s\n", i, $i)}' configFile
hi vgersh99,
Sorry if you felt that I was shouting. As you can see I have just registered yesterday. Your reply works but not as expected as I wanted.
Now I am revising my question:
VARIABLE1="some1|some2|some3" (Comes from config and this variable may increase in future)
VARIABLE2="some4|some5|some6" (Comes from config and this variable may increase in future)
There is a one to one relation between the above variables (if i have to add a variable in VARIABLE1 then I will also add a variable in VARIABLE2).
LOOP START { from i =0 ; i<= {something in this case 3 but I don't know how to get 3 dynamically); i++
1: SomeCommand on VARIABLE1 --> should return some1 as a VARIABLE subvar1
2: SomeCommand on VARIABLE2 ---> should return some4 as a VARIABLE subvar2
function1 (irrelevant here) ---> returns a Value = value1
function2 (irrelevant here) ---> returns a Value = value2
if both value 1 and value 2 are not same
then
exit
}LOOP END
When it runs first time 1: retunes some1(as a subvar1) and 2: returns some4(as a subvar2), subvar1 and subvar2 will be available to function1 and function2 which in turn return value1 and value2 if both are not same then exit else continue for 2nd RUN in the loop
second time : 1: retunes some2 (as a subvar1) and 2: returns some5(as a subvar2), subvar1 and subvar2 will be available to function1 and function2 which in turn return value1 and value2 if both are not same then exit else continue for 3rd RUN in the loop.
and so on.....
Please help. Thanks in Advance.
Jingi
assuming:
- file 'config1' contains: 'some1|some2|some3'
- file 'config2' contains: 'some4|some5|some6'
[not tested]
#!/bin/ksh
config1='config1'
config2='config2'
tr '|' '\n' < config1 > /tmp/$$_1
tr '|' '\n' < config2 > /tmp/$$_2
paste -d' ' /tmp/$$_1 /tmp/$$_2 | while read f1 f2
do
echo "f1->[${f1}] f2->[${f2}]"
if [ $(function1 "${f1}") != $(function2 "${f2}") ]; then
exit
fi;
done
\rm -rf /tmp/$$*
Hi vgersh99,
I had slightly modified the script but I had used your idea. Thanks for your help.
.
Thanks,
Jingi