LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 05-27-2008, 06:03 AM   #1
zerocool22
Member
 
Registered: Feb 2008
Posts: 95

Rep: Reputation: 15
bash check folder exists


Hey i need to know how i can get if a folder excists or not

if /backup/$a exists
do
?

how can i do this?
 
Old 05-27-2008, 06:11 AM   #2
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,843

Rep: Reputation: 122Reputation: 122
Code:
if [ -d /backup/$a ]; then ...
See http://www.faqs.org/docs/bashman/bashref_68.html and http://tldp.org/LDP/abs/html/redircb.html#REDIR5

Last edited by pwc101; 05-27-2008 at 06:12 AM.
 
Old 05-27-2008, 06:21 AM   #3
zerocool22
Member
 
Registered: Feb 2008
Posts: 95

Original Poster
Rep: Reputation: 15
hey thx,
Could you help me debug my script?

#!/bin/bash

for i in $( ls /backup ) ; do
if [ -d /backup/$i ];
then
chown --reference=/home/$i /backup/$i
for j in $( ls /backup/$i ) ; do
if [ -d /backup/$i/$j ];
then
chown --reference=/home/$i/$j /backup/$i/$j
done
else
echo error
done

error: ./backup.sh: line 11: syntax error near unexpected token `done'
./backup.sh: line 11: ` done'
 
Old 05-27-2008, 06:29 AM   #4
Nathanael
Member
 
Registered: May 2004
Location: Karlsruhe, Germany
Distribution: debian, gentoo, os x (darwin), ubuntu
Posts: 940

Rep: Reputation: 31
also look at 'test' (see the man page for details)

if [ condition ]; then
command1
command2
else
command3
command4
fi
 
Old 05-27-2008, 06:32 AM   #5
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,843

Rep: Reputation: 122Reputation: 122
You don't have to list the files using ls in a for loop, bash can do it for you. Also, if you read the second link I posted, it shows you how to close an if statement. For example:
Code:
for i in /backup/*; do
   if [ -d /backup/$i ]; then
      chown --reference=/home/$i /backup/$i
   fi
done
I suggest you take a look at the Advanced Bash Scripting Guide I linked to in my first post, paying particular attention to Part 2, Chapter 7. It has excellent examples that will allow you to learn rather than me tell you how to do this.
 
Old 05-27-2008, 06:33 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: back to Arch
Posts: 16,676

Rep: Reputation: 425Reputation: 425Reputation: 425Reputation: 425Reputation: 425
The "if" statement(s) need "fi" at the end, eg:
if something
then something
fi

"man bash" for more details
 
Old 05-27-2008, 06:41 AM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 21,610
Blog Entries: 47

Rep: Reputation: 1413Reputation: 1413Reputation: 1413Reputation: 1413Reputation: 1413Reputation: 1413Reputation: 1413Reputation: 1413Reputation: 1413Reputation: 1413
Also running the script as "sh -vxe scriptname" should be your first debug reflex (along with prefixing one-time or destructive commands with 'echo' to prevent them from executing). And please *do* read the basic Bash manuals please.
 
Old 05-27-2008, 06:42 AM   #8
zerocool22
Member
 
Registered: Feb 2008
Posts: 95

Original Poster
Rep: Reputation: 15
thx guys, yeah i know i have to read the manuals, but im just using bash for this one time, so i would prob get faster helped here then reading all the manuals
 
Old 05-27-2008, 07:01 AM   #9
zerocool22
Member
 
Registered: Feb 2008
Posts: 95

Original Poster
Rep: Reputation: 15
ok i got it working, but now i changed it a bit and now it doesnt work either.

#!/bin/bash

for i in $( ls /backup ) ; do
if [ -d /backup/$i ]; then
chown --reference=/home/$i /backup/$i
else
echo ls /backup/$i
for j in $( ls /backup/$i ) ; do
if [ -d /backup/$i/$j ]; then
chown -R --reference=/home/$i/$j /backup/$i/$j
fi
else
echo ls /backup/$i/$j
done
fi
done


./backup.sh: line 12: syntax error near unexpected token `else'
./backup.sh: line 12: ` else'

Im not sure that the echo command works though, the main idea after my script is /home/jacky permissions get copied to /backup/jacky and that part works fine, but somethimes there are more folders like, /home/jacky/tom and that why i have a second for loop. Dont know that its working like i intend.
 
Old 05-27-2008, 07:05 AM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: back to Arch
Posts: 16,676

Rep: Reputation: 425Reputation: 425Reputation: 425Reputation: 425Reputation: 425
Quote:
Originally Posted by zerocool22 View Post
thx guys, yeah i know i have to read the manuals, but im just using bash for this one time, so i would prob get faster helped here then reading all the manuals
We're aware that many people are too lazy to study a manual (or even to look up a man page on one command). Some of us, however, would rather not be reminded.
 
Old 05-27-2008, 07:12 AM   #11
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,843

Rep: Reputation: 122Reputation: 122
Quote:
Originally Posted by zerocool22 View Post
Im not sure that the echo command works though, the main idea after my script is /home/jacky permissions get copied to /backup/jacky and that part works fine, but somethimes there are more folders like, /home/jacky/tom and that why i have a second for loop. Dont know that its working like i intend.
Why not just recursively chown the whole backup directory? Read man chown, and look for -R.

edit:
Quote:
Originally Posted by pixellany View Post
We're aware that many people are too lazy to study a manual (or even to look up a man page on one command). Some of us, however, would rather not be reminded.
Hear hear.

Last edited by pwc101; 05-27-2008 at 07:14 AM.
 
Old 05-27-2008, 08:31 AM   #12
zerocool22
Member
 
Registered: Feb 2008
Posts: 95

Original Poster
Rep: Reputation: 15
ok i got it working, but it doesnt show the echo from missing folders, he just sees if there are any different files and echo's them? are you sure its -d?

Because of the user/group hiarchy i dont use -R on both of them I get my users from active directory, and ive build somewhat hiarchy.
 
Old 05-27-2008, 08:33 AM   #13
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,843

Rep: Reputation: 122Reputation: 122
Quote:
Originally Posted by zerocool22 View Post
are you sure its -d?
Why don't you read all the links yourself; the answer's there.
 
Old 05-27-2008, 08:46 AM   #14
zerocool22
Member
 
Registered: Feb 2008
Posts: 95

Original Poster
Rep: Reputation: 15
ok i checked and it is indeed -d so why does it not work, now there are for example some folders in /backup/ missing, and he doesnt show any??
 
Old 05-27-2008, 08:56 AM   #15
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,843

Rep: Reputation: 122Reputation: 122
Quote:
Originally Posted by zerocool22 View Post
ok i checked and it is indeed -d so why does it not work, now there are for example some folders in /backup/ missing, and he doesnt show any??
What does your script look like now? Please post it between [code][/code] tags to preserve formatting.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Makefile:condition to check whether a file exists ? Ashok_mittal Linux - Newbie 4 12-06-2011 07:52 PM
IMAP folder exists but invisible in Outlook&Webclient demsab Linux - Software 4 03-07-2008 01:13 AM
how to make ln check whether target exists bkeeper Linux - Software 2 12-23-2005 03:51 AM
ksh check if file exists (using wildcard) problem r18044 Linux - Newbie 5 02-22-2005 07:52 AM
c++ check if file exists Genjix Programming 3 03-15-2004 12:08 AM


All times are GMT -5. The time now is 03:00 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration