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? |
Code:
if [ -d /backup/$a ]; then ... |
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' |
also look at 'test' (see the man page for details)
if [ condition ]; then command1 command2 else command3 command4 fi |
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 |
The "if" statement(s) need "fi" at the end, eg:
if something then something fi "man bash" for more details |
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.
|
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 :)
|
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. |
Quote:
|
Quote:
edit: Quote:
|
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. |
Quote:
|
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??
|
Quote:
|
All times are GMT -5. The time now is 10:04 PM. |