LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 11-27-2017, 07:19 AM   #1
hazyy12
LQ Newbie
 
Registered: Nov 2017
Posts: 3

Rep: Reputation: Disabled
Answered.


This question has been answered thank you for your time.

Last edited by hazyy12; 11-27-2017 at 07:14 PM.
 
Old 11-27-2017, 08:10 AM   #2
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,794

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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
 
1 members found this post helpful.
Old 11-27-2017, 06:58 PM   #3
hazyy12
LQ Newbie
 
Registered: Nov 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank you!

It worked!
 
Old 11-27-2017, 07:18 PM   #4
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
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.
 
Old 11-27-2017, 07:25 PM   #5
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
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
 
2 members found this post helpful.
Old 11-27-2017, 08:48 PM   #6
hazyy12
LQ Newbie
 
Registered: Nov 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Keith Hedger View Post
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.
 
Old 11-27-2017, 09:41 PM   #7
jamison20000e
Senior Member
 
Registered: Nov 2005
Location: ...uncanny valley... infinity\1975; (randomly born:) Milwaukee, WI, US( + travel,) Earth&Mars (I wish,) END BORDER$!◣◢┌∩┐ Fe26-E,e...
Distribution: any GPL that work on freest-HW; has been KDE, CLI, Novena-SBC but open.. http://goo.gl/NqgqJx &c ;-)
Posts: 4,888
Blog Entries: 2

Rep: Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567
Thumbs up



The edit leaves a history of the original text so you may fix it‽
 
Old 11-29-2017, 01:46 AM   #8
!!!
Member
 
Registered: Jan 2017
Location: Fremont, CA, USA
Distribution: Trying any&ALL on old/minimal
Posts: 997

Rep: Reputation: 382Reputation: 382Reputation: 382Reputation: 382
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)

Last edited by !!!; 11-29-2017 at 02:03 AM.
 
1 members found this post helpful.
Old 11-29-2017, 02:18 AM   #9
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,150

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
Lightbulb

Quote:
Originally Posted by MadeInGermany View Post
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."

What's the consequences if it's not in quotes?

Thanks.
 
Old 11-29-2017, 02:50 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
Quote:
Originally Posted by hazyy12 View Post
I don't understand forum etiquette yet, I won't make that mistake again.
First of all, do not remove your original post.
 
1 members found this post helpful.
Old 11-29-2017, 02:56 AM   #11
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,150

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
Talking

Quote:
Originally Posted by pan64 View Post
First of all, do not remove your original post.
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.

It's like an email without a subject.
 
Old 11-29-2017, 04:52 AM   #12
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,794

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
$i should be in quotes, because
1. the shell splits on whitespace including newline (by default, tunable with IFS)
Code:
i="two        words"
j="two
lines"
echo $i
two words
echo $j
two lines
but not if the argument is a "string"
Code:
echo "$i"
two        words
echo "$j"
two
lines
2. the shell globs against files in the current directory
Code:
cd /
i=*
echo $i
bin boot cc dev env etc ftp home lib lib64 ...
but not if the argument is a "string"
Code:
echo "$i"
*
 
  


Reply



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] 'sudo chown root file.txt' is inconsistent in leading to a "permission denied" output charlemagne-is-my-son Linux - Newbie 1 11-09-2014 08:47 AM
[SOLVED] Silencing the line "echo test > test/test.txt" in a shell script Arenlor Linux - General 2 06-18-2010 01:37 PM
perl script -> take a file that end in ".txt" cccc Programming 4 07-21-2009 01:58 PM
PHP 5.2.6 "make test" zend test failures mlnutt Linux - Server 0 05-07-2008 02:25 PM
cannot exit normally after "nohup blablabla > output.txt &" LQYY Linux - Newbie 1 12-14-2004 10:02 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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