通过shell脚本确定当前平台

发布时间:2024年01月13日

? ? ? shell中的变量OSTYPE存储操作系统的名称也可以使用uname命令来确认当前所在的平台。
? ? ? shell中的变量HOSTTYPE存储操作系统的架构

? ? ? 测试代码如下所示:

#! /bin/bash

echo "use OSTYPE:"
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
    echo "Linux platform"
elif [[ "$OSTYPE" == "darwin"* ]]; then
    echo "Mac platform"
elif [[ "$OSTYPE" == "cygwin" ]]; then
    echo "Windows platform: Cygwin"
elif [[ "$OSTYPE" == "msys" ]]; then
    echo "Windows platform: MinGW"
# elif [[ "$OSTYPE" == "win32" ]]; then # not sure
#     echo "Windows platform"
elif [[ "$OSTYPE" == "freebsd"* ]]; then
    echo "FreeBSD platform"
else
    echo "Unknown platform: $OSTYPE"
fi

echo -e "\nuse uname:"
#os="`uname`" # os="$(uname)"
#echo "platform: ${os}" # echo "platform: $(uname)"
if [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
    echo "Linux platform"
elif [ "$(uname)" == "Darwin" ]; then
    echo "Mac platform"
elif [ "$(expr substr $(uname -s) 1 9)" == "CYGWIN_NT" ]; then
    echo "Windows platform: Cygwin"
elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW64_NT" ]; then
    echo "Windows platform: MinGW 64"
else
    echo "Unknown platform: $(uname)"
fi

echo -e "\nsystem architecture: $HOSTTYPE"

echo "test finish"

? ? ? 执行结果如下图所示:

? ? ? GitHubhttps://github.com/fengbingchun/Linux_Code_Test

文章来源:https://blog.csdn.net/fengbingchun/article/details/135576216
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。