|
Home > Archive > Unix Shell > May 2007 > Feeding two consecutive commands using xargs
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Feeding two consecutive commands using xargs
|
|
| Mark Hobley 2007-05-22, 7:18 am |
| I am using a borne compatible shell, and I want to like to try and chain a
series of commands together using a one-liner to achieve the following:
I have a command "modprobe", which provides a list of modules:
/lib/modules/2.6.18-4-486/kernel/sound/oss/uart6850.ko
/lib/modules/2.6.18-4-486/kernel/sound/oss/uart401.ko
/lib/modules/2.6.18-4-486/kernel/sound/oss/trix.ko
(and squillions more besides)
I now want to strip the full path and extention, which I have achieved as
follows:
/sbin/modprobe -l|xargs -l1 -I '{}' basename {} .ko
This produces:
uart6850
uart401
trix
Now I want to echo the name and modinfo -L. This is where I am stuck. I need
to feed two commands. I want to output a filename, a space and then a
description for each module.
The following gives me a list of descriptions, but no filename:
/sbin/modprobe -l|xargs -l1 -I '{}' basename {} .ko| xargs -l1 -I '{}'
/sbin/modinfo -F description {}
I want to modify this as follows, but I don't know the syntax:
/sbin/modprobe -l|xargs -l1 -I '{} basename {} .ko| xargs -l1 -I '{}' { echo
{}; echo ' '; /sbin/modinfo -F description {} }
I am trying three commands in a script as follows:
{ echo {}; echo ' '; /sbin/modinfo -F description {} }
I assumed the { and } would make a block of code, somehow.
Presumably, I could eliminate one of the xargs calls somehow.
Thanks in advance to anyone who can help.
Mark.
--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE
Email: markhobley at hotpop dot donottypethisbit com
http://markhobley.yi.org/
| |
| Icarus Sparry 2007-05-22, 1:25 pm |
| On Tue, 22 May 2007 12:08:03 +0000, Mark Hobley wrote:
> I am using a borne compatible shell, and I want to like to try and chain
> a series of commands together using a one-liner to achieve the
> following:
>
> I have a command "modprobe", which provides a list of modules:
>
> /lib/modules/2.6.18-4-486/kernel/sound/oss/uart6850.ko
> /lib/modules/2.6.18-4-486/kernel/sound/oss/uart401.ko
> /lib/modules/2.6.18-4-486/kernel/sound/oss/trix.ko
>
> (and squillions more besides)
>
> I now want to strip the full path and extention, which I have achieved
> as follows:
>
> /sbin/modprobe -l|xargs -l1 -I '{}' basename {} .ko
>
> This produces:
>
> uart6850
> uart401
> trix
>
> Now I want to echo the name and modinfo -L. This is where I am stuck. I
> need to feed two commands. I want to output a filename, a space and then
> a description for each module.
>
> The following gives me a list of descriptions, but no filename:
>
> /sbin/modprobe -l|xargs -l1 -I '{}' basename {} .ko| xargs -l1 -I '{}'
> /sbin/modinfo -F description {}
>
> I want to modify this as follows, but I don't know the syntax:
>
> /sbin/modprobe -l|xargs -l1 -I '{} basename {} .ko| xargs -l1 -I '{}' {
> echo {}; echo ' '; /sbin/modinfo -F description {} }
>
> I am trying three commands in a script as follows:
>
> { echo {}; echo ' '; /sbin/modinfo -F description {} }
>
> I assumed the { and } would make a block of code, somehow.
There is not often a need to uses "xargs -1", it can frequently be
replaced
with a shell loop.
/sbin/modprobe -l | while read name ; do
printf '%16s ' $(basename $name .ko );
/sbin/modinfo -F description $name ; done
This is written out over 3 lines, but if you remove all the newline
characters it will become a one-liner.
If you wanted to use xargs, then construct the command as a string
and give it to sh -c, e.g. something like
modprobe | xargs -1 basename | xargs -1 sh -c "echo {}; modinfo {}"
| |
| Jean-Rene David 2007-05-22, 1:25 pm |
| * Mark Hobley [2007.05.22 12:08]:
> I have a command "modprobe", which provides a list of modules:
>
> /lib/modules/2.6.18-4-486/kernel/sound/oss/uart6850.ko
> /lib/modules/2.6.18-4-486/kernel/sound/oss/uart401.ko
> /lib/modules/2.6.18-4-486/kernel/sound/oss/trix.ko
>
> (and squillions more besides)
[...]
> Now I want to echo the name and modinfo -L.
[...]
It's usually clearer to include an example of the output you
want rather than describing it in words only. This does what
I think you want.
IFS='
'
for module in $(modprobe -l)
do
paste <(basename "$module" .ko) <(modinfo -F description "$module")
done
As for being a one-liner, you can always make it that way
but I don't see the advantage.
--
JR
| |
| Stephane CHAZELAS 2007-05-22, 1:25 pm |
| 2007-05-22, 12:08(+00), Mark Hobley:
> I am using a borne compatible shell, and I want to like to try and chain a
> series of commands together using a one-liner to achieve the following:
>
> I have a command "modprobe", which provides a list of modules:
>
> /lib/modules/2.6.18-4-486/kernel/sound/oss/uart6850.ko
> /lib/modules/2.6.18-4-486/kernel/sound/oss/uart401.ko
> /lib/modules/2.6.18-4-486/kernel/sound/oss/trix.ko
>
> (and squillions more besides)
>
> I now want to strip the full path and extention, which I have achieved as
> follows:
>
> /sbin/modprobe -l|xargs -l1 -I '{}' basename {} .ko
>
> This produces:
>
> uart6850
> uart401
> trix
>
> Now I want to echo the name and modinfo -L. This is where I am stuck. I need
> to feed two commands. I want to output a filename, a space and then a
> description for each module.
>
> The following gives me a list of descriptions, but no filename:
>
> /sbin/modprobe -l|xargs -l1 -I '{}' basename {} .ko| xargs -l1 -I '{}'
> /sbin/modinfo -F description {}
[...]
modprobe -l | awk -F/ '
{print $NF; system("modinfo -F description " $0)}'
--
Stéphane
| |
| Chris F.A. Johnson 2007-05-22, 7:26 pm |
| On 2007-05-22, Stephane CHAZELAS wrote:
> 2007-05-22, 12:08(+00), Mark Hobley:
> [...]
>
> modprobe -l | awk -F/ '
> {print $NF; system("modinfo -F description " $0)}'
Better yet, since calling system() for every line is ridiculously
inefficient:
modprobe -l | while IFS= read mod
do
printf "%s\n" "${mod##*/}"
modinfo -F description "$mod"
done
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
| |
| Stephane CHAZELAS 2007-05-22, 7:26 pm |
| 2007-05-22, 15:16(-04), Chris F.A. Johnson:
[...]
>
> Better yet, since calling system() for every line is ridiculously
> inefficient:
>
> modprobe -l | while IFS= read mod
> do
> printf "%s\n" "${mod##*/}"
> modinfo -F description "$mod"
> done
[...]
you're calling read, printf and modinfo for every line. There's
no guarantee it will be more efficient, though I'd agree with
most nowadays implementations of sh (where read and printf are
built in), it is probably true.
Why you'd ask "read" to do some backslash processing is not clear
to me though.
--
Stéphane
|
|
|
|
|