运行带有其他选项或参数的可执行文件

时间:2019-04-02 16:09:16

标签: bash

我正在编写旨在执行Test.sh(Linux可执行文件)的bash脚本anotherscript

#!/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

请注意,anotherscript$myfile和选项-m mymode作为参数。

但是出现文件未找到错误(例如Test.sh: line 8: /my.path2/anotherscript: No such file or directory)。

我的问题是:

  1. 我有followed this question可以获取bash脚本来运行可执行文件。但恐怕我还是会收到上面的错误。
  2. 我是否在指定参数以执行文件?

2 个答案:

答案 0 :(得分:1)

我建议您使用

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

不只是

"$mp2 $myfile -m mymode"

答案 1 :(得分: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

确保anotherscript具有执行权(chmod +x anotherscript)。