IF statement help

Nils Kassube kassube at gmx.net
Wed Nov 11 17:52:59 GMT 2009


Oliver Marshall wrote:
> I have the following which checks for the existence of a folder ($1)
>  in two locations ($folder1_source and $folder2_source). First it
>  checks in the $folder1_source location then in the $ folder2_source
>  location. If it finds it in neither then the $folder_source variable
>  remains empty and the script quits.
> 
> ****************
> folder_source=""
> if [ -d /$folder1_source/$1 ]; then
>                 folder_source=1
> elif [ -d /$folder2_source/$1 ]; then
>                 folder_source=2
> elif [ $folder_source="" ]; then

What do you want to achieve with this statement? Did you perhaps mean

elif [ $folder_source = "" ]; then

instead?

>     exit
> fi
> *****************

> I want to add one more check which is that if the directory specified
>  by the $1 variable is found in BOTH the /$folder1_source AND
>  /$folder2_source locations then I want the script to fail.

I would change the entire block like this:

test -d "/$folder1_source/$1" -a -d "/$folder2_source/$1" && exit 1
test ! -d "/$folder1_source/$1" -a ! -d "/$folder2_source/$1" && exit 2
test -d "/$folder1_source/$1" && folder_source=1
test -d "/$folder2_source/$1" &&  folder_source=2

> Whats the best way of writing that last elif statement?

There is no _best_ way. :)


Nils




More information about the ubuntu-users mailing list