我有以下命令检查是否添加了任何新文件,并在所有这些文件上自动调用svn add
svn status | grep -v "^.[ \t]*\..*" | grep "^?" | awk '{print $2}' | xargs svn add
但是当没有文件时,svn add
会发出警告。
如何从xargs
停止调用上一个命令不会导致任何值?该解决方案需要与xargs
的GNU和BSD(Mac OS X)版本一起使用。
答案 0 :(得分:9)
如果您正在运行GNU版本,请使用xargs -r
:
--no-run-if-empty -r If the standard input does not contain any nonblanks, do not run the command. Normally, the command is run once even if there is no input. This option is a GNU extension.
答案 1 :(得分:1)
如果您正在使用bash,另一种方法是将输出存储在数组中。并且仅在有输出时运行svn。
readarray -t OUTPUT < <(exec svn status | grep -v "^.[ \t]*\..*" | grep "^?" | awk '{print $2}')
[[ ${#OUTPUT[@]} -gt 0 ]] && svn add "${OUTPUT[@]}"
答案 2 :(得分:0)
我最终使用了这个。不是很优雅,但有效。
svn status | grep -v "^.[ \t]*\..*" | grep "^?" && svn status | grep -v "^.[ \t]*\..*" | grep "^?" | awk '{print $2}' | xargs svn add
答案 3 :(得分:0)
ls /empty_dir/ | xargs -n10 chown root # chown executed every 10 args
ls /empty_dir/ | xargs -L10 chown root # chown executed every 10 lines
ls /empty_dir/ | xargs -i cp {} {}.bak # every {} is replaced with the args from one input line
ls /empty_dir/ | xargs -I ARG cp ARG ARG.bak # like -i, with a user-specified placeholder