如何在 bash 脚本中识别我在哪个操作系统上

Pet*_*etr 5 bash

我需要制作每个系统行为不同的脚本。今天,甚至可以在 microsoft windows、mac、linux、hp-ux、solaris 等系统上运行 bash...

如何确定我在这些操作系统中的哪一个?我不需要确切的版本,我只需要知道我是否在 windows、linux、solaris 上...

Vor*_*ung 4

有一个标准 shell 命令“uname”,它以字符串形式返回当前平台

要在 shell 程序中使用它,典型的节可能是

#!/bin/sh



if [ `uname` = "Linux" ] ;
then
    echo "we are on the operating system of Linux"
fi

if [ `uname` = "FreeBSD" ] ;
then
    echo "we are on the operating system of FreeBSD"
fi
Run Code Online (Sandbox Code Playgroud)

我们提供了更具体的信息,但不幸的是,它因平台而异。在许多版本的 Linux(以及 ISTR、Solaris)上,都有一个 /etc/issue 文件,其中包含已安装发行版的版本名称和编号。所以在ubuntu上

if [ -e "/etc/issue" ] ;
then
issue=`cat /etc/issue`
set -- $issue
if [ $1 = "Ubuntu" ] ;
then
    echo "we are on Ubuntu version " $2
fi
fi
Run Code Online (Sandbox Code Playgroud)

这将给出版本信息