LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   creating a for loop. (https://www.linuxquestions.org/questions/programming-9/creating-a-for-loop-4175581669/)

Lex_Linux 06-07-2016 10:48 AM

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?

Turbocapitalist 06-07-2016 11:19 AM

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

Habitual 06-07-2016 11:31 AM

Better see http://mywiki.wooledge.org/BashPitfalls

I assumed bash, sorry if that's incorrect.

Lex_Linux 06-07-2016 12:02 PM

Thanks
 
Thanks for the point in the right direction!



Code:

$ for i in */home/(username); do [ "i" ] || continue; ls -a /home/(username) "i"; done
The correct way...
Code:

for i in ~/*; do [ "$i" ] || continue; echo ["$i"]; done
this produces the desired result, but with the error ls: cannot access 'i': No such file or directory.

So at least I am in the ballpark now!

suicidaleggroll 06-07-2016 12:10 PM

When you reference a variable, you need to stick "$" in front of it, otherwise it just uses the literal character.
Code:

$ i=5
$ echo i
i
$ echo $i
5


Lex_Linux 06-07-2016 05:54 PM

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

allend 06-07-2016 06:22 PM

Quote:

for i in * ~/; do echo ls -a ~/ "$i"; done
That 'for' loop is not doing the task that you first described. Rather, it builds a list of files from the current directory as well as your home directory.
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
By default, the above code may not show hidden files (i.e. files with names starting with a period character). If so, you need to use 'shopt -s dotglob'.

Lex_Linux 06-07-2016 06:43 PM

like so?

Code:

shopt -s dotglob; for i in ~/*; do echo "$i"; done

Lex_Linux 06-07-2016 08:24 PM

Quote:

Originally Posted by allend (Post 5557463)
That 'for' loop is not doing the task that you first described. Rather, it builds a list of files from the current directory as well as your home directory.
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
By default, the above code may not show hidden files (i.e. files with names starting with a period character). If so, you need to use 'shopt -s dotglob'.

My placing of the * was wrong which caused the loop to also read from my current directory, instead of just my home directory.

Placing the * behind ~/ forced the loop to read from the files "inside" the home directory.

Code:

for i in ~/*; do echo "$i"; done
was less typing then the way I was originally trying. The error I was getting in post four(simply not placing "$i"), made me re-think how I was going about getting the result. The post where I triumphantly announced SOLVED seemed right(obviously).

Thanks for you help and corrections

onebuck 06-11-2016 07:53 AM

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.

grail 06-11-2016 08:28 AM

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

Lex_Linux 06-11-2016 10:49 PM

Code:

find ~/ -type f | less

suicidaleggroll 06-11-2016 10:52 PM

I thought the assignment was to use a for loop and echo?

Lex_Linux 06-12-2016 05:36 AM

Code:

for i in ~/* ; do [ -f "$i" ] && echo "$i" ;done
To show just hidden files

Code:

for i in ~/.* ; do [ -f "$i" ] && echo "$i" ;done
or you could do it this way

Code:

for i in * ; do [ -f "$i" ] && echo "$i" ;done
this is suppose to work, but doesn't(according to the book (echo *.)

Code:

for i in *. ; do [ -f "$i" ] && echo "$i" ;done
this works, but not sure it would be right.
Code:

for i in find ~/ -type f ; do echo "$i" ; done

grail 06-12-2016 06:54 AM

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.