Linux:在脚本中运行二进制文件
我想通过脚本运行一个程序。 通常我在 shell 中输入 ./program
,程序就会启动。
我的脚本看起来像这样:
#!/bin/sh
cd /home/user/path_to_the_program/
sh program
它失败了,我认为最后一行出错了......
我知道这是幼稚的问题,但非常感谢!
i want to run a program via script.
normally i type ./program
in the shell and the program starts.
my script looks like this:
#!/bin/sh
cd /home/user/path_to_the_program/
sh program
it fails, i think the last line went wrong...
i know this is childish question but thx a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果
./program
在 shell 中工作,为什么不在脚本中使用它呢?sh program
启动 sh 来尝试将program
解释为 shell 脚本。它很可能不是脚本,而是其他一些可执行文件,这就是它失败的原因。If
./program
works in the shell, why not use it in your script?sh program
launches sh to try and interpretprogram
as a shell script. Most likely it's not a script but some other executable file, which is why it fails.当您键入时,
shell 会尝试根据它确定需要执行的文件的方式来执行程序。如果它是二进制文件,它将尝试执行入口子例程。如果 shell 检测到它是一个脚本,例如通过使用
或
或更一般地,
shell 会将文件(以及任何提供的参数)作为参数传递给提供的解释器,然后解释器将执行该脚本。如果路径中给定的解释器不存在,shell 将出错,如果未找到解释器行,shell 将假定所提供的脚本将由其自身执行。
命令
相当于
程序的第一行包含
假设 /bin/sh 是路径中的 sh(例如,可以是 /system/bin/sh)。将二进制文件传递给 sh 会导致 sh 将其视为 shell 脚本,但事实并非如此,并且二进制文件不是可解释的 shell(它是纯文本)。这就是为什么你不能
在这种情况下使用。由于程序是 ruby、awk、sed 或任何其他不是 shell 脚本的程序,它也会失败。
When you type
The shell tries to execute the program according to how it determines the file needs to be executed. If it is a binary, it will attempt to execute the entry subroutine. If the shell detects it is a script, e.g through the use of
or
or more generally
the shell will pass the file (and any supplied arguments) as arguments to the supplied interpreter, which will then execute the script. If the interpreter given in the path does not exist, the shell will error, and if no interpreter line is found, the shell will assume the supplied script is to executed by itself.
A command
is equivalent to
when the first line of program contains
assuming that /bin/sh is the sh in your path (it could be /system/bin/sh, for example). Passing a binary to sh will cause sh to treat it as a shell script, which it is not, and binary is not interpretable shell (which is plain text). That is why you cannot use
in this context. It will also fail due to program being ruby, awk, sed, or anything else that is not a shell script.
您不需要
sh
并且看起来您的$PATH
中没有该程序的路径。试试这个:
You don't need the
sh
and looks like you don't have the path to the program in your$PATH
.Try this:
这里不需要“sh”。只需将“程序”单独放在最后一行即可。
You don't need the "sh" here. Just put "program" on the last line by itself.
这应该足够了:
如果这不起作用,请检查以下内容:
This should be enough:
If that does not work, check the following: