13

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!

codaddict
  • 445,704
  • 82
  • 492
  • 529
co-worker
  • 213
  • 2
  • 3
  • 5

5 Answers5

14

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.

Nick
  • 11,475
  • 1
  • 36
  • 47
  • this worked but I'm unable to understand why it works this way but if I run it as `. /home/user/path_to_program/program ` it gives an error – Vibhanshu Biswas Aug 25 '20 at 06:50
  • @VibhanshuBiswas probably parts of your script rely on the current working directory being the directory where the script is. – Nick Oct 15 '20 at 16:16
5

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.

user1207217
  • 547
  • 1
  • 4
  • 15
2

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
codaddict
  • 445,704
  • 82
  • 492
  • 529
0

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

Toucan
  • 19
  • 2
0

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)
Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
  • Giving the path might not work if the program depends on relative paths from its current directory. For example, the program might depend on input files which are in the same directory as the program. – Winston C. Yang Oct 17 '10 at 15:58
  • Regardless of how this is done, the full path has to come from /somewhere/ - if not on the path, then via the shebang line, if not there, then it must be explicitly set via some means. – Arafangion Oct 18 '10 at 04:30