While ...loop

#!/usr/bin/ksh

totalInstance=3
c=1
SID=SID
SID_1=BOYISH
SID_2=EAGALE
SID_3=PLUNE

while [ c -le $totalInstance ]
do
    BDUMP_DIR="${SID}_${c}"
    echo "$BDUMP_DIR"

    c=`expr $c + 1`
    echo "$c"
done

having problem printing the value of my var SID_1, SID_2, SID_3

instead the output printed out is just
SID_1
SID_2
SID_3

i need the value assign to SID_1..3 .

c is a variable, you need a $ before it.

while [ "$c" -le ....
 ...
  c=$((c + 1))
...
done

Hey,

try this,

..
while [ $c ...
..

Cheers,
Ranga:)

erm..i made the changes u recomanded still not able to get the value assign to SID_1 , SID_2 , SID_3

while [ $c -le $totalInstance ]
do
  BDUMP_DIR="${SID}_${c}"
  eval echo "\$$BDUMP_DIR"
  c=$((c + 1))
  echo "$c"
done
1 Like

You need either sth corresponding to bash indirection (don't know in ksh - ${!vname}?) or use eval to assign to BDUMP_DIR.

what happen if is:

BDUMP_DIR=/home/John/admin/"${SID}_${c}"/bdump

seem like

eval echo "\$$BDUMP_DIR"

doesn't produce /home/john/admin/plune

---------- Post updated at 09:31 PM ---------- Previous update was at 09:15 PM ----------

is ok guys figure it out to be:

BDUMP_DIR=/home/John/admin/"\$${SID}_${c}"/bdump

:b:

---------- Post updated 01-08-13 at 01:48 AM ---------- Previous update was 01-07-13 at 09:31 PM ----------

but i can't figure out why there is a $ infront when i

eval echo "\$$BDUMP_DIR"

$/home/john/admin/plune

You don't need \$ with the eval command, since you already have it in the variable declaration.

while (( c <= $totalInstance ))
do
    BDUMP_DIR="${SID}_${c}"
    eval DIR="$"$BDUMP_DIR
    print $DIR
    c=$((c +=1))
    print "$c"
done
totalInstance=1
c=1
SID=SID
SID_1=CERUAT
#SID_2=LAUAT
#SID_3=TATSUAT

while [ "$c" -le $totalInstance ]
do
    eval BDUMP_DIR=/opt/ora10g/admin/"\$${SID}_${c}"/bdump
    eval UDUMP_DIR=/opt/ora10g/admin/"\$${SID}_${c}"/udump
    eval ADUMP_DIR=/opt/ora10g/admin/"\$${SID}_${c}"/adump
    
    #eval echo "\$${SID}_${c}"
    
    #echo $BDUMP_DIR
    #echo $UDUMP_DIR
    #echo $ADUMP_DIR

    
    /usr/bin/mv $BDUMP_DIR/alert_"\$${SID}_${c}".log  $BDUMP_DIR/alert_"\$${SID}_${c}"_`date +%d%b%Y`.bak

bear with with me.

i am able to print the path of $BDUMP.

but got problem moving them.

this -> "\$${SID}_${c}" cannot be recognise .

i need to print the path of $BDUMP/alert_CERUAT.log

then rename them as $BDUMP/alert_CERUAT<%d%b%Y>.bak

You probably want something like this:

totalInstance=1
c=1
SID_1=CERUAT
SID_2=LAUAT
SID_3=TATSUAT
W=$(date '+%d%b%Y')
while [ $c -le $totalInstance ]
do
  eval D=\${SID_$c}
  BDUMP_DIR=/opt/ora10g/admin/$D/bdump
  UDUMP_DIR=/opt/ora10g/admin/$D/udump
  ADUMP_DIR=/opt/ora10g/admin/$D/adump
  echo /usr/bin/mv $BDUMP_DIR/alert_$D.log $BDUMP_DIR/alert_${D}_$W.bak
  c=$((c + 1))
done

(remove the "echo" if the output looks OK).

But to avoid using eval, why not just:

P=/opt/ora10g/admin
W=$(date '+%d%b%Y')
for I in CERUAT LAUAT TATSUAT; do
  for T in bdump udump adump; do
    echo mv $P/$I/$T/alert_$I.log $P/$I/$T/alert_${I}_$W.bak
  done
done
1 Like

eval solution :

#!/usr/bin/ksh

totalInstance=1
c=1
SID=SID
SID_1=CERUAT

while (( c <= $totalInstance ))
do
    var1="${SID}_${c}"
    eval var2="$"$var1
    BDUMP_DIR=/opt/ora10g/admin/"${var2}"/bdump
    print $BDUMP_DIR

    mv $BDUMP_DIR/alert_${var2}.log  $BDUMP_DIR/alert_${var2}_`date +%d%b%Y`.bak
    c=$((c +=1))
done
1 Like

tks scott. learn quite abit. very helpful. I understand now.