본문 바로가기

리눅스

리눅스에서 운영체제 및 버전을 구분하는 스크립트

728x90

리눅스에서 운영체제 및 버전을 구분하는 스크립트

  • 스크립트를 통해 운영체제 및 버전을 더 쉽게 판단할 수 있습니다.

os_checkv2.sh 스크립트 작성

vim os_checkv2.sh
#!/bin/bash

# 운영체제 판단 및 lsb-release 설치
if command -v apt >/dev/null; then
    # Ubuntu
    echo "Installing lsb-release on Ubuntu"
    apt update -qq -y >/dev/null 2>&1
    apt install -qq -y lsb-release >/dev/null 2>&1
    distro=$(lsb_release -i | cut -f2)
    os_version=$(lsb_release -sr | cut -d'.' -f1)
elif command -v yum >/dev/null; then
    # CentOS / RedHat
    echo "Installing lsb-release on CentOS"
    yum install -q -y redhat-lsb-core >/dev/null 2>&1
    distro=$(lsb_release -i | cut -f2 | tr -d '"')
    os_version=$(lsb_release -sr | cut -d'.' -f1)
else
    echo "Other OS"
    exit 1
fi

# 운영체제 버전 출력
if [ "$distro" == "CentOS" ]; then
    if [ $os_version -ge 8 ]; then
        echo "CentOS $os_version"
    elif [ $os_version -ge 7 ]; then
        echo "CentOS $os_version"
    elif [[ $os_version -eq 6 || $os_version -eq 5 ]]; then
        echo "CentOS $os_version"
    else
        echo "$distro OS"
    fi
elif [ "$distro" == "Ubuntu" ]; then
    echo "Ubuntu $os_version"
else
    echo "Other OS"
fi

스크립트 실행

$ bash os_checkv2.sh
install lsb-release on Ubuntu
Ubuntu 18
$ bash os_checkv2.sh
install lsb-release on Ubuntu
Ubuntu 20
$ sh os_checkv2.sh
install lsb-release on CentOS
CentOS 7
728x90

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