Reputation: 103
the title is quite self explanatory. I'm am writing a bash script that will manage all my packages (update, upgrade every upgrade-able apt and flatpak packages as well as removing the unneeded ones.
In one of the lines I'm using xargs to automatically run the upgrade command on every upgrade-able package without having to do a while loop. I'd like to echo a simple "No packages to upgrade" in the case where there's none.
I have tried this line but it didn't work
apt-get -s --no-download upgrade -V | grep '=>' | awk '{print $1}' | { sudo xargs --no-run-if-empty apt-get --only-upgrade upgrade -y } || { echo "No package to upgrade" }
Is there a way to run a "default" command in the case where xargs is empty ?
Upvotes: 0
Views: 165
Reputation: 69318
You can create a function like this
if_not_empty() {
read -r -t 1 line
if [[ -z "$line" ]]; then
echo "$1" >&2
echo "empty"
exit 0
fi
echo "$line"
exec cat -
}
and then include it in your pipeline
mycommand | if_not_empty "No packages to upgrade" | xargs -E empty <other-xargs-args...>
Upvotes: 1