LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-07-2016, 10:48 AM   #1
Lex_Linux
LQ Newbie
 
Registered: May 2016
Distribution: Debian
Posts: 17

Rep: Reputation: Disabled
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?
 
Old 06-07-2016, 11:19 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,288
Blog Entries: 3

Rep: Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718
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
 
1 members found this post helpful.
Old 06-07-2016, 11:31 AM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Better see http://mywiki.wooledge.org/BashPitfalls

I assumed bash, sorry if that's incorrect.

Last edited by Habitual; 06-07-2016 at 11:32 AM.
 
Old 06-07-2016, 12:02 PM   #4
Lex_Linux
LQ Newbie
 
Registered: May 2016
Distribution: Debian
Posts: 17

Original Poster
Rep: Reputation: Disabled
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!

Last edited by Lex_Linux; 06-07-2016 at 08:05 PM.
 
Old 06-07-2016, 12:10 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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
 
1 members found this post helpful.
Old 06-07-2016, 05:54 PM   #6
Lex_Linux
LQ Newbie
 
Registered: May 2016
Distribution: Debian
Posts: 17

Original Poster
Rep: Reputation: Disabled
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

Last edited by Lex_Linux; 06-07-2016 at 06:11 PM.
 
Old 06-07-2016, 06:22 PM   #7
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,367

Rep: Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747
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'.
 
1 members found this post helpful.
Old 06-07-2016, 06:43 PM   #8
Lex_Linux
LQ Newbie
 
Registered: May 2016
Distribution: Debian
Posts: 17

Original Poster
Rep: Reputation: Disabled
like so?

Code:
shopt -s dotglob; for i in ~/*; do echo "$i"; done
 
Old 06-07-2016, 08:24 PM   #9
Lex_Linux
LQ Newbie
 
Registered: May 2016
Distribution: Debian
Posts: 17

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by allend View Post
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

Last edited by Lex_Linux; 06-07-2016 at 08:33 PM.
 
Old 06-11-2016, 07:53 AM   #10
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,923
Blog Entries: 44

Rep: Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158
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.
 
Old 06-11-2016, 08:28 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
Old 06-11-2016, 10:49 PM   #12
Lex_Linux
LQ Newbie
 
Registered: May 2016
Distribution: Debian
Posts: 17

Original Poster
Rep: Reputation: Disabled
Code:
find ~/ -type f | less
 
Old 06-11-2016, 10:52 PM   #13
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
I thought the assignment was to use a for loop and echo?
 
Old 06-12-2016, 05:36 AM   #14
Lex_Linux
LQ Newbie
 
Registered: May 2016
Distribution: Debian
Posts: 17

Original Poster
Rep: Reputation: Disabled
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
 
Old 06-12-2016, 06:54 AM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Creating an ntfs fs in a file and mounting it with loop Wocky Linux - Software 1 02-11-2012 04:29 AM
Bash - Creating Arrays using loop counter Mixiul Programming 4 02-08-2012 09:05 AM
Creating a loop within a script using ps2pdf kaplan71 Linux - Software 9 10-02-2009 07:51 AM
Defining loop statement for creating new file each time neo009 Linux - Newbie 6 04-12-2009 06:49 AM
Creating an application loop keysorsoze Linux - Software 6 01-19-2007 10:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration