LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash check folder exists (https://www.linuxquestions.org/questions/programming-9/bash-check-folder-exists-645042/)

zerocool22 05-27-2008 06:03 AM

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?

pwc101 05-27-2008 06:11 AM

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

zerocool22 05-27-2008 06:21 AM

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'

Nathanael 05-27-2008 06:29 AM

also look at 'test' (see the man page for details)

if [ condition ]; then
command1
command2
else
command3
command4
fi

pwc101 05-27-2008 06:32 AM

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.

pixellany 05-27-2008 06:33 AM

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

"man bash" for more details

unSpawn 05-27-2008 06:41 AM

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.

zerocool22 05-27-2008 06:42 AM

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 :)

zerocool22 05-27-2008 07:01 AM

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.

pixellany 05-27-2008 07:05 AM

Quote:

Originally Posted by zerocool22 (Post 3165849)
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.

pwc101 05-27-2008 07:12 AM

Quote:

Originally Posted by zerocool22 (Post 3165860)
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 (Post 3165863)
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.

zerocool22 05-27-2008 08:31 AM

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.

pwc101 05-27-2008 08:33 AM

Quote:

Originally Posted by zerocool22 (Post 3165922)
are you sure its -d?

Why don't you read all the links yourself; the answer's there.

zerocool22 05-27-2008 08:46 AM

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??

pwc101 05-27-2008 08:56 AM

Quote:

Originally Posted by zerocool22 (Post 3165931)
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.


All times are GMT -5. The time now is 10:04 PM.