creating a for loop.
A Linux question,
In a self study I am doing, the exercise question is "Write a for loop which goes through all the files in a directory and prints out their names with echo . If you write the whole thing on one line, then it will be easy to repeat it using the command line history." I am assuming this has something to do with using ls -a or ls -l. My problem is I can not figure out what the var should be. All successful attempts at getting something to actually echo, have not been the desired result. I hope my description is not vague. If someone could kindly point me in the right direction I am happy to do the work myself. for <var> ls -a /home/username; do echo "$<var> ls -a/home/username" done Am I anywhere near the ballpark? |
You're close. You'll see the exact syntax of the for loop in the man page for the shell you are using : sh, bash, ksh, zsh, etc
It will be further down but it'll be there. "ls" is kind of awkward though. You might want to use globbing or "find" instead. http://mywiki.wooledge.org/BashPitfa...8ls_.2A.mp3.29 http://mywiki.wooledge.org/glob |
|
Thanks
Thanks for the point in the right direction!
Code:
$ for i in */home/(username); do [ "i" ] || continue; ls -a /home/(username) "i"; done Code:
for i in ~/*; do [ "$i" ] || continue; echo ["$i"]; done So at least I am in the ballpark now! |
When you reference a variable, you need to stick "$" in front of it, otherwise it just uses the literal character.
Code:
$ i=5 |
Solved!
Thanks for the help. Sorry it took so long to get back to ya all. I had a few things to do.
Code:
for i in * ~/; do echo ls -a ~/ "$i"; done nope it's this one! Code:
for i in * ~/ ; do ls -a "$i"; done |
Quote:
If you just want to list the files in your home directory, as per post #4, then Code:
for i in ~/*; do echo "$i"; done |
like so?
Code:
shopt -s dotglob; for i in ~/*; do echo "$i"; done |
Quote:
Placing the * behind ~/ forced the loop to read from the files "inside" the home directory. Code:
for i in ~/*; do echo "$i"; done Thanks for you help and corrections |
Moderator response
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
|
Thought I would just tack on an addendum for you ... your current solution will echo all the items (except hidden) in your home directory ... bonus points to you if you can workout how to print only files??
Currently if you have directories or other types in your home directory they will all get echoed, but your original requirement was for only the files. See how you go :) |
Code:
find ~/ -type f | less |
I thought the assignment was to use a for loop and echo?
|
Code:
for i in ~/* ; do [ -f "$i" ] && echo "$i" ;done Code:
for i in ~/.* ; do [ -f "$i" ] && echo "$i" ;done Code:
for i in * ; do [ -f "$i" ] && echo "$i" ;done Code:
for i in *. ; do [ -f "$i" ] && echo "$i" ;done Code:
for i in find ~/ -type f ; do echo "$i" ; done |
Good job on extra credit :)
Just to help with the last 2 loops: *. - this will look for any item in the current directory ending in a dot, ie. file. find ~/ -type f - did you run this for loop, I think if you run it you will see that it does run and gives output, but has absolutely nothing to do with the files in any directory To make the last one work you could investigate $(), however, I would not recommend this process with a for loop as word splitting will be your downfall. To help with this and other queries, try to work your way through the following site :- http://mywiki.wooledge.org/TitleIndex |
All times are GMT -5. The time now is 04:18 PM. |