Now on ScienceBlogs: Oh, no! School wi-fi is making our kids sick! (2012 edition)

ScienceBlogs Book Club: Inside the Outbreaks

Greg Laden's Blog

Evolution, Life Sciences, Science Education, Human Evolution, and Stuff

Hornbill170.jpg Looking for stuff about birds?

Darwing_Face.jpg Learn more about Charles Darwin and his work.

Lion_mane170.jpg Lean more about lions

Congo_sidebar.jpg An archaeological expedition to the Congo


The Skeptical Search Engine


Nature Blog Network
Climate Defense Fund


The contents of Greg Laden's Blog are copyrighted by Greg Laden.

Recent Comments

Search

Profile


Click on "About" for the big picture, and "Archives" for the details.


Recent Posts

Blogroll

If you don't see yourself on my blogroll, just drop me a line and let me know. I'll add you.*
*Assuming that I'm on your blogroll, of course!

Archives

« Ooops ... gun goes off on commercial plane | Main | New early human fossil »

Learning the bash shell, third look

Posted on: March 26, 2008 6:52 PM, by Greg Laden

image.jpgTime for another installment of our look at the book Learning the bash Shell (In a Nutshell (O'Reilly)). Today's foray into Linux Land: Unix Command Substitution.

There is more than one way to do this, but here is a sensible way to look at the format. In command substitution you stick something that comes out to be a valid UNIX command insice parentheses preceded with a dollar sign:

$(UNIX command)

The unix command is evaluated and then the result replaces the $() bit.

For example, say you wanted to edit all of the files in the current directory that included the letters "html". You could use the "grep" command to parse out all of the files to get a look at them. Grep goes through files and searches for a string, and by default spits out the line containing the string. There is an option (-l) that tells grep to spit out only the filename. So, for instance, I can do this:


greg@greg-desktop:~/Desktop$ cd markdown
greg@greg-desktop:~/Desktop/markdown$ grep -l 'html' *
4sh.html
blog2.html
blog2.txt
blogrolJava.txt
blogroll_master_SU
boneyard.html
boneyard.txt
calendar2html
debate2.html

...

Then I could type these names into the text editor command.

Or, I can turn this list of phrases into part of a command line. If I use "getdit" as the text editor, I can type"

gedit grep -l 'html' *

But that would not work! -l is not an available option for gedit, and there are other confusing things on this line to cause gedit to not get it. Get it? Good.

BUt, if I put the mojo that spits out the list of files (which I tried above and know works) into the command sbustitution command, then then that will convert "grep -l 'html' *" into a a list of files.

So this works:


greg@greg-desktop:~/Desktop/markdown$ gedit $(grep -l html *) &

This is the same as typing:

greg@greg-desktop:~/Desktop/markdown$ gedit 4sh.html blog2.html blog2.txt .... and so on. The "and" sign at the end puts the operation into the background so I can keep using the same command line.


Share on Facebook
Share on StumbleUpon
Share on Facebook
Find more posts in: Technology

TrackBacks

TrackBack URL for this entry: http://scienceblogs.com/mt/pings/67704

Comments

1

How about "grep -l 'html' * > filename.txt"?

Posted by: Max Dobberstein | March 26, 2008 9:01 PM

2

Well, the goal is to edit the files that have html in them, not to edit a list of names of files that have html in them.

But
grep -l 'html' * | xargs gedit &
will work for many people who don't have bash as their shell.

Posted by: rpenner | March 26, 2008 9:42 PM

Post a Comment

(Email is required for authentication purposes only. On some blogs, comments are moderated for spam, so your comment may not appear immediately.)





ScienceBlogs

Search ScienceBlogs:

Go to:

Advertisement
Follow ScienceBlogs on Twitter

© 2006-2011 ScienceBlogs LLC. ScienceBlogs is a registered trademark of ScienceBlogs LLC. All rights reserved.