0

Inside a bash shell script, I am attempting to

  • Find an executable in an unknown position the current directory (guaranteed to exist)
  • Run that executable, passing in a set of files as arguments.

Given that:

  • The executable may be in a different sub-directory on a given system (No control over that)
  • The executable will not be in $PATH

Currently, I can find the executable using:

    #!/bin/bash
    EXECUTABLE=$(find . -name 'executable_name*' -type f)

(suffix wildcard for platform-related reasons)

Trying to then execute it fails, returning:

    No such file or directory ./path/to/executable_name

*Note: The executable does work if I supply the exact path returned by find explicitly

Trying the following two options all fail with the same error:

As far as I understand using -exec {} with find will pass the find results as args to the executable, which is the opposite of what I'm trying to achieve.

Setting execution permissions beforehand on the target file also yields the same results

Zander Fick
  • 106
  • 1
  • 9
  • 1
    What about `find . -name 'executable*' -type f -exec '{}' arg1 arg2 ... ';'`? The `{}` refers to the path of the executable found. – tectux Jun 18 '20 at 11:04
  • `executable=$(find find . -name 'executable_name*' -type f); command "$executable" arg1 arg2 ....` or you might want to put an `echo` in front. – Jetchisel Jun 18 '20 at 11:08
  • Thanks @tectux, this does let me work around this issue by not having to bind the output of `find` to a variable (Needs multiple uses now) Leaving this open in case someone can help with the original problem. – Zander Fick Jun 18 '20 at 12:21

2 Answers2

0

try

find . -name 'executable_name*' -type f | bash
Digvijay S
  • 2,665
  • 1
  • 9
  • 21
0

Possibly the executable was found in a path that contains spaces. In that case you have to quote your variable.

For example:

$ cat with\ spaces/test.sh 
#! /usr/bin/bash
echo test
$ foo=$(find with\ spaces/ -type f -name '*.sh')
$ echo $foo
with spaces/test.sh
$ $foo
bash: with: command not found...
$ "$foo"
test

Another possibility is that in your script you cd to another directory somewhere between executing find (and storing its output in the variable) and calling the executable.

tectux
  • 181
  • 5