Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
First of all the `ls /etc/*conf` involves an external command and expands any directories.
Simpler is /etc/*conf
and a test for a file.
Then, $i should be in quotes, otherwise the shell tries expansions on it.
Last but not least, each loop cycle creates a new redirection, overwriting the previous file. Finally the file contains the last wc -l result. But certainly you want all the results. A simple and efficient method is to redirect the whole loop (i.e. all output from within the loop goes to the file)
Code:
for i in /etc/*conf
do
# it's a file, otherwise jump to next loop cycle
[ -f "$i" ] || continue
wc -l "$i"
done > /pleasework.txt
Last edited by MadeInGermany; 11-27-2017 at 08:14 AM.
Reason: we don't need 2>/dev/null
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150
Rep:
For clarity you should use the $() construct rather than backticks ` as its easy to mistake a backtick for a single quote, especially when you are begining, the $() is also posix correct but can't be used ( normally ) in a Makefile for instance.
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150
Rep:
Mark your thread solved with the thread tools, don't delete the original query or change the post title unless requested to do so, iits very bad mannered as other people may have had the same problem as you and now wont find the soulution whilst searching the forums
Mark your thread solved with the thread tools, don't delete the original query or change the post title unless requested to do so, iits very bad mannered as other people may have had the same problem as you and now wont find the soulution whilst searching the forums
I don't understand forum etiquette yet, I won't make that mistake again.
My thinking behind the decision was I didn't want anyone to waste their time solving a problem that was fixed but I should of looked properly for thread solved instead of deleting sorry about that, for future posts I will not delete any contents I will just mark as solved.
If you click on #1's blue-underlined Last edited by hazyy12; ...day at 0x:14 PM.
You will see the 'Post Edit History', which you can use, to restore your original post, to help future readers.
Welcome to LQ!!! No big harm on missing a procedural technicality on first try
(There may be a bit of 'extra' sensitivity @LQ because of other UNrelated posters hiding (covering-up) stuff, rendering the responses mysterious)
First of all the `ls /etc/*conf` involves an external command and expands any directories.
Simpler is /etc/*conf
and a test for a file.
Then, $i should be in quotes, otherwise the shell tries expansions on it.
Last but not least, each loop cycle creates a new redirection, overwriting the previous file. Finally the file contains the last wc -l result. But certainly you want all the results. A simple and efficient method is to redirect the whole loop (i.e. all output from within the loop goes to the file)
Code:
for i in /etc/*conf
do
# it's a file, otherwise jump to next loop cycle
[ -f "$i" ] || continue
wc -l "$i"
done > /pleasework.txt
Sorry for this noob question, what do you mean by this ==> "Then, $i should be in quotes, otherwise the shell tries expansions on it."
Agree, it doesn't make sense to remove the original post. It will be super confusing for future readers. Like WTH, readers will not be able to follow the whole thread.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.