【问题标题】:bash 'declare' can not be used in a script?bash 'declare' 不能在脚本中使用?
【发布时间】:2013-08-23 08:56:50
【问题描述】:

我的 `.bashrc' 文件中定义了一些 bash 函数。在 emacs 中 shell-mode,我想用完整的函数名。

emacs `shell' 包默认不能完成功能,所以我 计划加强它。

问题是我无法获取 shell 的当前函数定义。这 查询函数定义的常用方法是键入:

   $ declare -F

由于'declare'只是一个内置命令,我们应该将它包装在一个 脚本文件然后用 `bash' 调用它。使用 emacs 我们可以:

   * create a file, wrap the code:

     -------------- file: get-functions -----------

     declare -F

     -------------- file end ----------------------

   * in emacs, eval the expression:

     (call-process "bash" "get-functions" "output-buffer")

但是,我在“输出缓冲区”中一无所获。有趣的是,如果我 在真正的 shell 中运行脚本:

  $ bash get-functions

我仍然没有任何输出。

这是否意味着我们不能在脚本中使用`declare -F'?

顺便说一句,如果我们从脚本文件中删除“-F”选项,那么我们可以 调用“bash get-functions”时得到输出,但我们得到的不是函数 定义,但是是所有变量。

我的环境:GNU bash,版本 4.1.2(1)-release (i386-redhat-linux-gnu),CentOS 6.3 32bits

【问题讨论】:

标签: bash emacs


【解决方案1】:

还没有解决方案,只是为了检查“declare -F”是否有效:

(shell)
(comint-send-string (get-buffer-process "*shell*") "declare -F")
(comint-send-string (get-buffer-process "*shell*") "\n")
(switch-to-buffer "*shell*") 

【讨论】:

  • 这就像你在下层shell缓冲区中输入“declare -F”一样。
【解决方案2】:

从我的脑海中,我猜这与未导出的函数有关(而 bash 当前无法导出函数)。 换句话说:你必须获取你的函数。

...您也可以通过使用登录 shell 来解决这个问题,但对我来说,立即采购似乎更容易。

【讨论】:

  • 函数写在登录shell的rc文件中。这意味着函数已经被采购了。
  • 显然它们不是来源的——它们不是继承的 :) 您可以尝试添加一个简单的 '. ~/.bashrc' 在脚本的顶部。
猜你喜欢
  • 2013-11-13
  • 1970-01-01
  • 2016-03-21
  • 1970-01-01
  • 2015-01-16
  • 2015-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多