LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-27-2007, 03:18 PM   #1
tostay2003
Member
 
Registered: Jun 2006
Posts: 126

Rep: Reputation: 15
conditional execution


I want to execute two statements if my variable _Validity is false if true none of them should be executed.

But the exit 1 in below code is executed regardless of value in _Validity. Echo statement works appropriately.

Code:
$_Validity=/sbin/false
$_Validity || echo "Invalid variable"; exit 1
I have tried to enclose these two statments in braces

Code:
$_Validity ||  { echo "Invalid variable"; exit 1 }
but I get the following error
Quote:
{ Not found
FYI : I am using ksh
 
Old 12-27-2007, 04:25 PM   #2
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by tostay2003 View Post
Code:
$_Validity=/sbin/false
$_Validity || echo "Invalid variable"; exit 1
That semi-colon before "exit 1" tells the interpreter that it is the end of the command. So exit 1 will alway be executed unless someone else exits.

You could try
Code:
$_Validity=/sbin/false
$_Validity || echo "Invalid variable" && exit 1
but I'm not sure how the operator precedence works out.
 
Old 12-27-2007, 10:17 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by tostay2003 View Post

Code:
$_Validity=/sbin/false
$_Validity || echo "Invalid variable"; exit 1
keep it simple
Code:
if [ $_Validity equal something ];then
  ...do something
else
  .... do other things
  exit
fi
 
Old 12-28-2007, 07:45 AM   #4
tostay2003
Member
 
Registered: Jun 2006
Posts: 126

Original Poster
Rep: Reputation: 15
David 1357
Quote:
$_Validity || echo "Invalid variable" && exit 1
This statement is behaving like if ... else i.e. if true then exit 1, if false then execute the echo statement.

ghostdog74
Quote:
keep it simple

Code:
if [ $_Validity equal something ];then
...do something
else
.... do other things
exit
fi
In addition to making the scripts work. I am trying to learn scripting as well. That is the reason for using that sort of things. I read somewhere that if I put those in braces it will work , but its giving an error.
 
Old 12-28-2007, 10:22 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by tostay2003 View Post
I am trying to learn scripting as well.
yes, and learn it the proper way. Learn to make your code more readable and scalable. There's nothing wrong writing it that way , however if you are to add in more conditions later you might have a problem. Anyway, its up to you.
 
Old 12-28-2007, 03:01 PM   #6
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by tostay2003 View Post
This statement is behaving like if ... else i.e. if true then exit 1, if false then execute the echo statement.
That is what I meant about not knowing how the operator precedence would work out.

Reading the man page for ksh says that
Code:
_Validity=/bin/false
$_Validity ||  { echo "Invalid variable"; exit 1 ;}
should work. I just tested it here. You have to put the trailing "}" right after a ";".
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use conditional #define? kranti Programming 3 09-20-2006 09:06 AM
Conditional Compilation oulevon Programming 3 02-26-2006 08:21 PM
CASE conditional la_mafia63 Linux - General 3 07-24-2005 01:02 PM
c compound conditional steve_96 Programming 9 06-04-2005 07:17 PM
OS Conditional Defines kevin.cowley Programming 1 09-24-2003 03:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration