views:

72

answers:

5

Hi all,

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!

A: 

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

Toucan
it tells me: "program: not found"
co-worker
Ah, program isn't in your $PATH. ./program will work.
Toucan
ok, is there a way to run it via script without putting it to PATH?
co-worker
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.)
Toucan
Please explain *why* the `sh` is not neccessary.
Arafangion
+5  A: 

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

#!/bin/sh
cd /home/user/path_to_the_program/
./program
Nick
+2  A: 

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
It would be better if the use of 'sh' was explained. Why doesn't it work with 'sh'?
Arafangion
A: 

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
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
In that case, you are right.
Alan Haggai Alavi
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
A: 

thx guys you helped me all alot!!!!

co-worker
Put text like this as a comment to your original post, not as an answer. Pick (put a check mark by) the answer that helped you most, and possibly upvote other helpful answers.
Winston C. Yang
-1; Reading this response, I see nothing that answers the question!
Arafangion