我试图通过exec将参数传递给shell脚本,在另一个shell脚本中。但是,我得到一个错误,该脚本不存在于路径中 - 但事实并非如此。将参数传递给由exec调用的脚本产生不良结果
$ ./run_script.sh
$ blob has just been executed.
$ ./run_script.sh: line 8: /home/s37syed/blob.sh test: No such file or directory
出于某种原因,它是将整个执行作为脚本一个整体绝对路径 - 它不读字符串作为blob.sh参数。
这是正在执行的脚本。
#!/bin/bash
#run_script.sh
blobPID="$(pgrep "blob.sh")"
if [[ -z "$blobPID" ]]
then
echo "blob has just been executed."
#execs as absolute path - carg not read at all
(exec "/home/s37syed/blob.sh test")
#this works fine, as exepcted
#(exec "/home/s37syed/blob.sh")
else
echo "blob is currently running with pid $blobPID"
ps $blobPID
fi
和脚本通过run_script.sh被调用,而不是做多,只是模拟一个漫长的过程/任务:
#!/bin/bash
#blob.sh
i=0
carg="$1"
if [[ -z "$carg" ]]
then
echo "nothing entered"
else
echo "command line arg entered: $carg"
fi
while [ $i -lt 100000 ];
do
echo "blob is currently running" >> test.txt
let i=i+1
done
下面是我使用的Bash版本:
$ bash --version
GNU bash, version 4.2.37(1)-release (x86_64-pc-linux-gnu)
任何意见/意见/帮助为什么发生这种情况将不胜感激!
由于提前,
s37syed
它看起来像你是'exec'与'eval'混淆。 – chepner
*为什么*你在这里使用'exec'或'eval'?我没有看到任何一个合法的目的。 –