How to check if running in Cygwin, Mac or Linux?

后端 未结 11 876
忘掉有多难
忘掉有多难 2020-11-27 23:58

I have a shell script that is used both on Windows/Cygwin and Mac and Linux. It needs slightly different variables for each versions.

How can a shell/bash script de

11条回答
  •  攒了一身酷
    2020-11-28 00:51

    Ok, here is my way.

    osis()
    {
        local n=0
        if [[ "$1" = "-n" ]]; then n=1;shift; fi
    
        # echo $OS|grep $1 -i >/dev/null
        uname -s |grep -i "$1" >/dev/null
    
        return $(( $n ^ $? ))
    }
    

    e.g.

    osis Darwin &&
    {
        log_debug Detect mac osx
    }
    osis Linux &&
    {
        log_debug Detect linux
    }
    osis -n Cygwin &&
    {
        log_debug Not Cygwin
    }
    

    I use this in my dotfiles

提交回复
热议问题