Following my previous question which got closed— basically I have a script that check availability of packages on target server, the target server and the packages have been stored to an array.
declare -a prog=("gdebi" "firefox" "chromium-browser" "thunar")
declare -a snap=("beer2" "beer3")
# checkvar=$(
for f in "${prog[@]}"; do
for connect in "${snap[@]}"; do
ssh lg@"$connect" /bin/bash <<- EOF
if dpkg --get-selections | grep -qE "(^|\s)"$f"(\$|\s)"; then
status="[INSTALLED] [$connect]"
else
status=""
fi
printf '%s %s\n' "$f" "\$status"
EOF
done
done
With the help of fellow member here, I've made several fix to original script, script ran pretty well— except there's one problem, the output contain duplicate entries.
gdebi [INSTALLED] [beer2]
gdebi
firefox [INSTALLED] [beer2]
firefox [INSTALLED] [beer3]
chromium-browser [INSTALLED] [beer2]
chromium-browser [INSTALLED] [beer3]
thunar
thunar
I know it this is normal behavior, as for
pass multiple server from snap
array, making ssh
travel to all the two server.
Considering that the script checks same package for two server, I want the output to be merged.
If
beer2
havefirefox
packages, butbeer3
doesn't.firefox [INSTALLED] [beer2]
If
beer3
havefirefox
packages, butbeer2
doesn't.firefox [INSTALLED] [beer3]
If both
beer2
andbeer3
have the packages.firefox [INSTALLED] [beer2, beer3]
or
firefox [INSTALLED] [beer2] [beer3]
If both
beer2
andbeer3
doesn't have the package, it will return without extra parameter.firefox
Sound like an easy task, but for the love of god I can't find how to achieve this, here's list of things I have tried.
- Try to manipulate the
for
loops. - Try putting return value after one successful loops (exit code).
- Try nested
if
. All of the above doesn't seem to work, I haven't tried changing/manipulate the return string as I'm not really experienced with some text processing such as:awk
,sed
,tr
and many others.
Can anyone shows how It's done ? Would really mean the world to me.