# multiple while loops in expect script

**URL:** <https://community.unix.com/t/multiple-while-loops-in-expect-script/303823>\
**Category:** Shell Programming and Scripting\
**Created:** [February 1, 2012, 4:11pm UTC](https://community.unix.com/t/multiple-while-loops-in-expect-script/303823 "2012-02-01T16:11:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cic](https://community.unix.com/letter_avatar/cic/32/6_22734026db12803ebb3e059622ac3534.png) [@cic](https://community.unix.com/u/cic)\
**Post date:** [February 1, 2012, 4:11pm UTC](https://community.unix.com/t/multiple-while-loops-in-expect-script/303823/1 "2012-02-01T16:11:30Z")

</div>

Hi,

I am trying to incorporate multiple while loops into an expect script written in ksh shell. This is on a Solaris 10 system. Here is the code:

```nohighlight
#!/bin/ksh
  EXPECT=/usr/local/bin/expect
  exp_internal
   
  i=1
  h=0
  while [[$i -le 9 && $h -le 23]]; do
   
  $EXPECT << DONE
  set stty_init raw
   
  spawn database_connect
  expect ">"
  send "insert into DB1 \(account, sum, DATETIME_
  ID\) values (\'BG0$i\', 100, \'2012-02-01 0$h:30:00\'\)\r"
  expect eof
  close $fp
  DONE
  ((i+=1))
  ((h+=1))
  done

```

So far the script loops with the 9 values for variable $i as desired.  
However, not all values in range are looped for $h.  
The while for $i runs to completion and increments $h with each iteration  
Here is output of script so far:

./db.ksh[3]: exp\_internal: not found  
spawn database\_connect  
(db)\> insert into DB1 (account, sum, DATETIME\_ID) values ('BG01', 100, '2012-02-01 00:30:00')^M1 row(s) inserted  
Execution time: 2.852 seconds

(db)\> spawn database\_connect

(db)\> insert into DB1 (account, sum, DATETIME\_ID) values ('BG02', 100, '2012-02-01 01:30:00')  
:  
:

I would like all instances of variable $h to be applied to $i while first loop runs:  
BG01 - 00:30  
BG01 - 01:30  
BG01 - 23:30  
:  
:  
BG09 - 23:30

Any ideas on where I can improve my while loops to accomplish?

---

<div class="post-metadata">

**Author:** ![cic](https://community.unix.com/letter_avatar/cic/32/6_22734026db12803ebb3e059622ac3534.png) [@cic](https://community.unix.com/u/cic)\
**Post date:** [February 16, 2012, 12:37pm UTC](https://community.unix.com/t/multiple-while-loops-in-expect-script/303823/2 "2012-02-16T12:37:56Z")

</div>

problem was not initialising variable after iteration at bottom of script:

```nohighlight
DONE
  ((i+=1)) ((h+=1)) done
i=1
h=1

```

[/CODE]
