开发者

How to execute a command with one parameter at a time in the *nix shell?

开发者 https://www.devze.com 2022-12-10 00:59 出处:网络
Some commands like svn log, for example will only take one input from the command line, so I can\'t say grep \'pattern\' | svn log. 开发者_开发知识库It will only return the information for the first f

Some commands like svn log, for example will only take one input from the command line, so I can't say grep 'pattern' | svn log. 开发者_开发知识库It will only return the information for the first file, so I need to execute svn log against each one independently.

I can do this with find using it's exec option: find -name '*.jsp' -exec svn log {} \;. However, grep and find provide differently functionality, and the -exec option isn't available for grep or a lot of other tools.

So is there a generalized way to take output from a unix command line tool and have it execute an arbitrary command against each individual output independent of each other like find does?


The answer is xargs -n 1.

echo moo cow boo | xargs -n 1 echo

outputs

moo
cow
boo


try xargs:

grep 'pattern' | xargs svn log


A little one off shell script (using xargs is much better for a one off, that's why it exists)

#!/bin/sh

# Shift past argv[0]
shift 1

for file in "$@"
do
        svn log $file
done

You could name it 'multilog' or something like that. Call it like this:

./multilog.sh foo.c abc.php bar.h Makefile

It allows for a little more sanity when being called by automated build scripts, i.e. test the existence of each before talking to SVN, or redirect each output to a separate file, insert it into a sqlite database, etc.

That may or may not be what you are looking for.

0

精彩评论

暂无评论...
验证码 换一张
取 消