LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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, 02: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, 02:53 AM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 3,879
Blog Entries: 1

Rep: Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998
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, 03:47 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,109

Rep: Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268
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, 04:48 AM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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 04:51 AM.
 
Old 08-22-2013, 05:04 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,109

Rep: Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268
in that case a simple mkdir -p <dir> is sufficient, it will make that check
 
Old 08-22-2013, 05:39 AM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 01:02 PM
New Operator ?¿ energiza Programming 1 04-02-2009 01:49 PM
What is the operator * ./. . . * royeo Linux - Newbie 2 09-03-2006 07:13 PM
C++ operator += uman Programming 1 02-20-2005 05:37 PM
~ operator linuxanswer Programming 7 04-08-2004 05:56 PM


All times are GMT -5. The time now is 06:45 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration