xargs Multiple Commands – Using Same Argument in Shell

command lineshellxargs

Am trying to write a one-liner that can probe the output of df -h and alert when one of the partitions is out [or almost] of space. It's the part using xargs that kicking me in the ass now…

echo 95 | xargs -n1 -I{} [ {} -ge 95 ] && echo "No Space on disk {}% full -- remove old backups please"

How can i make the second {} show "95" too?

Best Answer

That && is not part of the xargs command, it's a completely separate invocation. I think you'll want to explicitly execute a subshell:

echo 95 | xargs -n1 -I_percent -- sh -c '[ _percent -ge 95 ] && echo "No Space on disk _percent% full -- remove old backups please"'

Note also I'm using _percent instead of {} to avoid extra quoting headaches with the shell. It's not a shell variable; still just an xargs replacement string.

Related Question