如果find为空,请不要执行下一个命令(xargs—如果为空,则不运行)



如果xargs没有运行,我如何阻止执行"if/else条件"?

find $SRC -type f -name 'file_*' | xargs --no-run-if-empty rm -v
if [ $? -eq 0 ] ; then
echo "removed"
else
echo "failed to remove"
fi

xargs:内的shell运行中移动if/else

xargs sh -c '
if [ "$#" = 0 ]; then
echo "no files to remove"
elif rm -v "$@"; then
echo "removed"
else
echo "failed to remove"
fi ' --

使用像文件这样的单独实体来传输没有输入的信息。

rm -f /tmp/tempfile
... |
xargs sh -c '
if [ "$#" = 0 ]; then
echo "no files to remove" > /tmp/tempfile
fi
rm -v "$@" ' --
# or with a separate part in a pipeline that
#   tries to read one line of input
... |
{
if ! IFS= read -r l; then
echo "no files to remove" > /tmp/tempfile
else
printf "%sn" "$l"
cat
fi
} | xargs --no-run-if-empty rm -v 

ret=$?
if [ -e /tmp/tempfile ]; then
if [ "$ret" -eq 0 ] ; then
echo "removed"
else
echo "failed to remove"
fi
fi

在255退出状态的情况下,使用GNU xargs的属性返回124。

... |
xargs sh -c '
if [ "$#" = 0 ]; then
exit 255
fi
rm -v "$@" ' --
ret=$?
if [ "$ret" -ne 124 ]; then
if [ "$ret" -eq 0 ] ; then
echo "removed"
else
echo "failed to remove"
fi
fi

相关内容

最新更新