Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...this web site is a 'Godsend' for me. If I have a programming problem that I'm unable to solve, I'll get a sensible reply in no time. It's really great!..."

Geography

Where in the world do Tek-Tips members come from?
w5000 (TechnicalUser)
3 Apr 12 11:10
Hello,

what is wrong in selected in blue failed  then selected in red was executed?

I was expecting tht selected in red will only be executed when [ ! -f $a ] is false /due to ()/.

CODE

$ a=tmp
$ [ ! -d $a ] && ( [ ! -f $a ] && ( echo "$a :: no such dir, so doing it";mkdir $a ) || echo "$a :: is a file, can't do dir" ) || echo "$a :: such dir already exists"
tmp :: no such dir, so doing it
$ [ ! -d $a ] && ( [ ! -f $a ] && ( echo "$a :: no such dir, so doing it";mkdir $a ) || echo "$a :: is a file, can't do dir" ) || echo "$a :: such dir already exists"
tmp :: such dir already exists
$ rmdir $a
$ touch $a
$ [ ! -d $a ] && ( [ ! -f $a ] && ( echo "$a :: no such dir, so doing it";mkdir $a ) || echo "$a :: is a file, can't do dir" ) || echo "$a :: such dir already exists"
tmp :: is a file, can't do dir
$ ls /test
ls: 0653-341 The file /test does not exist.
$ a=/test
$ [ ! -d $a ] && ( [ ! -f $a ] && ( echo "$a :: no such dir, so doing it";mkdir $a ) || echo "$a :: is a file, can't do dir" ) || echo "$a :: such dir already exists"
/test :: no such dir, so doing it
mkdir: 0653-357 Cannot access directory /.
/: The file access permissions do not allow the specified action.
/test :: is a file, can't do dir
$ su
root's Password:
$ mkdir /test
$ ls -ld /test
drwxr-xr-x    2 root     system          256 Apr 03 14:41 /test
$ exit
$ [ ! -d $a ] && ( [ ! -f $a ] && ( echo "$a :: no such dir, so doing it";mkdir $a ) || echo "$a :: is a file, can't do dir" ) || echo "$a :: such dir already exists"
/test :: such dir already exists
$ su
root's Password:
$ rmdir /test
$ touch /test
$ exit
$ [ ! -d $a ] && ( [ ! -f $a ] && ( echo "$a :: no such dir, so doing it";mkdir $a ) || echo "$a :: is a file, can't do dir" ) || echo "$a :: such dir already exists"
/test :: is a file, can't do dir
$

Is a good solution adding "return 0" after the mkdir command? Or maybe other () sets would do the trick?

CODE

$ [ ! -d $a ] && ( [ ! -f $a ] && ( echo "$a :: no such dir, so doing it";mkdir $a 2>/dev/null ) || echo "$a :: is a file, can't do dir" ) || echo "$a :: such dir already exists"
/test :: no such dir, so doing it
/test :: is file, can't do dir
$ [ ! -d $a ] && ( [ ! -f $a ] && ( echo "$a :: no such dir, so doing it";mkdir $a 2>/dev/null;return 0 ) || echo "$a :: is a file, can't do dir" ) || echo "$a :: such dir already exists"
/test :: no such dir, so doing it
$ [ ! -d $a ] && ( [ ! -f $a ] && ( echo "$a :: no such dir, so doing it";mkdir $a 2>/dev/null;[ $? -ne 0 ] && echo mkdir failed;return 0 ) || echo "$a :: is a file, can't do dir" ) || echo "$a :: such dir a>
/test :: no such dir, so doing it
mkdir failed
$

When doing it with if, only mkdir error is displayed (as expected):

CODE

$ if [ ! -d $a ];then if [ ! -f $a ];then echo "$a :: no such dir, so doing it";mkdir $a;else echo "$a :: is file, can't do dir";fi;else echo "$a :: such dir already exists";fi
/test :: no such dir, so doing it
mkdir: 0653-357 Cannot access directory /.
/: The file access permissions do not allow the specified action.
$
Annihilannic (MIS)
3 Apr 12 20:37
Yes, return 0 would work around it, however I'd advise sticking with if conditionals once your logic starts getting complex, otherwise, as you have found, debugging can become quite cumbersome.  When you use || it will just keep running commands in the statement until one of them returns 0.

Annihilannic
tgmlify - code syntax highlighting for your tek-tips posts

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close