Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple tests

Status
Not open for further replies.

w5000

Technical User
Nov 24, 2010
223
PL
Hello,

what is wrong in selected in blue[/color blue] failed then selected in red[/color red] was executed?

I was expecting tht selected in red[/color 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 ] && [COLOR=blue]( echo "$a :: no such dir, so doing it";mkdir $a )[/color blue] || [COLOR=red]echo "$a :: is a file, can't do dir"[/color red] ) || echo "$a :: such dir already exists"
[COLOR=blue]/test :: no such dir, so doing it[/color blue]
mkdir: 0653-357 Cannot access directory /.
/: The file access permissions do not allow the specified action.
[COLOR=red]/test :: is a file, can't do dir[/color red]
$ 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.
$
 
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
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top