12

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!

4

5 回答 5

14

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

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

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

于 2010-10-17T15:40:12.220 回答
3

当您键入

./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 脚本的程序,它也会失败。

于 2012-10-11T17:51:19.730 回答
2

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

尝试这个:

#!/bin/sh
cd  /home/user/path_to_the_program/
./program
于 2010-10-17T15:41:29.973 回答
0

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

于 2010-10-17T15:36:11.690 回答
0

这应该足够了:

/home/user/path_to_the_program/program

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

  • 可执行位
  • 程序的 shebang 行(如果是脚本)
于 2010-10-17T15:47:08.733 回答