我在我的 Linux 机器上安装了一个名为 puppet 的应用程序。它安装在位置 /usr/test/bin/puppet
这就是 .bash_profile
的样子
export PATH=/usr/test/bin
如果我从控制台运行命令 puppet apply
,它工作正常但是当我从 bash 脚本内部调用 puppet
命令时,它说找不到命令
#!/bin/bash
puppet apply x.pp
有什么问题吗?
最佳答案
.bash_profile
仅当 bash 被调用为登录 shell(bash -l
或从真实的 tty)时才加载,至少在虚拟 tty 中的基于 Debian 的发行版 bash 中(例如,当使用 xterm
、gnome-terminal
等时)作为交互式 shell 调用。
交互式 shell 从 ~/.bashrc
加载配置.
bash 手册页:
~/.bash_profile
The personal initialization file, executed for login shells
~/.bashrc
The individual per-interactive-shell startup file
Shellscripts 不加载任何这些。
您可以使用 strace
查看任何程序打开了哪些文件:
strace ./s.sh 2>&1 | grep -e stat -e open
可能的解决方案:
您可以在每个脚本的开头导出变量:
#!/bin/bash export PATH=$PATH:...
或者您可以拥有另一个包含所需变量的文件,并从任何需要这些变量的脚本中获取它:
/etc/special_vars.sh:
export PATH=$PATH:...
脚本:
#!/bin/bash . /etc/special_vars.sh puppet ...
配置
PATH
在~/.bashrc
,~/.bash_profile
和~/.profile
对于运行脚本的用户(子进程将继承环境变量)有一些保证用户可以从不同的环境和 shell 运行脚本(一些 bourne 兼容的 shell 加载~/.profile
)
关于linux - 从 bash 脚本内部调用时找不到命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27830914/