LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-22-2013, 01:42 AM   #1
vicky007aggrwal
Member
 
Registered: Aug 2012
Posts: 95

Rep: Reputation: Disabled
Smile AND OR operator


I would like to use multiple commands in below command

[ $? == 0 ] && echo "directory exits" || echo "no Directory";mkdir -p /amt

But using multiple commands in && and || is somehow not working as expected
Can somebody pls help me in understand the syntactical issue in above command.

If there is any web link available for this, that will be great help

Thanks in advance
 
Old 08-22-2013, 01:53 AM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
It is probably working as you told it to, but you did not tell it correctly...

You do not say how it is failing so I will guess and say that it always attempts to create the directory...

Code:
[ $? == 0 ] && echo "directory exits" || echo "no Directory";mkdir -p /amt
If you intend for the OR clause to echo then make the directory, that is not what you are telling it. The trailing ';' ends the logical && ... || statement and the mkdir always executes.

What you need is...

Code:
[ $? == 0 ] && echo "directory exits" || echo "no Directory" && $(mkdir -p /amt)
If that is not what you need, please tell us how it is not working for you.
 
1 members found this post helpful.
Old 08-22-2013, 02:47 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,949

Rep: Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326
just group them:
Code:
like this
[[ <some test> ]] && ( <true case> ) || ( <false case> )

in your case:
[[ $? -eq 0 ]] && echo "directory exits" || ( echo "no Directory";mkdir -p /amt )

use -eq to integer comparison
 
Old 08-22-2013, 03:48 AM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
and since $? is just the exit code of last command

Code:
[[ $(SomeComand) ]] && echo "directory exits" || ( echo "no Directory";mkdir -p /amt )
BUT, guessing based on context

Code:
[[ -d "/path/to/someDir" ]] && echo "directory exits" || ( echo "no Directory";mkdir -p /amt )
could also use test -d "/path/to/someDir"



http://www.tldp.org/LDP/Bash-Beginners-Guide/html/
http://www.tldp.org/LDP/abs/html/

Last edited by Firerat; 08-22-2013 at 03:51 AM.
 
Old 08-22-2013, 04:04 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,949

Rep: Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326
in that case a simple mkdir -p <dir> is sufficient, it will make that check
 
Old 08-22-2013, 04:39 AM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
true, but quietly

mkdir -vp

will tell you when it makes one, and say nothing otherwise

Code:
mkdir -vp amt;echo $?
mkdir: created directory ‘amt’
0
mkdir -vp amt;echo $?
0
 
  


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
[SOLVED] C++ Operator Overloading Within an Already Overloaded Operator mirlin510 Programming 8 04-17-2011 12:02 PM
New Operator ?¿ energiza Programming 1 04-02-2009 12:49 PM
What is the operator * ./. . . * royeo Linux - Newbie 2 09-03-2006 06:13 PM
C++ operator += uman Programming 1 02-20-2005 04:37 PM
~ operator linuxanswer Programming 7 04-08-2004 04:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:42 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