0

I need some help with a bit of shell script.

The code below works, and if sed is found then it continues with the script but if it doesn't exist it will exit.

  if ! [ -x "$(command -v sed)" ]; then
    echo "Error: sed is not installed, please install sed." >&2
    exit
  fi

What would I have to change to make it if the system finds ufw than it will run those commands.

    if ! [ -x "$(command -v ufw)" ]; then
      ufw allow 80/tcp
      ufw allow 443/tcp
    fi
Dave M
  • 4,514
  • 22
  • 31
  • 30

1 Answers1

0

Simply remove the exclamation mark, which means "not". So now instead of checking if the command is "not" there, we are checking if it does exist.

   if [ -x "$(command -v ufw)" ]; then 
      ufw allow 80/tcp
      ufw allow 443/tcp
   fi

Another option, if you want to catch it first and exit if your conditions aren't met.

   if ! [ -x "$(command -v ufw)" ]; then 
        echo "Your error message here"
        exit # Stop execution
   fi
        # below code only runs if command exists
        ufw allow 80/tcp
        ufw allow 443/tcp



turbo
  • 36
  • 3