gpt4 book ai didi

windows - 根据 bash 中的操作系统运行不同命令的最佳方法

转载 作者:可可西里 更新时间:2023-11-01 09:25:26 26 4
gpt4 key购买 nike

在 bash 脚本中,有一种“官方”方式可以根据操作系统版本等运行不同的命令。我的意思是您基本上可以在顶部设置一次,然后在其他地方以相同的方式调用它。我试过使用别名,但这似乎是一个废话,在某些系统上并没有真正起作用(一个是使用 win-bash 的 Windows 7)。

例如,这是我尝试过的:

if [ "$(uname)" = "Darwin" ]; then
alias p4cli=./bin/p4
else
alias p4cli=C:\bin\p4.exe
fi

p4cli login

如果我使用 shopt -s expand_aliases 它可以在 Mac 上运行,但 win-bash 没有 shopt。我假设有比别名更好的方法来做到这一点?

最佳答案

要确定 bash 中的底层操作系统,最好依赖环境变量 OSTYPE。 bash 手册页说变量 OSTYPE 存储操作系统的名称:

OSTYPE Automatically set to a string that describes the operating system on which bash is executing. The default is system- dependent.

if [[ "$OSTYPE" == "darwin"* ]]; then
p4cli="./bin/p4"
else
p4cli="C:\bin\p4.exe"
fi

"$p4cli" login

关于windows - 根据 bash 中的操作系统运行不同命令的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13827157/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com