The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
If statement - How to write a null statement april Shell Programming and Scripting 3 04-16-2008 01:14 PM
while read loop w/ a nested if statement - doesn't treat each entry individually littlefrog Shell Programming and Scripting 7 12-11-2007 09:49 PM
For loop statement - catch error lumdev Shell Programming and Scripting 4 09-20-2007 07:50 AM
if statement in for loop of a string Sniper Pixie UNIX for Dummies Questions & Answers 7 03-02-2006 07:28 AM
how to get the similar function in while loop or for loop trynew Shell Programming and Scripting 3 06-17-2002 11:09 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-07-2006
bobo bobo is offline
Registered User
  
 

Join Date: Jan 2006
Posts: 148
Unhappy if statement in a while loop

#!/usr/bin/ksh
echo Please enter
while read n
do
echo $n >> datafile
done

question:

How can I enject an if statement that if the users enter 0 (zero) the program will exit?

this is what I have but not working

#!/usr/bin/ksh
echo Please enter number
while read n
do
if $n=0
then
exit
else
echo $n >> datafile
fi
done


Thanks!
  #2 (permalink)  
Old 11-07-2006
tmarikle tmarikle is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2005
Posts: 683
Read up on the test command (man test)
Code:
if [[ $n -eq 0 ]]
then
    exit
fi
  #3 (permalink)  
Old 11-07-2006
BOFH BOFH is offline Forum Advisor  
Registered User
  
 

Join Date: Feb 2005
Location: Broomfield, CO
Posts: 406
You have to use the test construct in the if statement:

Code:
#!/usr/bin/ksh

echo Please enter number

while read n
do
  if [ $n=0 ]
  then
    exit
  else
    echo $n >> datafile
  fi
done
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 08:17 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0