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!

0

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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
@VibhanshuBiswas probably parts of your script rely on the current working directory being the directory where the script is.
6

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.

Comments

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

1 Comment

It would be better if the use of 'sh' was explained. Why doesn't it work with 'sh'?
0

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

4 Comments

it tells me: "program: not found"
ok, is there a way to run it via script without putting it to PATH?
Yes. Use "./program" once in the directory, or write out the full path, something like "/home/user/path_to_the_program/program". Make sure program is executable (use chmod for this.)
Please explain why the sh is not neccessary.
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)

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.