FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Debian > Debian User

 
 
LinkBack Thread Tools
 
Old 10-28-2008, 03:42 PM
Kent West
 
Default Q if test of command in bash script

Hey all!

After a couple of hours of searching, I've decided that both my
scripting-foo and my google-foo are weak this morning, so I turn to you
folks for a quick answer.

In a bash script, I'm trying to create a directory and test if it is
successful.

The man page doesn't indicate what mkdir returns as a successful code,
but I have a vague impression (being a non-programmer) that a return
code of zero is generally considered success and non-zero is generally
considered failure. (So man-page writer, hint hint ...)

Here's the relevant snippet of code I have so far:

===

echo "Does target directory '$targetDir' exist?"
if [ -d $targetDir ]
then
echo "YES, '$targetDir exists!"
echo
else
echo "No, '$targetDir' does not exist."
echo
echo "Attempting to create $targetDir"
echo
if [ mkdir -p $targetDir ] # If the mkdir fails
then
echo "Failed to create directory. Aborting."
exit 1
else
echo "Created $targetDir directory
successfully!"
fi
fi

===

When I run this, I get this output:

===

Does target directory '/TERASTATIONBACKUP/GOSHEN/2008/OCTOBER' exist?
No, '/TERASTATIONBACKUP/GOSHEN/2008/OCTOBER' does not exist.

Attempting to create /TERASTATIONBACKUP/GOSHEN/2008/OCTOBER

/usr/local/bin/BackupGoshenHome: line 57: [: -p: binary operator expected
Created /TERASTATIONBACKUP/GOSHEN/2008/OCTOBER directory successfully!
Tarring up source into target
/TERASTATIONBACKUP/GOSHEN/2008/OCTOBER/2008-Oct-28.tgz

===


I've tried various permutations of the test case - put it in
double-quotes, in single-quotes, put a bang in front of the text, remove
the brackets, add a semi-colon at the end of the test line before the
comment, etc, but can't figure out what I'm doing wrong.

Thanks for any help!

--
Kent West <*)))><
http://kentwest.blogspot.com


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 10-28-2008, 04:16 PM
Steve Kemp
 
Default Q if test of command in bash script

On Tue Oct 28, 2008 at 11:42:09 -0500, Kent West wrote:

> if [ mkdir -p $targetDir ] # If the mkdir fails

Use:

if ( mkdir -p $targetDir )


Steve
--
http://www.steve.org.uk/


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 10-28-2008, 05:26 PM
Kent West
 
Default Q if test of command in bash script

Chris Jones wrote:
> if mkdir; then
> echo 'created OK'
> else
> echo 'not created OK'
> exit 1
>
>
> ... the [] is redundant, IOW:
>
> if test mkdir # ???
>
> also your sample has the then/else actions inverted.
>

Thank you. I thought I had tried it without the brackets, but I must've
introduced some other little problem when I tried it 'cause it works now.

Steve Kemp suggested:

> if ( mkdir -p $targetDir )

Do the parenths add any value? (meaning? readability? it seems to me to
add readability)

Thanks!


--
Kent West <*)))><
http://kentwest.blogspot.com


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 10-28-2008, 05:36 PM
Ken Irving
 
Default Q if test of command in bash script

On Tue, Oct 28, 2008 at 11:42:09AM -0500, Kent West wrote:
> ===
>
> echo "Does target directory '$targetDir' exist?"
> if [ -d $targetDir ]
> then
> echo "YES, '$targetDir exists!"
> echo
> else
> echo "No, '$targetDir' does not exist."
> echo
> echo "Attempting to create $targetDir"
> echo
> if [ mkdir -p $targetDir ] # If the mkdir fails
> then
> echo "Failed to create directory. Aborting."
> exit 1
> else
> echo "Created $targetDir directory successfully!"
> fi
> fi
>
> ===
>
> When I run this, I get this output:
>
> ===
>
> Does target directory '/TERASTATIONBACKUP/GOSHEN/2008/OCTOBER' exist?
> No, '/TERASTATIONBACKUP/GOSHEN/2008/OCTOBER' does not exist.
>
> Attempting to create /TERASTATIONBACKUP/GOSHEN/2008/OCTOBER
>
> /usr/local/bin/BackupGoshenHome: line 57: [: -p: binary operator expected
> Created /TERASTATIONBACKUP/GOSHEN/2008/OCTOBER directory successfully!
> Tarring up source into target
> /TERASTATIONBACKUP/GOSHEN/2008/OCTOBER/2008-Oct-28.tgz
>
> ===

The complaint about line 57 is from the [ operator, which is expecting a
conditional expression and not a command. Since [ ... ] returns a false
value, the else is executed, whic incorrectly says that mkdir worked.

The [ ... ] construct isn't needed for a command like mkdir, just
for conditional tests of variables and strings (see CONDITIONAL
EXPRESSIONS in bash(1)).

I'd tend to format the above code a little differently, but here's
the same thing with the `if' using the mkdir command directly:

if [ -d $targetDir ]; then
echo -e "YES, '$targetDir exists!

"
else
echo -e "No, '$targetDir' does not exist.

"
echo -e "Attempting to create $targetDir

"
if mkdir -p $targetDir; then
echo "Created $targetDir directory successfully!"
else
echo "Failed to create directory. Aborting."
exit 1
fi
fi

> I've tried various permutations of the test case - put it in
> double-quotes, in single-quotes, put a bang in front of the text, remove
> the brackets, add a semi-colon at the end of the test line before the
> comment, etc, but can't figure out what I'm doing wrong.
>
> Thanks for any help!
>
> --
> Kent West <*)))><
> http://kentwest.blogspot.com

I sympathise with finding understandable documentation or man pages for
these things. Regarding return codes, they're often implicit and not
documented; often the term `exit status' and others might be used. Shell
commands always return an exit status if 0 -- meaning true -- if the command
doesn't fail, and usually 1 otherwise, but some other exit values are used.
See also EXIT STATUS in bash(1).

Ken

--
Ken Irving, fnkci@uaf.edu, 907-474-6152
Water and Environmental Research Center
Institute of Northern Engineering
University of Alaska, Fairbanks


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 10-28-2008, 05:56 PM
Kurian Thayil
 
Default Q if test of command in bash script

Kent West wrote:
> Chris Jones wrote:
>
>> if mkdir; then
>> echo 'created OK'
>> else
>> echo 'not created OK'
>> exit 1
>>
>>
>> ... the [] is redundant, IOW:
>>
>> if test mkdir # ???
>>
>> also your sample has the then/else actions inverted.
>>
>>
>
> Thank you. I thought I had tried it without the brackets, but I must've
> introduced some other little problem when I tried it 'cause it works now.
>
> Steve Kemp suggested:
>
>
>> if ( mkdir -p $targetDir )
>>
>
> Do the parenths add any value? (meaning? readability? it seems to me to
> add readability)
>
> Thanks!
>
>
>
Hi,

There is another way of solving this issue. Use the format below:

if [ `mkdir -p $targetDir` ]
then
................
.......
fi

Thanks.

Regards,

Kurian.


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 10-28-2008, 05:57 PM
Kent West
 
Default Q if test of command in bash script

Ken Irving wrote:
> The [ ... ] construct isn't needed for a command like mkdir, just
> for conditional tests of variables and strings (see CONDITIONAL
> EXPRESSIONS in bash(1)).
>

Ah, beginning to coalesce in my brain now.

> I'd tend to format the above code a little differently, but here's
> the same thing with the `if' using the mkdir command directly:
>
> if [ -d $targetDir ]; then
> echo -e "YES, '$targetDir exists!

"
>

Ah, the "-e" enables things like newlines and the bell character. No
wonder my bell character never sounded in another part of my script; I
just assumed it was something turned off in my terminal settings.

> if mkdir -p $targetDir; then
> echo "Created $targetDir directory successfully!"
> else
> echo "Failed to create directory. Aborting."
> exit 1
> fi
>

I at first liked the format

if test
then
statement 1
else
statement 2
fi

because I don't have to remember the semi-colon (when it's used; when
it's not, etc), and because it sort of conceptually separates the test
from the consequences of the test, but I'm beginning to swing around to
your format of

if test; then
statement 1
else
statement 2
fi

Is there any advantage/disadvantage to one format over the other?


> I sympathise with finding understandable documentation or man pages for
> these things. Regarding return codes, they're often implicit and not
> documented; often the term `exit status' and others might be used. Shell
> commands always return an exit status if 0 -- meaning true -- if the command
> doesn't fail, and usually 1 otherwise, but some other exit values are used.
> See also EXIT STATUS in bash(1).
>
>

Thanks!



--
Kent West <*)))><
http://kentwest.blogspot.com


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 10-28-2008, 06:28 PM
Ken Irving
 
Default Q if test of command in bash script

On Tue, Oct 28, 2008 at 01:57:40PM -0500, Kent West wrote:
> Ken Irving wrote:
> ...
> > I'd tend to format the above code a little differently, but here's
> > the same thing with the `if' using the mkdir command directly:
> >
> > if [ -d $targetDir ]; then
> > echo -e "YES, '$targetDir exists!

"
>
> Ah, the "-e" enables things like newlines and the bell character. No
> wonder my bell character never sounded in another part of my script; I
> just assumed it was something turned off in my terminal settings.

I use it a lot (but I think just needed one
above), and often without
quoting the whole thing, e.g.,

echo -e
leading blank line to separate output from previous lines

> ...
> I at first liked the format
>
> if test
> then
> statement 1
> else
> statement 2
> fi
>
> because I don't have to remember the semi-colon (when it's used; when
> it's not, etc), and because it sort of conceptually separates the test
> from the consequences of the test, but I'm beginning to swing around to
> your format of
>
> if test; then
> statement 1
> else
> statement 2
> fi
>
> Is there any advantage/disadvantage to one format over the other?

No difference, bash just uses the `;' to mean the same thing as a
newline. I just like that the latter form is a bit more concise looking
(to me, anyway).

If there's no else condition to worry about, the && and || operators
can be used to good effect, e.g.,

test -d $targetdir || {
test -n "$OK_TO_CREATE_DIR" || {
echo directory not found
exit 1
}
mkdir -p $targetdir || exit 1 # (mkdir itself emits a complaint)
# ASSERT: mkdir was succesful
}
# ASSERT: directory exists

Ken

--
Ken Irving, fnkci@uaf.edu, 907-474-6152
Water and Environmental Research Center
Institute of Northern Engineering
University of Alaska, Fairbanks


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 10-28-2008, 06:46 PM
"Boyd Stephen Smith Jr."
 
Default Q if test of command in bash script

On Tuesday 28 October 2008 02:28:16 pm Ken Irving wrote:
>On Tue, Oct 28, 2008 at 01:57:40PM -0500, Kent West wrote:
>> Ken Irving wrote:
>> > echo -e "YES, '$targetDir exists!

"
>>
>> Ah, the "-e" enables things like newlines and the bell character.
>
>I use it a lot (but I think just needed one
above), and often without
>quoting the whole thing, e.g.,
>
> echo -e
leading blank line to separate output from previous lines

Yeah, but echo may or may not support '-e' (or any options) depending on your
shell and utilities installed. If you need portable C-style escapes, use
printf.
--
Boyd Stephen Smith Jr. * * * * * * * * * * ,= ,-_-. =.
bss03@volumehost.net * * * * * * * * * * *((_/)o o(\_))
ICQ: 514984 YM/AIM: DaTwinkDaddy * * * * * `-'(. .)`-'
http://iguanasuicide.org/ * * * * * * * * * * *\_/ * *
 

Thread Tools




All times are GMT. The time now is 04:32 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org