LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Problem with simple shell script for loop (http://www.linuxquestions.org/questions/programming-9/problem-with-simple-shell-script-for-loop-376791/)

abefroman 10-25-2005 01:25 PM

Problem with simple shell script for loop
 
I am trying to list the permissions of public_html for all users in my home directory.

The command I am using is:
for i in `ls`;do `ls -al $ipublic_html`; done

but it is not working and displays the output as follows"
root@server1 [/home]# for i in `ls`;do `ls -al $ipublic_html`; done
-bash: total: command not found
-bash: total: command not found
-bash: total: command not found
-bash: total: command not found
-bash: total: command not found
-bash: total: command not found
-bash: total: command not found
-bash: total: command not found
-bash: total: command not found
-bash: total: command not found
-bash: total: command not found
-bash: total: command not found
-bash: total: command not found
-bash: total: command not found
-bash: total: command not found
-bash: total: command not found
-bash: total: command not found
-bash: total: command not found
-bash: total: command not found
-bash: total: command not found
-bash: total: command not found
-bash: total: command not found

What would the correct command be?

Thanks in advance.

:newbie: :Pengy:

MensaWater 10-25-2005 02:05 PM

You shouldn't have back ticks (`) around the command following "do". The back ticks tell it to execute the command then use the commands outuput. They are correct following the "in".

Also you need to encapsulte your variable because it is confused. "for i" says for each item call it $i. You then say "$ipublic_html" so it thinks your variable is ipublic_html instead of $i followed by public_html.
To encapsulate it just use the braces as in ${i}public_html.

for i in `ls`;do ls -al ${i}public_html; done

Finally from what you wrote I suspect these are subdirectories you're trying to list. If so you need to put a slash between the variable and the file name - otherwise its looking for a concatenated name (that is ${i}public_html for subdirectory user1 would give you "user1public_html" but you probably want "user1/public_html". The revised syntax would be:

for i in `ls`;do ls -al ${i}/public_html; done

paulsm4 10-25-2005 08:26 PM

Quote:

I am trying to list the permissions of public_html for all users in my home directory.
Uh - what?!?

Could you please explain a bit better what you're trying to do? We'd be happy to help you script doing it.


All times are GMT -5. The time now is 08:41 PM.