gpt4 book ai didi

linux - Bash 脚本 : Can't set a variable using which

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:47:50 25 4
gpt4 key购买 nike

在我的终端,

prog="cat"
name=$(which $prog)
echo $name

打印/bin/cat

但是在我的脚本中:

pro="$1"
prog=$(which $pro)
echo "pro is $pro"
echo "prog is "$prog""

运行 scriptname cat 打印

pro is cat
prog is

我如何使哪个工作?它应该打印 prog is/bin/cat

最佳答案

which(1) 是一个外部程序,用于在 PATH 中搜索可执行文件。它在不同的系统上表现不同,您不能依赖有用的退出代码;使用(从最便携到最不便携)command -vtype -P(查找路径)或 hash(检查)。

在脚本内外尝试 printf '%s\n' "$PATH"。也许您要查找的命令不在脚本中使用的 PATH 中?

这几乎肯定是原因。

关于linux - Bash 脚本 : Can't set a variable using which,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35002147/

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