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 > Ubuntu > Ubuntu User

 
 
LinkBack Thread Tools
 
Old 11-11-2009, 03:57 PM
Oliver Marshall
 
Default IF statement help

Hi chaps,


*


Can you help me with an if statement?


*


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


*** 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.


*


Whats the best way of writing that last elif statement?


*


Olly


*


--


G2 Support


Network Support : Online Backups : Server Management


*





*


Tel:*** 0845 307 3443


Email:* oliver.marshall@g2support.com


Web:*** http://www.g2support.com


Twitter: g2support


Newsletter: http://www.g2support.com/newsletter


Mail:** 2nd Floor, 130a Western Rd, Brighton,
Sussex, BN12LA


*


G2 Support LLP is registered
at Mill House, 103 Holmes Avenue, HOVE


BN3 7LE. Our registered
company number is OC316341.


*


*







--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 11-11-2009, 04:27 PM
matteo filippetto
 
Default IF statement help

2009/11/11 Oliver Marshall <Oliver.Marshall@g2support.com>
















Hi chaps,


*


Can you help me with an if statement?


*


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


*** 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.


*


Whats the best way of writing that last elif statement?


*


Olly


*


--


G2 Support


Network Support : Online Backups : Server Management


*





*


Tel:*** 0845 307 3443


Email:* oliver.marshall@g2support.com


Web:*** http://www.g2support.com


Twitter: g2support


Newsletter: http://www.g2support.com/newsletter


Mail:** 2nd Floor, 130a Western Rd, Brighton,
Sussex, BN12LA


*


G2 Support LLP is registered
at Mill House, 103 Holmes Avenue, HOVE


BN3 7LE. Our registered
company number is OC316341.


*


*








--

ubuntu-users mailing list

ubuntu-users@lists.ubuntu.com

Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users



Hi,

maybe you can

folder_source=""


if [ -d /$folder1_source/$1 ]; then


*************** folder_source=1


elif [ -d /$folder2_source/$1 ]; then


*************** folder_source=2


elif [ $folder_source=""* ] || [ $folder_source="1"* ] && [ $folder_source="2"* ]; then


*** exit


fi
Best regards,

--
Matteo Filippetto


--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 11-11-2009, 04:52 PM
Nils Kassube
 
Default IF statement help

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

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 11-13-2009, 04:42 AM
Matthew Flaschen
 
Default IF statement help

matteo filippetto wrote:
> Hi,
>
> maybe you can
>
> folder_source=""
>
> if [ -d /$folder1_source/$1 ]; then
>
> folder_source=1
>
> elif [ -d /$folder2_source/$1 ]; then
>
> folder_source=2
>
> elif [ $folder_source="" ] || [ $folder_source="1" ] && [
> $folder_source="2" ]; then

This is incorrect. First, either the if or the elif could execute but
never both. Even if they did both execute, folder_source obviously
can't equal two different values at the same time.

Nils's solution appears correct.

Matthew Flaschen

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 

Thread Tools




All times are GMT. The time now is 04:43 AM.

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