Alternative for goto

 
#!/bin/sh
label:
echo sql poll
 v=`sqlplus -s <<!
 HR/HR
 set pages 0 echo off feed off
 select distinct status from
 emp
 where
 id=5;
 !
 `
echo $v;
echo it comes here after false
if [ "$v" -eq 4 ]
then
echo if condition true
sqlplus -l scott/tiger <<EOF
select * from department;
EXIT
EOF
else
   sleep 30;
   goto label
fi

If the condition fails in the above script then it needs to sleep for 30 seconds then go to label and poll the database tabel. I read there is no goto in bourne shell so i tried to use other options like funtions but i could not finish this task. can someone help me to fix this. I tried like below . I am a absolute beginner in shell script so couldnot catch the concepts well.Thanks for your help in advance

 
#!/bin/sh
start(){
echo sql poll
 v=`sqlplus -s <<!
 HR/HR
 set pages 0 echo off feed off
 select distinct status from
 emp
 where
 id=5;
 !
 `
echo $v;
}
rval=start
if [ "$rval" -eq 4 ]
then
echo if condition true
sqlplus -l scott/tiger <<EOF
select * from department;
EXIT
EOF
else
   sleep 30;
   start
fi

Instead of a goto you could use something like this:

start() {
.....
}

while :
do
  rval=`start`
  if [ "$rval" -eq 4 ]
  then
    echo if condition true
    sqlplus -l scott/tiger <<EOF
    select * from department;
    EXIT
EOF
    break
  else
     sleep 30
  fi
done

In your start function the second ! should be at the first character position on the line or it will not work..

Are you sure it is Bourne Shell, are you on Solaris, otherwise /bin/sh is likely a POSIX shell which allows or a less restrictive syntax.

1 Like

Thanks a lot. It worked great.