Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with if 1

Status
Not open for further replies.

johngiggs

Technical User
Oct 30, 2002
492
0
0
US
I have started working on the script below, but I cannot remember how to direct the script to go from one point back to another point. I.E. If the user does not respond to a question and just presses enter or enters a query that is not found, I would like the question to be re-asked until a valid response is entered.

#!/usr/bin/ksh

cd ~jxg4

echo " "
echo "Enter the description that you would like to search for: "
read desc

if [ "$desc" ]
then
awk -f TNG-desc.awk workloadjobs.out | sed 's/Description: //g' | grep $desc > workloadsdesc.rpt
else
echo "Enter the description that you would like to search for: "
read desc
fi

if [ $? -ne 0 ]
then

echo "\nNo match was found. Please try again.\n"
echo "Enter the description that you would like to search for: "
read desc
else
cat desc.header
cat workloadsdesc.rpt

fi

#######################

Any help would be greatly appreciated.

Thanks,

John
 
while true
do
echo " 1. xxxxxx"
echo " 2. xxxxxx"
echo " 3. xxxxxx"
echo " 4. xxxxxx"
echo "99. Exit"

read RPLY

case $RPLY in
1) command1
;;
2) command2
;;
3) command3
;;
4) command4
;;
99) exit
;;
*) echo "\nInvalid choice\n"
;;
esac
done

--
| Mike Nixon
| Unix Admin
|
----------------------------
 
Try something like this:
Code:
#!/usr/bin/ksh
cd ~jxg4
while echo "\nEnter the description that you would like to search for: "
do
  read desc
  [ "$desc" ] || continue
  awk -f TNG-desc.awk workloadjobs.out |
   sed 's/Description: //g' | grep $desc > workloadsdesc.rpt
  [ -s workloadsdesc.rpt ] && 
    cat desc.header workloadsdesc.rpt && break
  echo "\nNo match was found.  Please try again."
done

Hope This Help
PH.
 
Mike,

Thanks, but isn't there a way to do it similar to what I explained above? I think it's something like return or something like that which will bring you to different points of the script. I'm not sure if I need brackets at certain points or how it works. I know it was mentioned in one of my books, but I don't have any of them with me.

John

[afro]
 
Thanks, PHV!! That works great. For my own knowledge, can someone please explain how to do what I explained initially?

Thanks,

John

[afro]
 
You have no goto on ksh, but while ... do ... done or do ... until loop.
Do a
Code:
 man ksh
and pay attention to the
Code:
 while
instruction and to
Code:
 break
and
Code:
 continue
.

Hope This Help
PH.
 
PHV,

Maybe I'm thinking of csh because I already tried goto, so that must be where I'm getting confused. Thanks again for your help.

John

[afro]
 
I believe you were thinking of a function.

Example:

#!/usr/bin/ksh

#### Functions #####
Search ()
{
echo " "
echo "Enter the description that you would like to search for: "
read desc
if [ "$desc" ]
then
awk -f TNG-desc.awk workloadjobs.out | sed 's/Description: //g' | grep $desc > workloadsdesc.rpt
else
echo echo "\nNo match was found. Please try again.\n"
Search
fi

if [ $? -ne 0 ]
then
echo "\nNo match was found. Please try again.\n"
Search
else
cat desc.header
cat workloadsdesc.rpt
fi
}

#####################

# Body of code
# call to function
cd ~jxg4
Search
.
.
#end of code
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top