I have script that checks the availability of package on my server, it iterate through array and should return [INSTALLED] if it is. Below script is modified to the basic to give you understanding what'd it do.
#!/bin/bash declare -a prog=("mysql-server" "apache2" "php" "ufw") declare -a snap=("beer") for f in "${!prog[@]}"; do for connect in "${snap[@]}"; do ssh jan@"$connect" /bin/bash <<- EOF if [ \$(dpkg --get-selections | grep -E "(^|\s)${prog[$f]}(\$|\s)" | wc -l) -gt 0 ]; then prog[$f]="${prog[$f]} [INSTALLED]" # The program is already installed echo "\${prog[@]}" else echo "False" fi EOF done done This should return the package name and [INSTALLED] suffix if it is installed, otherwise it will only return the package name.
I expect the output to be:
mysql-server [INSTALLED] apache2 [INSTALLED] php [INSTALLED] ufw Turns out the output was:
mysql-server [INSTALLED] apache2 [INSTALLED] php [INSTALLED] False ufw package is not installed on the server, I expect it to return ufw instead Not installed— if I remove the else clause altogether, ufw would not be appear on the output at all.
elseblock is coded to printFalseand that appears to be what it does; if different output is desired then update theelseblock to output the desired output; what am I missing?if [ \$(dpkg --get-selections | grep -E "(^|\s)${prog[$f]}(\$|\s)" | wc -l) -gt 0 ]just doif dpkg --get-selections | grep -qE "(^|\s)${prog[$f]}(\$|\s)"; then. But just to check if a package is installed, I think you can justif dpkg -l "${prog[$f]}"; then[is an alias to thetestcommand. If you wouldn't usetest, don't use[.progat all; just output$fon each iteration, possibly with[INSTALLED]following it if the condition is true.