开发者

linux shell stream redirection to run a list of commands directly

开发者 https://www.devze.com 2023-03-23 14:47 出处:网络
I have this svn project... to get a list of unadded files (in my case, hundreds): svn status |grep \"^?\"

I have this svn project... to get a list of unadded files (in my case, hundreds):

svn status |grep "^?"

outputs

? somefile.txt
? somefile1.txt
? somefile2.txt

I was recently introduced to sed... so now I have a list of commands I want to run

svn status | grep "^?"|sed "s/^?/svn add/"

outputs

svn add somefile.txt
svn add somefile1.txt
svn add somefile2.txt

I realize I could just pipe it to a file

svn status 开发者_C百科| grep "^?"|sed "s/^?/svn add/" >out.sh && sh out.sh && rm out.sh

But I'd like to avoid writing to a temporary file. Is there a way I pipe it to some command like this:

svn status | grep "^?"|sed "s/^?/svn add/" |some_command_that_runs_each_line


What about bash/sh?

svn status | grep "^?"|sed "s/^?/svn add/" | bash


What you are looking for is the xargs command:

svn status | grep "^?" | sed "s/^..//" | xargs svn add

You can also use substitution:

svn add `svn status | grep "^?"` | cut -c 3-`


How about:

for i in `svn status | grep "^?"|sed "s/^?/svn add/"`
do
    $i
done
0

精彩评论

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