LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-27-2011, 09:15 AM   #1
amario26
LQ Newbie
 
Registered: Apr 2011
Posts: 11

Rep: Reputation: 0
Checking if multiple directories exist


Hi guys,

I'll get straight to the point.. I have one file called test.sh and in that file I have the below code. All this code is, is paths to three directories (as you can you can clearly see!).

Code:
#!/bin/bash
BACKUP="Documents /bin /sbin"
Now I have this other file which reads the directories (by using $BACKUP) and creates a tar file of everything in that folder. But what I am unsure of what to is create a bit of code that will simply look in test.sh, read all the directories and print a line saying either they all exist or some are missing. If possible it would be good to know which directories are missing too!

I have fiddled around with using -d but I can only get it to work for one directory or manually having to write out each directory.

Any ideas experts?

Many thanks in advance.. you all do a great job!
 
Old 04-27-2011, 09:28 AM   #2
savona
Member
 
Registered: Mar 2011
Location: Bellmawr, NJ
Distribution: Red Hat / Fedora
Posts: 195

Rep: Reputation: 50
Quote:
Originally Posted by amario26 View Post
Hi guys,

I'll get straight to the point.. I have one file called test.sh and in that file I have the below code. All this code is, is paths to three directories (as you can you can clearly see!).

Code:
#!/bin/bash
BACKUP="Documents /bin /sbin"
Now I have this other file which reads the directories (by using $BACKUP) and creates a tar file of everything in that folder. But what I am unsure of what to is create a bit of code that will simply look in test.sh, read all the directories and print a line saying either they all exist or some are missing. If possible it would be good to know which directories are missing too!

I have fiddled around with using -d but I can only get it to work for one directory or manually having to write out each directory.

Any ideas experts?

Many thanks in advance.. you all do a great job!
I am not much of a bash script writer (as you will probably see) but it seems to me you would want to split the $BACKUP variable and then check each directory.


Code:
#!/bin/bash
BACKUP="Documents /bin /sbin"

set -- $BACKUP

DIRS=$@

for i in "$@"
do
 if [ -d $i ]; then
        echo "Directory $i exists"
fi
done
 
1 members found this post helpful.
Old 04-27-2011, 09:34 AM   #3
amario26
LQ Newbie
 
Registered: Apr 2011
Posts: 11

Original Poster
Rep: Reputation: 0
Thank you so much! That is awesome! It prints out whether the directory exists! one question though (sorry i'm a bit new to this) but do you know how to change this a little so that it will tell me that a directory "doesnt exist"?

Many thanks
 
Old 04-27-2011, 09:35 AM   #4
amario26
LQ Newbie
 
Registered: Apr 2011
Posts: 11

Original Poster
Rep: Reputation: 0
Actually dont worry.. it was just a simple else function.. i.e.:

Code:
else
 echo "Directory $i doesnt exist"
 
Old 04-27-2011, 10:12 AM   #5
savona
Member
 
Registered: Mar 2011
Location: Bellmawr, NJ
Distribution: Red Hat / Fedora
Posts: 195

Rep: Reputation: 50
Quote:
Originally Posted by amario26 View Post
Actually dont worry.. it was just a simple else function.. i.e.:

Code:
else
 echo "Directory $i doesnt exist"
You got it!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking directories modification dates crofty13 Linux - Software 1 06-25-2007 11:50 AM
Change text in multiple files in multiple directories vivo2341 Linux - General 5 11-27-2006 09:16 PM
Searching multiple directories and sub directories for a file jeep99899 Linux - Newbie 2 10-13-2005 01:23 PM
script to check $PATH directories exist Frustin Linux - Software 3 09-21-2004 01:20 PM
when backingup directories, how to detect files already exist WarriorWarren Linux - General 10 04-06-2003 02:54 PM


All times are GMT -5. The time now is 08:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration