skip to Main Content

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?

3

Answers


  1. 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
    
    Login or Signup to reply.
  2. Try this:

    #!/bin/sh
    grep -qE 'AlmaLinux|RockyLinux 8' /etc/redhat-release
    if [ $? ]; then
      echo "your system is supported"
    else
      echo "Unsupported OS"
    fi
    
    Login or Signup to reply.
  3. Other perfectly valid detection:

    #!/bin/sh
    
    NAME=unknown
    VERSION='??'
    if . /etc/os-release && case $NAME in
      *Rocky*)
        case $VERSION in
          8*) ;;
          *) false ;;
        esac
        ;;
      *AlmaLinux*) ;;
      *) false ;;
    esac
    then
      printf 'Your system %s version %s is supportedn' "$NAME" "$VERSION"
    else
      printf '%s %s is not supported!n' "$NAME" "$VERSION" >&2
      exit 1
    fi
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search