【问题标题】:How to source an external file in .bash_profile macOS如何在 .bash_profile macOS 中获取外部文件
【发布时间】:2020-10-23 00:19:00
【问题描述】:

我试图将我的脚本从 linux 移动到 mac,文件夹 ~/Scripts 与所有其他脚本一起放在家里,
但在.bash_profile 中使用源代码不会调用我的脚本。

.bash_profile 的内容

f [ -f ~/.bashrc ]; then
    . ~/.bashrc
    . ~/Scripts/ipmitool
fi

alias test='echo test from bash_profile'

/Scripts/ipmitool 的内容

#!/bin/bash

if [[ -z $1 ]] ; then
    printf "ipmitool <Host IP> \n\n"
else
    ssh con_1 ipmitool -I lanplus -U * -P * -H  $1 fru print
fi

终端

1232324fa:~ OiO$ ipmitool
-bash: ipmitool: command not found

1232324fa:~ OiO$ bash ~/Scripts/ipmitool
ipmitool <Host IP> 

我需要在前面使用bash才能调用ipmitool,在linux中我只需要输入ipmitool。

我尝试关注此How to source an external file in .bash_profile in OSX?,但没有成功。

有人可以帮助这个新手吗,谢谢。

【问题讨论】:

  • 我猜你的 .bash_profile 中唯一的 f 应该是 if。做一个set -x; . ~/.bash_profle 来调试它。您是如何验证您的 .bash_profile 的来源?

标签: bash macos terminal alias


【解决方案1】:

您似乎不想获取它。您想将~/Scripts 添加到您的路径中,以便稍后您可以键入ipmitool 而不是~/Scripts/ipmitool 来执行它。

if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

PATH=~/Scripts:$PATH

alias test='echo test from bash_profile'

(通过运行确保~/Scripts/ipmitool 也是可执行的

chmod +x ~/Scripts/ipmitool

)

【讨论】:

  • 抱歉我的愚蠢问题,我错过了要粘贴的那部分脚本,因为我对路径和源代码缺乏了解。你能给我一个简单的解释源和路径有什么区别吗?非常感谢。
  • PATH 只是告诉外壳程序在您尝试仅按名称运行命令时在哪里查找命令。 Sourcing 实际上在当前 shell 进程中立即执行文件的内容。
猜你喜欢
  • 2023-01-17
  • 2012-10-06
  • 2020-01-24
  • 2020-12-04
  • 1970-01-01
  • 2010-11-28
  • 2019-04-18
  • 2011-04-20
  • 2021-12-09
相关资源
最近更新 更多