Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

statement

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-08-2007   -   Original Discussion by Ernst
Ernst Ernst is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 17 February 2012, 11:44 AM EST
Posts: 89
Thanks: 3
Thanked 0 Times in 0 Posts
statement

I want to write a program that would convert yard to feet and feet to yard.
i.e
1 yard = 3 * feet
Echo "enter a"
read a
expr a *3

The trick is that I want to give the user some options. After the conversion from yard to feet is done, I want to ask the user whether or not he/she wants to also convert from feet to yard. If the user says yes, then the script should be executed and the conversion from feet to yard done. But the user says no, the program should stop and exit out.

I need some ideas.
Sponsored Links
    #2  
Old Unix and Linux 03-08-2007   -   Original Discussion by Ernst
cfajohnson's Unix or Linux Image
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 11 July 2016, 2:55 PM EDT
Location: Toronto, Canada
Posts: 2,898
Thanks: 0
Thanked 134 Times in 118 Posts
Quote:
Originally Posted by Ernst
I want to write a program that would convert yard to feet and feet to yard.
i.e
1 yard = 3 * feet
Echo "enter a"
read a
expr a *3

The trick is that I want to give the user some options. After the conversion from yard to feet is done, I want to ask the user whether or not he/she wants to also convert from feet to yard. If the user says yes, then the script should be executed and the conversion from feet to yard done. But the user says no, the program should stop and exit out.

I need some ideas.
This sounds like homework. If it is, you should make an effort to write the script. If you then need help, post what you have written, and ask a more detailed question concerning your problem.

If it isn't homework, you should still make the effort.
Sponsored Links
    #3  
Old Unix and Linux 03-09-2007   -   Original Discussion by Ernst
ahmedwaseem2000 ahmedwaseem2000 is offline
Registered User
 
Join Date: Aug 2005
Last Activity: 16 July 2017, 12:05 PM EDT
Location: Bangalore
Posts: 306
Thanks: 10
Thanked 2 Times in 2 Posts
Well, If you are not aware of this then you must read this link
    #4  
Old Unix and Linux 03-09-2007   -   Original Discussion by Ernst
Ernst Ernst is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 17 February 2012, 11:44 AM EST
Posts: 89
Thanks: 3
Thanked 0 Times in 0 Posts
Statement

Actually, It is not a homework. I left school a long time ago Linux. Anyways, I am just trying to develop some unix skills especially when it comes to scripting. I am writing simple programs on my own, but am asking for some help after many trials and I cannot get it to work.
If you think it is too easy for me to request the forum's input. I apologize for my ignorance.

I am guessing I need to use a maybe "if statement", but I am not sure. I have tried (if,then,elseif,fi) to no avail. I get many error messages.
Sponsored Links
    #5  
Old Unix and Linux 03-09-2007   -   Original Discussion by Ernst
Ernst Ernst is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 17 February 2012, 11:44 AM EST
Posts: 89
Thanks: 3
Thanked 0 Times in 0 Posts
I am looking to write a statement that allow me to contimue when the user enters Yes and exit the program if the user enters no. Let me know whta you guys think. what is wrong with my if statement?




cat conversion
#! /usr/bin/ksh
echo "This program is designed to do measurement conversion"
echo "Enter yard"
read yard
echo "The result in feet is:"
expr $yard '*' 3
echo "Do you want to convert from feet to yard as well?"
echo "Please enter your answer"
echo "please enter yes to continue or no to exit:"
read
if
($answer = yes) &&

echo "You have entered yes:"
then
echo "please enter feet"
read feet
echo "The result in yard is:"
expr $feet / 3
else
clear
fi
Sponsored Links
    #6  
Old Unix and Linux 03-09-2007   -   Original Discussion by Ernst
cfajohnson's Unix or Linux Image
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 11 July 2016, 2:55 PM EDT
Location: Toronto, Canada
Posts: 2,898
Thanks: 0
Thanked 134 Times in 118 Posts
What you need is a while loop. I'd write it so that it is an infinite loop, with a break statement to exit the loop when the user is done. Here is the basic form:


Code:
while true
do
  # your main routine goes here

  printf "%s " "Another conversion?"
  read continue
  case $continue in
    [yY]*) ;;
    *) printf "%s\n" "Good-bye"
        break
        ;;
  esac
done


Last edited by cfajohnson; 03-09-2007 at 02:07 PM.. Reason: To trim the quoting
Sponsored Links
    #7  
Old Unix and Linux 03-09-2007   -   Original Discussion by Ernst
Ernst Ernst is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 17 February 2012, 11:44 AM EST
Posts: 89
Thanks: 3
Thanked 0 Times in 0 Posts
statement

I am still having the same issue. Whether I enter yes or no, the program prompts me to enter feet without exiting. I want the program to exit when the user enters no. What is still missing?

#! /usr/bin/ksh
echo "This program is designed to do measurement conversion"
echo "Enter yard"
read yard
echo "The result in feet is:"
expr $yard '*' 3
echo "Do you want to convert from feet to yard as well?"
echo "Please enter your answer"
echo "please enter yes to continue or no to exit:"
read
while true
do
printf "%s " " Enter feet?"
read feet
echo "The result in yard is:"
expr $feet / 3
read continue
case $continue in
[yy]*) ;;
*) printf "%s\n" "Good-bye"
break
;;
esac
done


"OUTPUT"
This program is designed to do measurement conversion
Enter yard
125468
The result in feet is:
376404
Do you want to convert from feet to yard as well?
Please enter your answer
please enter yes to continue or no to exit:
no
Enter feet?
The result in yard is:
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help with for statement within while statement bbbngowc Shell Programming and Scripting 1 05-07-2011 05:31 PM
IF statement Great Uncle Kip UNIX for Dummies Questions & Answers 4 04-09-2009 08:17 AM
How is use sselect statement o/p in insert statement. nkosaraju Shell Programming and Scripting 2 08-06-2008 10:26 PM
If statement - How to write a null statement april Shell Programming and Scripting 3 04-16-2008 02:14 PM
if statement in ksh gfhgfnhhn UNIX for Dummies Questions & Answers 3 10-20-2006 10:37 AM



All times are GMT -4. The time now is 02:33 AM.