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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3953886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:41:55  来源:igfitidea点击:

Linux: Run a binary in a script

linuxshellcdsh

提问by co-worker

i want to run a program via script. normally i type ./programin the shell and the program starts.

我想通过脚本运行一个程序。通常我./program在 shell 中输入并启动程序。

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!

我知道这是幼稚的问题,但非常感谢!

回答by Toucan

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

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

回答by Nick

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

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

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

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

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

回答by codaddict

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

您不需要sh并且看起来您的$PATH.

Try this:

尝试这个:

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

回答by Alan Haggai Alavi

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)
  • 可执行位
  • 程序的shebang行(如果是脚本)

回答by user1207217

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

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

#!/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.

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

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

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

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.

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