# While ...loop

**URL:** <https://community.unix.com/t/while-loop/322263>\
**Category:** Shell Programming and Scripting\
**Created:** [January 7, 2013, 4:22am UTC](https://community.unix.com/t/while-loop/322263 "2013-01-07T04:22:57Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![redologger](https://community.unix.com/letter_avatar/redologger/32/6_449c533f586939b6e5f246632a46421f.png) [@redologger](https://community.unix.com/u/redologger)\
**Post date:** [January 7, 2013, 4:22am UTC](https://community.unix.com/t/while-loop/322263/1 "2013-01-07T04:22:57Z")

</div>

```nohighlight
#!/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 .

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [January 7, 2013, 4:27am UTC](https://community.unix.com/t/while-loop/322263/2 "2013-01-07T04:27:31Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![rangarasan](https://community.unix.com/user_avatar/community.unix.com/rangarasan/32/2356_2.png) [@rangarasan](https://community.unix.com/u/rangarasan)\
**Post date:** [January 7, 2013, 4:29am UTC](https://community.unix.com/t/while-loop/322263/3 "2013-01-07T04:29:05Z")

</div>

Hey,

try this,

```nohighlight
..
while [ $c ...
..

```

Cheers,  
Ranga:)

---

<div class="post-metadata">

**Author:** ![redologger](https://community.unix.com/letter_avatar/redologger/32/6_449c533f586939b6e5f246632a46421f.png) [@redologger](https://community.unix.com/u/redologger)\
**Post date:** [January 7, 2013, 4:31am UTC](https://community.unix.com/t/while-loop/322263/4 "2013-01-07T04:31:25Z")

</div>

> [@scott](#):
>
> c is a variable, you need a $ before it.
> 
> ```plaintext
> while [ "$c" -le ....
> ...
> c=$((c + 1))
> ...
> done
> 
> ```

erm..i made the changes u recomanded still not able to get the value assign to SID\_1 , SID\_2 , SID\_3

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [January 7, 2013, 4:34am UTC](https://community.unix.com/t/while-loop/322263/5 "2013-01-07T04:34:54Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [January 7, 2013, 4:37am UTC](https://community.unix.com/t/while-loop/322263/6 "2013-01-07T04:37:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![redologger](https://community.unix.com/letter_avatar/redologger/32/6_449c533f586939b6e5f246632a46421f.png) [@redologger](https://community.unix.com/u/redologger)\
**Post date:** [January 8, 2013, 1:48am UTC](https://community.unix.com/t/while-loop/322263/7 "2013-01-08T01:48:05Z")

</div>

what happen if is:

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

```

seem like

```plaintext
eval echo "\$$BDUMP_DIR"

```

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

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

> [@redologger](#):
>
> what happen if is:
> 
> ```plaintext
> BDUMP_DIR=/home/John/admin/"${SID}_${c}"/bdump
> 
> ```
> 
> seem like
> 
> ```plaintext
> eval echo "\$$BDUMP_DIR"
> 
> ```
> 
> doesn't produce /home/john/admin/plune

is ok guys figure it out to be:

```plaintext
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

```plaintext
eval echo "\$$BDUMP_DIR"

$/home/john/admin/plune

```

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [January 8, 2013, 2:25am UTC](https://community.unix.com/t/while-loop/322263/8 "2013-01-08T02:25:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Fundix](https://community.unix.com/letter_avatar/fundix/32/6_449c533f586939b6e5f246632a46421f.png) [@Fundix](https://community.unix.com/u/Fundix)\
**Post date:** [January 8, 2013, 2:50am UTC](https://community.unix.com/t/while-loop/322263/9 "2013-01-08T02:50:14Z")

</div>

```nohighlight
while (( c <= $totalInstance ))
do
    BDUMP_DIR="${SID}_${c}"
    eval DIR="$"$BDUMP_DIR
    print $DIR
    c=$((c +=1))
    print "$c"
done

```

---

<div class="post-metadata">

**Author:** ![redologger](https://community.unix.com/letter_avatar/redologger/32/6_449c533f586939b6e5f246632a46421f.png) [@redologger](https://community.unix.com/u/redologger)\
**Post date:** [January 8, 2013, 3:10am UTC](https://community.unix.com/t/while-loop/322263/10 "2013-01-08T03:10:16Z")

</div>

```nohighlight
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

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [January 8, 2013, 4:08am UTC](https://community.unix.com/t/while-loop/322263/11 "2013-01-08T04:08:25Z")

</div>

You probably want something like this:

```nohighlight
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:

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![Fundix](https://community.unix.com/letter_avatar/fundix/32/6_449c533f586939b6e5f246632a46421f.png) [@Fundix](https://community.unix.com/u/Fundix)\
**Post date:** [January 8, 2013, 4:51am UTC](https://community.unix.com/t/while-loop/322263/12 "2013-01-08T04:51:28Z")

</div>

eval solution :

```nohighlight
#!/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

```

---

<div class="post-metadata">

**Author:** ![redologger](https://community.unix.com/letter_avatar/redologger/32/6_449c533f586939b6e5f246632a46421f.png) [@redologger](https://community.unix.com/u/redologger)\
**Post date:** [January 8, 2013, 4:56am UTC](https://community.unix.com/t/while-loop/322263/13 "2013-01-08T04:56:57Z")

</div>

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