Huckle
Huckle

Reputation: 1940

Execute relative path executable in bash script

I googled for this senario, but it is flooded with results on how to turn a relative path into an absolute one (which would work, but I feel there is an easier way)

I have the following scenario:

$ls ../
executable1 dir1
$
$ls
shellscript1
$
$cat ./shellscript1
#!/bin/bash
#Run executable1, which I know is one dir up towards root
../executable1 arg1 arg2 arg3 etc
exit 0
#----End Of Script----
$
$./shellscript1
./shellscript1: line 3: ../executable1: No such file or directory

Essentially I need to call an executable with a relative path from a bash script. It works fine in a bash shell, but in a script it fails to resolve the path. I have verified the working directory is what I expect it to be (i.g., dir1). Is there some call or exec like command I need in front of it? I tried sh ../executable1 but of course bash baffs at the executable.

Upvotes: 2

Views: 6198

Answers (2)

pizza
pizza

Reputation: 7640

I just tested this, it is based on your information and seems to work just fine.

$ ls ../
dir1  executable1  executable1.c
$
$ cat ../executable1.c
#include <stdio.h>
int main(int argc, char ** argv) {
        while (*argv) {printf ("<%s> ",*argv++);}
        printf("\n");
        return 0;
}
$ cat ./shellscript1
$
#!/bin/bash
#Run executable1, which I know is up dir up towards root
../executable1 arg1 arg2 arg3 etc
exit 0
#----End Of Script----
$
$ ./shellscript1
<../executable1> <arg1> <arg2> <arg3> <etc>
$
$ ls -l ../ ./
./:
total 4
-rwxr-xr-x 1 pizza pizza 133 2012-03-29 00:19 shellscript1

../:
total 16
drwxr-xr-x 2 pizza pizza 4096 2012-03-29 00:24 dir1
-rwxr-xr-x 1 pizza pizza 6501 2012-03-29 00:20 executable1
-rw-r--r-- 1 pizza pizza  126 2012-03-29 00:20 executable1.c

Upvotes: 4

Kashyap
Kashyap

Reputation: 17476

Could be due to permissions on .. or ../executable1

user executing ./shellscript should have execute permissions on .. AND ../executable1. Does he?

Upvotes: 1

Related Questions