2

I'm writing a bash script Test.sh that aims to execute anotherscript (a linux executable file):

#!/bin/bash -l
mp1='/my/path1/'
mp2='/my/path2/anotherscript'

for myfile in $mp1*.txt; do
    echo "$myfile" 

    "$mp2 $myfile -m mymode"
    echo "finished file"
done

Notice that anotherscript takes as arguments $myfile and options -m mymode.

But I get the file not found error (says Test.sh: line 8: /my.path2/anotherscript: No such file or directory).

My questions are:

  1. I have followed this question to get my bash script to run the executable file. But I'm afraid I still get the error above.
  2. Am I specifying arguments as they should to execute the file?
Sos
  • 1,783
  • 2
  • 20
  • 46
  • 2
    Try `"$mp2" "$myfile" -m mymode`. Note that quoting values groups them together, e.g. `echo "hello"` != `"echo hello"`. – 0x5453 Apr 02 '19 at 16:13
  • 1
    Maybe `sh -c "$mp2 $myfile -m mymode"` works – Tom Atix Apr 02 '19 at 16:15
  • Where are the scripts located in your filesystem? Where from the file system are you running them? Did you remember to chmod +x both scripts? – Perplexabot Apr 02 '19 at 16:17
  • @Perplexabot I did `chmod +x anotherscript` before attempting to run it through the bash script. Both scripts are somewhere in the file system. I have not add their directory to the `$PATH`. – Sos Apr 02 '19 at 16:19
  • 1
    @TomAtix and @0x5453, both suggestions work, thanks a lot. Feel free to add it as answers and I'll gladly accept them. I'd like to know however why it isn't important to have `-m mymode` within quotations. Thanks once again! – Sos Apr 02 '19 at 16:22
  • Let us assume that both scripts are located in the same directory. That directory must be in $PATH if the directory isn't already in your $PATH. For example, as you may know, /usr/bin/, /bin/ are already in your $PATH. So shoving the scripts in those dirs should work. If you don't want to add that dir to your path, you can just cd into where they are and run the script. – Perplexabot Apr 02 '19 at 16:22
  • @Perplexabot thanks, I understand. However, the idea was that I could indicate the file path without adding it to $PATH. I know this wasn't clear in my original post, so if you'd like to contribute an answer it is very appreciated! – Sos Apr 02 '19 at 16:25
  • 1
    Glad to hear you got it fixed! – Perplexabot Apr 02 '19 at 16:26

2 Answers2

2

I suggest you use

sh -c "$mp2 $myfile -m mymode"

instead of just

"$mp2 $myfile -m mymode"
Tom Atix
  • 381
  • 1
  • 21
0
#!/bin/bash -l

dir=`find /my/path1/ -name "*.txt"`
mp2='/my/path2/anotherscript'

for myfile in "$dir"; do
    echo "$myfile" 

    "$mp2" "$myfile" -m mymode
    echo "finished file"
done

Make sure anotherscript has execution right (chmod +x anotherscript).