ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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'
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.
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.
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.
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.