Home > OS >  System Variable set in bash not sticking after i go to an IF statement
System Variable set in bash not sticking after i go to an IF statement

Time:11-29

apacherelease=$(curl -s "https://httpd.apache.org" | grep Released | awk '{print $4}' | perl -p  -e 's/2.4.54/2.4.54-1/g') &&
apacheinstallversion=$(dnf list installed | grep httpd.x86_64|awk '{print $2}') &&
echo $apacherelease
echo $apacheinstallversion


if test "$apacheinstallversion" = "$apacherelease"; then
: variables are the same
 else
 : variables are different
 fi

`

If I run the commands to set variable directly from the command line instead of a script the variables stick however in the script they disappear the moment I move to the if statement.

Any input would extremely help!

CodePudding user response:

Corrected version:

apacherelease=$(curl -s "https://httpd.apache.org" | grep Released | awk '{print $4}' | perl -p  -e 's/2.4.54/2.4.54-1/g') &&
apacheinstallversion=$(dnf list installed | grep httpd.x86_64 | awk '{print $2}')
echo "$apacherelease"
echo "$apacheinstallversion"


if [[ $apacheinstallversion == $apacherelease ]]; then
    echo "variables are the same"
else
    echo "variables are different" >&2
fi
  • use the full featured bash test [[
  • use == instead of =
  • Related