我試圖通過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'?我沒有看到任何一個合法的目的。 –