[Techtalk] once again frustrated by bash :(

dominik.schramm at gmxpro.net dominik.schramm at gmxpro.net
Tue Mar 9 23:53:06 EST 2004


Hi,

Angelina Carlton <brat at magma.ca> writes:

> I cannot see how to test if an argument is a directory or a file. 
> [...]
> 
> #!/bin/bash
>
>
> if [ -d = "$1" ] ;

"[" is actually the executable (on my Debian system a hard link to) 
"test". The man page for test will give you the correct syntax for 
parameters like -d:
It is

  -d filename

without "=".

So we get:

if [ -d "$1" ];

> then
>
> echo "$1 is not a directory"

echo "$1 **is** a directory"

Remember that in shell programming the exit code 0
means "true":
If $1 is a directory, then [ -d "$1" ] is true,
or in shell terms: the exit code of [ ... ] is 0.
As usual the "then" part of the if clause is executed if the 
expression between if and then is true.

> else
>
> echo "$1 is a directory"

echo "$1 is **not** a directory"

> fi

Hope this helps you figure out the rest.
regards,
dominik



More information about the Techtalk mailing list