linux - 无法从另一个 shell 脚本执行脚本

标签 linux bash shell

我有一个脚本 (A),它以 #!/bin/sh shebang 开头。

还有一个文件(B)不是以#!/bin/sh开头;它有一些我需要设置的环境变量,A 才能工作。

文件 B 运行良好,并在从 .bash_profile 执行时设置变量。例如,在我的 .bash_profile 文件中:

{. /(full_path)/(envname)/(filename).sh;}

当我在我的 A 脚本文件中写入同一行时,没有任何反应。为什么不呢?

我做了echo $env_variable_name;这在通过 .bash_profile 执行时给了我正确的路径,但在通过我的脚本 A 时给了我空白。我试过了

  • /(full_path)/(envname)/(filename).sh
    
  • SCRIPT_FULL_NAME=$(readlink -f "$0")
    SCRIPT_PATH=$(dirname $SCRIPT_FULL_NAME)
    sh ${SCRIPT_PATH}/(filename).sh
    

我该怎么办?

最佳答案

There is another file (B) which doesn't begin with #!/bin/sh; it has some environment variables which I need to set for A to work.

在这种情况下,您应该在脚本 A 中获取您的脚本 B。要在 sh 中获取脚本,命令是:

. /path/to/B.sh

请注意,. 不是脚本 B 的相对路径的开头。

关于linux - 无法从另一个 shell 脚本执行脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37540576/

相关文章:

linux - dpkg:无法创建备份符号链接(symbolic link)

linux - shell脚本中的字符串连接

eclipse - 如何更改默认的 eclipse shell 解释器?

php - ps 辅助 | grep php --- 检查 GET 参数

Android 设备选择器不显示我的设备,但 adb 和 Eclipse 显示

Python 进程由于打开 Paramiko ssh 连接而挂起

linux - cron 任务的 bash 脚本

linux - 从 bash 脚本中的字符串中提取模式

linux - 如何在 USB 驱动器上运行 Docker 容器?

Bash 和 (graphviz) 点输出重定向