Linux: run the binary in a script

I want to run a program through a script. I usually 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 a children's question, but a lot!

+7
linux shell sh cd
source share
5 answers

If ./program running in a shell, why not use it in a script?

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

sh program runs sh to try to interpret program as a shell script. Most likely this is not a script, but some other executable file, so it fails.

+11
source share

When entering

 ./program 

The shell tries to execute the program according to how it determines that the file should be executed. If it is binary, it will try to execute the input routine. If the shell detects that it is a script, for example, using

 #!/bin/sh 

or

 #!/bin/awk 

or in general

 #!/path/to/interpreter 

the shell will pass the file (and any arguments provided) as arguments to the provided interpreter, which will then execute the script. If the interpreter specified in the path does not exist, the shell will be an error, and if the interpreter string is not found, the shell will assume that the supplied script should be executed by itself.

Team

 sh program 

equivalently

 ./program 

when the first line of the program contains

 #!/bin/sh 

assuming / bin / sh is sh in your path (for example, it could be / system / bin / sh). Passing binary code to sh will cause sh to treat it as a shell script, which it is not, and binary as an uninterpreted shell (which is plain text). That is why you cannot use

 sh program 

in this context. It will also fail because the program is ruby, awk, sed, or something else that is not a shell script.

+3
source share

You don't need sh and it looks like you have no path to the program in your $PATH .

Try the following:

 #!/bin/sh cd /home/user/path_to_the_program/ ./program 
+2
source share

That should be enough:

 /home/user/path_to_the_program/program 

If this does not work, check the following:

  • executable bit
  • program shebang line (if it is a script)
+1
source share

Here you do not need "sh". Just put the β€œprogram” on the last line.

0
source share

All Articles