Linux:在脚本中运行二进制文件

发布于 2024-09-28 11:54:53 字数 226 浏览 2 评论 0原文

我想通过脚本运行一个程序。 通常我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

筑梦 2024-10-05 11:54:53

如果 ./program 在 shell 中工作,为什么不在脚本中使用它呢?

#!/bin/sh
cd /home/user/path_to_the_program/
./program

sh program 启动 sh 来尝试将 program 解释为 shell 脚本。它很可能不是脚本,而是其他一些可执行文件,这就是它失败的原因。

If ./program works in the shell, why not use it in your script?

#!/bin/sh
cd /home/user/path_to_the_program/
./program

sh program launches sh to try and interpret program as a shell script. Most likely it's not a script but some other executable file, which is why it fails.

尬尬 2024-10-05 11:54:53

当您键入时,

./program

shell 会尝试根据它确定需要执行的文件的方式来执行程序。如果它是二进制文件,它将尝试执行入口子例程。如果 shell 检测到它是一个脚本,例如通过使用

#!/bin/sh

#!/bin/awk

或更一般地,

#!/path/to/interpreter

shell 会将文件(以及任何提供的参数)作为参数传递给提供的解释器,然后解释器将执行该脚本。如果路径中给定的解释器不存在,shell 将出错,如果未找到解释器行,shell 将假定所提供的脚本将由其自身执行。

命令

sh program

相当于

./program

程序的第一行包含

#!/bin/sh

假设 /bin/sh 是路径中的 sh(例如,可以是 /system/bin/sh)。将二进制文件传递给 sh 会导致 sh 将其视为 shell 脚本,但事实并非如此,并且二进制文件不是可解释的 shell(它是纯文本)。这就是为什么你不能

sh program

在这种情况下使用。由于程序是 ruby​​、awk、sed 或任何其他不是 shell 脚本的程序,它也会失败。

When you type

./program

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

#!/bin/sh

or

#!/bin/awk

or more generally

#!/path/to/interpreter

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

sh program

is equivalent to

./program

when the first line of program contains

#!/bin/sh

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

sh program

in this context. It will also fail due to program being ruby, awk, sed, or anything else that is not a shell script.

久光 2024-10-05 11:54:53

您不需要 sh 并且看起来您的 $PATH 中没有该程序的路径。

试试这个:

#!/bin/sh
cd  /home/user/path_to_the_program/
./program

You don't need the sh and looks like you don't have the path to the program in your $PATH.

Try this:

#!/bin/sh
cd  /home/user/path_to_the_program/
./program
当爱已成负担 2024-10-05 11:54:53

这里不需要“sh”。只需将“程序”单独放在最后一行即可。

You don't need the "sh" here. Just put "program" on the last line by itself.

最近可好 2024-10-05 11:54:53

这应该足够了:

/home/user/path_to_the_program/program

如果这不起作用,请检查以下内容:

  • 可执行位shebang 行(如果它是脚本)
  • 程序的

This should be enough:

/home/user/path_to_the_program/program

If that does not work, check the following:

  • executable bit
  • shebang line of the program (if it is a script)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文