User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 456,519 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,806 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 2009 | Replies: 2
Reply
Join Date: Aug 2007
Posts: 7
Reputation: arvindishukla is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
arvindishukla arvindishukla is offline Offline
Newbie Poster

How to use Logical OR operator in C- Shell

  #1  
Oct 4th, 2007
Hi All,

Small can some please let me know hpw to use logical OR in csh scripts, below is the code line which is not running and i don't know why it is entring in "if" statement, even though i'm inputing value "1",

set ans = $<
echo $ans
if ( $ans != 1 || $ans != "2" ) then
echo "Invalid option selected. Exixting from the from the script $0 ."
exit 0
endif

It goes in if clause and then exits from the script
Any suggestions pls .???
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Posts: 1,514
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is online now Online
Posting Virtuoso

Re: How to use Logical OR operator in C- Shell

  #2  
Oct 4th, 2007
Because 1 != 2. If you want it to enter the if statement only when the input is not 1 and not 2, then you need to use the logical and (&&), not the logical (or).

If you wanted to enter the loop only if the answer is either 1 or 2, then you would use the logical or. i.e.
if ( $ans == 1 || $ans == 2 ) then
but, when you invert the == to !=, then you must also invert the or to an and, because every number is either not equal to one or not equal to two, one and two included.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Aug 2007
Posts: 7
Reputation: arvindishukla is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
arvindishukla arvindishukla is offline Offline
Newbie Poster

Re: How to use Logical OR operator in C- Shell

  #3  
Oct 4th, 2007
Originally Posted by masijade View Post
Because 1 != 2. If you want it to enter the if statement only when the input is not 1 and not 2, then you need to use the logical and (&&), not the logical (or).

If you wanted to enter the loop only if the answer is either 1 or 2, then you would use the logical or. i.e.
if ( $ans == 1 || $ans == 2 ) then
but, when you invert the == to !=, then you must also invert the or to an and, because every number is either not equal to one or not equal to two, one and two included.



Thanks mate Its working now !!!
It's an logical error from part ..
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Shell Scripting Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 4:06 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC