【问题标题】:The fish shell and executing programs from bash through `function`通过`function`从bash执行fish shell和程序
【发布时间】:2015-01-08 18:42:16
【问题描述】:

我目前正在尝试运行 atom editor 在bash 外壳中,来自fish 外壳。由于 ide-haskell 如何处理 ghc-mod 路径解析以及其他一些标准化问题,我在 bash 中运行 atom 很重要。

这是我的做法:

#~/.config/fish/config.fish

function start-atom
  bash $HOME/lib/atom/bin/Atom/atom $argv
end

但是,当我尝试从 fish 运行 start-atom 时,出现以下错误:

/home/athan/lib/atom/bin/Atom/atom: /home/athan/lib/atom/bin/Atom/atom: cannot execute binary file

即使我知道这个文件是正确且可执行的。有任何想法吗?谢谢!

【问题讨论】:

  • atom 的 shebang 是什么?如果它以#!/bin/bash 开头,无论如何都会用bash 运行。
  • ...相反,如果您依赖于通过~/.bashrc、~/.bash_profile 等添加的功能,那么您将无法通过您正在做的事情获得这些功能无论如何,因为这些只是为 interactive shells 提供的。
  • (关于一个完全不同的主题——shell 函数和别名具有非常不同的语义;函数不是别名的子类型)。
  • @Charles,在fish中,别名是作为函数实现的。 alias foo bar 变成 function foo; bar $argv; end
  • @glennjackman,使别名成为一种函数,而不是使函数成为一种别名。

标签: bash executable fish


【解决方案1】:

当您运行 bash file_name 时,这意味着您正在尝试将 file_name 作为 bash 脚本运行。

试试这个:

bash -c '$HOME/lib/atom/bin/Atom/atom "$@"' dummy $argv

-c 的意思是“用 bash 运行这个命令”而不是“用 bash 运行这个脚本”。

正如 Charles 在 cmets 中指出的那样,我们必须进行一些调整才能将参数传递给命令。我们将它们传递给bash,它将在提供的命令中将它们用作位置参数,因此$@。

【讨论】:

  • 这意味着给定的 argv 将被传递给 shell,而不是传递给 atom 脚​​本(因此当运行 $HOME/lib/atom/bin/Atom/atom 时,参数将在 $@ 中)。这真的是我们想要的吗?我想我们希望将 argv 传递给 atom 脚本本身。
  • ...否则,它可能更像bash -c "$HOME"'/lib/atom/bin/Atom/atom "$@"' $argv,但可能带有一些鱼本地修复。 (重点是如果使用-c 传递的命令不引用$@,那么额外的参数将被忽略)。
  • @CharlesDuffy - 啊,看来我误解了手册页。它说-c string If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parameters, starting with $0. 我认为它是指命令的位置参数,而不是bash 本身。
【解决方案2】:

应该是:bash -c '$HOME/lib/atom/bin/Atom/atom "$@"' _ $argv

下划线会变成bash的$0

一个演示:

$ function test_bash_args
      bash -c 'printf "%s\n" "$@"' _ $argv
  end
$ test_bash_args one two three
one
two
three

如果您需要该 bash 会话来加载配置,请将其设为登录 shell。

所以,底线:~/.config/fish/functions/start-atom.fish

function start-atom
    bash -l -c '$HOME/lib/atom/bin/Atom/atom "$@"' _ $argv
end

【讨论】:

    猜你喜欢
    • 2014-02-19
    • 1970-01-01
    • 2014-02-09
    • 2011-10-23
    • 1970-01-01
    • 2015-09-21
    • 2012-07-25
    • 2012-11-03
    • 2013-09-02
    相关资源
    最近更新 更多