You can just use the sh
option mentioned and enclose the command in single quotes as show below with an extra equivalent if
version:
#Your example
echo 95 | xargs -n1 -I{} sh -c '[ {} -ge 95 ] && echo "No Space on disk {}% full -- remove old backups please"'
#if equivalent
echo 95 | xargs -n1 -I{} sh -c 'if [ {} -ge 95 ]; then echo "No Space on disk {}% full -- remove old backups please"; fi'
Using xargs
with alias in -I
option for better readability
echo 95 | xargs -n1 -IusedSpace sh -c 'if [ usedSpace -ge 95 ]; then echo "No Space on disk usedSpace% full -- remove old backups please"; fi'
With if and chain as many command as You wish separated by semicolon ; below example with 2 and 4 commands chained
# 2 independent commands
echo 95 | xargs -n1 -I{} sh -c 'if [ {} -ge 95 ]; then echo "No Space on disk {}% full -- remove old backups please"; echo "Value processed {}"; fi'
# 4 independent commands
echo 95 | xargs -n1 -I{} sh -c 'if [ {} -ge 95 ]; then echo "No Space on disk {}% full -- remove old backups please"; echo "Space left on disk $( expr 100 - {})%"; echo "Space used {}%"; echo "Value processed {}"; fi'
I added the else
case in your example, It prints the left disk space
#If with else block
echo 80 | xargs -n1 -I{} sh -c 'if [ {} -ge 95 ]; then echo "No Space on disk {}% full -- remove old backups please"; else echo "Space left on disk $( expr 100 - {})%"; fi'
#Equivalent
echo 80 | xargs -n1 -I{} sh -c '[ {} -ge 95 ] && echo "No Space on disk {}% full -- remove old backups please"; [ {} -ge 95 ] || echo "Space left on disk $( expr 100 - {})%"'
Another modification could be isolate the 95 percent of limit usage in a variable as show below:
echo 96 | xargs -n1 -IusedSpace sh -c 'usageLimit=95; if [ usedSpace -ge ${usageLimit} ]; then echo "No Space on disk usedSpace% full -- remove old backups please"; else echo "Space left on disk $( expr 100 - usedSpace)%"; fi'
Happy scripting!!!
@enrique