[Techtalk] re: Joining commands together in bash
Random832
random832 at rcbooks.org
Mon May 12 06:50:18 EST 2003
On Mon, May 12, 2003 at 03:25:23AM -0700, Berenice wrote:
> This is great. So now I've learned 2 new things about bash.
>
> On Sun, May 11, 2003 at 12:34:07PM +0100, Meredydd wrote:
> > This is how I would do it:
> >
> > for FILE in * ; do
> > if [ x`file $FILE | grep 'shell script text executable'` != x ];
> > then
> > head -n 3 $FILE
> > fi
> > done
>
>
> Do newer versions of bash accept statements in the form of
> x`blahblah` != x ? I know you've said it works in older shells, but
> it doesn't seem to work in mine. I don't know if I'm typing it wrong,
> but I also tried `echo hi` at the command prompt and got the message
> "bash: hi: command not found". Just wondering...
first, that's the expected result for `echo hi` at the command prompt:
"`echo hi`" evaluates to "hi"
if you typed just "hi" at the command prompt, you would also get command
not found.
`this` itself is a holdover from older shells, if you're using bash you
should use $(this) instead, since it works better for nesting.
the actual problem with the example was probably the `` statement
evaluated to multiple tokens... replace it with "``" or "$()"
the [ x.... != x ] construct has never made much sense to me, when
simply [ -n ...] works fine
given that, the example should be
for FILE in * ; do
if [ -n "$(file $FILE | grep 'shell script text executable')" ];
then
head -n 3 $FILE
fi
done
however, it's much simpler to just check the return status of grep:
for FILE in *; do
if file $FILE | (grep 'shell script text executable' >&/dev/null);
then
echo "-- $FILE --"
head -n 3 $FILE
fi
done
(i also added something you'll probably need, a separator line that
identifies the file)
that said, the xargs solution is probably a better way to do it.
> Date: Sun, 11 May 2003 21:46:52 +1000
> From: Malcolm Tredinnick <malcolm at commsecure.com.au>
>
> >Straight out of the "more than one way to skin a cat" bucket, here
> is
> >my solution:
>
> >file * | grep 'shell script text executable' | cut -f1 -d':' | xargs
>
> >head -n 3
>
> This is the first time I've seen the xargs command. It's quite handy!
>
> Berenice
More information about the Techtalk
mailing list