본문 바로가기

기타

[스크립트] 운영체제 구분 스크립트

728x90

운영체제 구분 스크립트

CentOS, Ubuntu 구분하여 스크립트를 적용하기 위함

#!/bin/bash

if command -v apt >/dev/null; then
    echo "install lsb-release on Ubuntu"
    apt update -qq -y >/dev/null 2>&1
    apt install -qq -y lsb-release >/dev/null 2>&1
    lsb_release -ds
elif command -v yum >/dev/null; then
    echo "install lsb-release on CentOS"
    yum install -q -y redhat-lsb-core >/dev/null 2>&1
    lsb_release -ds | tr -d '"'
else
    echo "other OS"
fi

distro=$(lsb_release -i | cut -f2)
os_version=$(lsb_release -sr | cut -d'.' -f1)

if [ "$distro" == "CentOS" ]; then
    if [ $os_version -ge 8 ]; then
        echo "$distro $os_version"
    elif [ $os_version -ge 7 ]; then
        echo "$distro $os_version"
    elif [[ $os_version -eq 6 || $os_version -eq 5 ]]; then
        echo "$distro $os_version"
    else
        echo "$distro OS"
    fi
elif [ "$distro" == "Ubuntu" ]; then
    echo "$distro $os_version"
else
    echo "Other OS"
fi
$ bash z.sh
install lsb-release on Ubuntu
Ubuntu 18.04.6 LTS
Ubuntu 18
$ bash z.sh
install lsb-release on Ubuntu
Ubuntu 20.04.4 LTS
Ubuntu 20
$ sh z.sh
install lsb-release on CentOS
CentOS Linux release 7.9.2009 (Core)
CentOS 7

command 사용법(help)

$ command --help
command: command [-pVv] command [arg ...]
    Execute a simple command or display information about commands.

    Runs COMMAND with ARGS suppressing  shell function lookup, or display
    information about the specified COMMANDs.  Can be used to invoke commands
    on disk when a function with the same name exists.

    Options:
      -p    use a default value for PATH that is guaranteed to find all of
            the standard utilities
      -v    print a description of COMMAND similar to the `type' builtin
      -V    print a more verbose description of each COMMAND

    Exit Status:
    Returns exit status of COMMAND, or failure if COMMAND is not found.

lsb_release 사용법(help)

$ lsb_release --help
Usage: lsb_release [options]

Options:
  -h, --help         show this help message and exit
  -v, --version      show LSB modules this system supports
  -i, --id           show distributor ID
  -d, --description  show description of this distribution
  -r, --release      show release number of this distribution
  -c, --codename     show code name of this distribution
  -a, --all          show all of the above information
  -s, --short        show requested information in short format
728x90