MST

星途 面试题库

面试题:Bash 中条件编译的基础实现

请描述如何在 Bash 脚本中实现简单的条件编译,比如根据不同的操作系统类型执行不同的代码块。假设通过判断 `uname -s` 的输出结果来区分 Linux 和 macOS 系统。请给出具体的脚本示例。
40.5万 热度难度
编程语言Bash

知识考点

AI 面试

面试题答案

一键面试
#!/bin/bash

os_type=$(uname -s)

if [ "$os_type" == "Linux" ]; then
    echo "This is Linux, execute Linux - specific code here"
    # 在这里添加 Linux 特定的代码
elif [ "$os_type" == "Darwin" ]; then
    echo "This is macOS, execute macOS - specific code here"
    # 在这里添加 macOS 特定的代码
else
    echo "Unsupported operating system"
fi