[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Off Topic: streaming a text file into a command?



what a maroon i am. lemme fix:

On Wed, Oct 11, 2000 at 06:14:47PM -0500, will trillich wrote:
> On Wed, Oct 11, 2000 at 04:47:28PM -0500, William Jensen wrote:
> > Hey guys,
> > 
> > I'm trying to find the fastest mirror site.  I am using a package called
> > netselect.  Works like this netselect -vv host1 host2 ... hostx.  Wonderful
> > little utility.  Anyway, I have a list of mirrors in a text file like this:
> > 
> > host1
> > host2
> > host3
> > 
> > How can I pump that file into netselect?  I tried netselect -vv < file but
> > it just laughed at me. :)
> 
> probably rolling its eyes as it did so...
> 
> short version:
> 
> 	if your text file has one word per line, try this:
> 
> 		netselect -vv `cat my-settings-file`

interruptions can mess with your head. i hadn't actually finished this
(including a double-check on what wound up as a bad assumption)
before i sent it...

	the backticks (left of the numeric keys atop your keyboard)
	execute the command within, and return the output of that command,
	as if you'd pasted it there:

	% which vi
	/usr/bin/vi
	% ls -l `which vi`
	lrwxrwxrwx  1 root   root   20 Sep 14 15:39 /usr/bin/vi -> /etc/alternatives/vi

that last one is the same as saying
	% ls -l /usr/bin/vi
but you get the shell to do some of your homework for you.

here's some more productive examples:
	% more `which apacheconfig`
		# see the source of a system shell script
	% ls -lt `locate somefile`
		# have 'locate' print some file paths that you sort in modified-order
	% munge-these `cat my-list-of-items`
		# tell "munge-these" every item listed in the my-list-of-items file
	% set SUBJECT="look out below"
	% set TARGET_GROUP=/a/file/with/email/addresses/in/it
	% set MESSAGE_FILE=/file/containing/the/body/of/the/message
	% mail -s $SUBJECT `cat $TARGET_GROUP` < $MESSAGE_FILE

> long version:
> 
> 	if you 'store' the output of a command in a variable,
> 	you get all the tabs, spaces and newlines verbatim.
NOT.
> 	when you use that as an argument to a command,
> 	*nix scrunches all whitespace into single space
> 	characters, so all the command will 'see' is the
> 	individual words.

correction: all whitespace is scrunched down to one space immediately;
thus any distinct, separate words in the output, whether separated by
spaces, newlines, tabs or multiple occurrances of each, are returned as
separated by one space, before being stored in the variable (or before
being used in the context of the backtick, even if you're not storing
the output into a variable):

	% ps t0
	  PID TTY      STAT   TIME COMMAND
	31688 pts/0    S      0:00 -tcsh
	32295 pts/0    T      0:05 mutt -y -e push Od
	32542 pts/0    T      0:00 -sh
	 1670 pts/0    T      0:01 /usr/bin/elvis -G termcap /tmp/mutt-server-32295-58
	 1707 pts/0    R      0:00 ps t0

so that's the output of the command "ps t0". nail-biting excitement, huh?

but here, we save it in shell (tcsh) variable "x":

	% set x=`ps t0`
	% echo "$x"
	PID TTY STAT TIME COMMAND 31688 pts/0 S 0:00 -tcsh 32295 pts/0
	T 0:05 mutt -y -e push Od 32542 pts/0 T 0:00 -sh 1670 pts/0 T
	0:01 /u sr/bin/elvis -G termcap /tmp/mutt-server-32295-58 1708
	pts/0 R 0:00 ps t0

another subtle distinction: using "$x" in quotes sends just ONE argument
to the echo command, which consists of a long string that includes
some spaces.

	% echo $x
	PID TTY STAT TIME COMMAND 31688 pts/0 S 0:00 -tcsh 32295 pts/0
	T 0:05 mutt -y -e push Od 32542 pts/0 T 0:00 -sh 1670 pts/0 T
	0:01 /u sr/bin/elvis -G termcap /tmp/mutt-server-32295-58 1708
	pts/0 R 0:00 ps t0

using $x without the quotes here, sends a whole bunch of separate
arguments to the echo command, which turns around and repeats each
one, separated by a single space.

> 	for fancier stuff, try
> 
> 		generate-some-list-however-you-like | xargs cmd -options

which is similar to
	% cmd -options `generate-list`
and much more intelligent than
	% find /start/here -condition -exec cmd \{} \;
which sucks whatever life remains out of your processor.
and just plain sucks in general.

> 	man xargs for the full poop.

that xargs gadget is one of those "why-didn't-they-think-of-that-sooner"
kind of things, once you figure it out.

-- 
things are more like they used to be than they are now.

will@serensoft.com *** http://www.dontUthink.com/



Reply to: