【问题标题】:How to pass arguments to /bin/bash? [closed]如何将参数传递给 /bin/bash? [关闭]
【发布时间】:2014-04-12 02:21:50
【问题描述】:

在我的终端中,我会 $ myprogram myfile.ext.

我希望能够通过命令/bin/bash 执行相同的操作,但它不起作用。

我试过了

$ /bin/bash -c -l myprogram myfile.ext

但它不起作用。似乎my program 已启动,但没有我的file.ext 作为选项(作为输入文件)

有人知道怎么做吗?


我为什么要问这个问题

我想通过NSTask 在我为OSX 编写的程序中使用cocoa 框架启动myprogram。如果我只是要求NSTask 启动我的程序,似乎缺少一些variable environment。所以,我尝试的是启动一个 shell 并在这个 shell 内启动myprogram

【问题讨论】:

  • 请提供您正在尝试执行的操作的完整上下文,包括任何使其成为特殊情况的环境条件。根据您对 cme​​ts 的回答,这不仅仅是使用要运行的文件名调用 bash 的情况。

标签: bash cocoa unix terminal nstask


【解决方案1】:

完全放弃-c。从手册页:

bash [options] [file]
…
Bash  is  an  sh-compatible command language interpreter that executes commands read from the standard input or from a file.

因此,您可以通过

执行您的程序
bash myprogram myfile.ext

myfile.ext 将是第一个位置参数。

bash -l myprogram myfile.ext

也可以,(但是否将 bash 作为登录 shell 调用似乎与这个问题无关。)

【讨论】:

    【解决方案2】:
    -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.
    

    您需要引用作为字符串传入的命令,以便将其视为-c 选项的单个参数,然后使用命令后面的参数正常执行,即

    /bin/bash -c -l 'myprogram myfile.ext'
    

    【讨论】:

    • 谢谢!!如果我在终端中键入此命令,它就可以工作。但是,如果尝试通过fork()exec()执行它,它就不起作用了......有什么想法吗?
    • 我得到的错误是/bin/bash: myprogarm myfile.ext No such file or directory/bin/bash -c -l 'myprogram' 有效。
    • @Colas 尝试提供myprogarm的完整路径
    • 我也试过了,但结果是一样的。 :(
    • @Colas 你能显示完整的execfork 你正在尝试吗?
    猜你喜欢
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-26
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    相关资源
    最近更新 更多