Home > Software engineering >  How to match one of two strings as a condition in bash
How to match one of two strings as a condition in bash

Time:05-20

firstly I had very simple script like this

#!/bin/sh
if cat /etc/redhat-release | grep -q 'AlmaLinux'; then
    echo "your system is supported"
    MY SCRIPT HERE
else
    echo "Unsupported OS"
    exit0;
fi

and it works but I want to add another correct value that will also return "your system is supported" and let me pass the script to go further

so for example if file /etc/redhat-release will contain AlmaLinux or Rockylinux 8 it will work for both AlmaLinux and Rockylinux but if it will contain Centos 6 it will not go further

I tried something along this:

#!/bin/sh
if cat '/etc/redhat-release' | grep -q 'AlmaLinux'|| | grep -q 'RockyLinux 8'; then
    echo "your system is supported"
else
    echo "Unsupported OS"
fi

but it gives me an error and I am even not sure if this is a right syntax.

Can anyone help me?

CodePudding user response:

Maybe by using a regex pattern?

#!/bin/sh
if cat '/etc/redhat-release' | grep -q -E 'AlmaLinux|RockyLinux 8'; then
    echo "your system is supported"
else
    echo "Unsupported OS"
fi

CodePudding user response:

Try this:

#!/bin/sh
if [ $(grep -E 'AlmaLinux|RockyLinux 8' /etc/redhat-release | wc -l) -eq 1 ]; then
  echo "your system is supported"
else
  echo "Unsupported OS"
fi
  • Related