2

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.

6
  • 1
    More information is needed. When you test this, where are you? Enter pwd. The one piece of information I don't see after the cat is your running executable1. Try that. It should not work. Commented Mar 28, 2012 at 19:36
  • There are some problems with your shell command history: ls returned shellscript1, then you cat shellscript, that should not be there, since ls didn't show you... and you got the script. You then called ./shellscript (that it's not there). It printed ./shellscript1: line 3 ... something it's wrong with your query. It's sure 100% that you have a syntax error on file names. Double check if anything it's there, don't assume anything it's there. Commented Mar 28, 2012 at 21:46
  • Make sure none of those are symlinks. Commented Mar 28, 2012 at 22:39
  • @dAm2K I generalized the problem to make it more obvious, I certainly don't name my scripts, dirs, etc numerically. Commented Mar 29, 2012 at 3:45
  • @pizza the executable is one that I compiled myself so it is a regular file with rwx permissions. I also wrote the shell script, so that isn't a symlink. Commented Mar 29, 2012 at 3:47

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

I have no idea what exactly I changed but it is working now. I applaud your effort, going so far as to code a complete mockup. I'd give you another upvote if I could. (Maybe I'll find another of your answers)
1

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

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

3 Comments

Else it would produce an permission error, wouldn't it: ./l2: line 1: ../l1: Permission denied But it's a no such file or directory.
Both exist under my home directory so I have rwx on whatever I need.
It can be tricky. If you don't have exec permission on dir then you can't "see" anything inside so you get "no such file or dir".. also (not in this case but) if you have no exec permission on file and you try to execute the file from it's directory using "file" instead of "./file" then you get "no such command".. guess the msg might depend on the flavor of OS..

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.