0

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?

aynber
  • 22,380
  • 8
  • 50
  • 63

3 Answers3

2

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
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Kristof Rado
  • 713
  • 1
  • 8
  • 19
  • 2
    Eventually get ride of the useless `cat` with: `if grep -q -E 'AlmaLinux|RockyLinux 8' /etc/redhat-release; then` – Léa Gris May 19 '22 at 22:05
0

Try this:

#!/bin/sh
grep -qE 'AlmaLinux|RockyLinux 8' /etc/redhat-release
if [ $? ]; then
  echo "your system is supported"
else
  echo "Unsupported OS"
fi
Dean Householder
  • 549
  • 1
  • 7
  • 13
  • 2
    This will fail if the result from `grep` has more than one word (e.g. "RockyLinux 8"). You could fix this by double-quoting the the command substitution, but it's really better to just use the exit status of `grep -q` directly. – Gordon Davisson May 19 '22 at 20:12
  • Good catch. Updated. – Dean Householder May 19 '22 at 20:17
  • 2
    Now it'll fail if `grep` prints more than one line. (I don't know the format of `/etc/redhat-release`, but if it's like `/etc/lsb-release` or `/etc/os-release` on Ubuntu, it might have more than one line with the name of the distro.) You want `-ge 1`, but again, it's easier to just use `grep -q`. – wjandrea May 19 '22 at 20:29
  • 1
    Why doing a `wc`? The exit code of `grep` already tells you whether or not you have a match. – user1934428 May 20 '22 at 05:24
0

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 supported\n' "$NAME" "$VERSION"
else
  printf '%s %s is not supported!\n' "$NAME" "$VERSION" >&2
  exit 1
fi
Léa Gris
  • 17,497
  • 4
  • 32
  • 41