【问题标题】:Shell command executed from scala code not recognizing bash alias从 scala 代码执行的 Shell 命令无法识别 bash 别名
【发布时间】:2015-08-18 23:26:12
【问题描述】:

我有一个执行类似这样的 hdfs 命令的 scala 文件:

hdfs dfs -mkdir /test/

当我在 shell 中运行该命令时,它运行良好,因为我在 .bashrc 文件中设置了以下别名:

alias hdfs='/usr/local/hadoop-2.7.0/bin/hdfs'

...但是,当我使用 SBT 在我的 Scala 代码中构建运行 shell 命令的代码时,我收到以下错误:

 java.io.IOException: Cannot run program "hdfs": error=2, No such file or directory

有没有办法识别别名?我无法更改代码,因为我希望程序在任何安装了 hdfs 的系统上运行,无论目录如何,唯一应该更改的是别名。

更多信息: 我正在使用 hadoop 的 docker 映像中运行我的测试(不应更改任何内容),该映像运行一个脚本,每次容器启动时都会执行以下两行:

source ~/.bashrc         # To get the alias
shopt -s expand_aliases  # To allow alias expansion in your script

【问题讨论】:

  • @hek2mgl 不,我已经看过并尝试了他们的解决方案(包含在我的答案中),但它们不起作用,所以我认为问题不同。
  • 如果您将 shell 作为交互式 shell 启动,它将读取您的 ~/.bashrc 并允许别名:bash -i 'hdfs dfs -mkdir /test/'

标签: linux bash sbt


【解决方案1】:

修复了我的 .bashrc 文件中的以下行

export PATH=$PATH:/usr/local/hadoop/bin/

【讨论】:

    最近更新 更多