我有一個 make 目標,它首先呼叫一個生成報告的 CAE 工具。完成后,目標呼叫一個 python 腳本,該腳本將 CAE 報告的內容(或更具體的報告的一些 grep 行)作為引數。
一個最小的例子是
target1:
date > ./bar.txt
echo $(shell cat ./bar.txt)
問題是,$(shell cat ./bar.txt)在呼叫第一個命令和更新 bar.txt 之前,make 會擴展。因此,對于這個最小示例,echo 會在更新之前列印 bar.txt 的內容(上一次目標運行的日期)。
(我知道我可以在沒有變數和 shell 函式呼叫的情況下以另一種方式撰寫這個示例,這只是為了顯示我呼叫從 shell 呼叫中獲取引數的工具的問題。所以實際上我想這樣做:
target1:
cae_tool_call
report_eval.py -text "$(shell cat $(generated_report) | grep 'foo')"
其中 cae_tool_call 生成 generate_report。如果沒有顯式呼叫 shell 函式,這個 -text "argument" 不會決議引數。)
我已經嘗試過使用實際的 shell 變數(而不是 make 變數)、雙重轉義、立即變數和延遲變數,但還沒有可行的解決方案。有任何想法嗎?
#######################################
編輯以顯示一些意外行為:
我有這個python腳本argument_example.py
import argparse
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-r", "--reporttext", help="text string", required=True)
args=parser.parse_args()
if args.reporttext:
print(args.reporttext)
main()
它只是列印使用引數 -r 給出的文本。
我有這兩個目標:
####################################
#this does not work
REPORTNAME := ./bar.txt
variable_report:
date > $REPORTNAME
python3 ./argument_example.py --reporttext "`(cat $REPORTNAME)`"
####################################
#this works
static_report:
date > ./bar.txt
python3 ./argument_example.py --reporttext "`(cat ./bar.txt)`"
呼叫 variable_report 時,python 腳本會列印過期的 bar.txt 內容。呼叫 static_report 時,python 腳本會列印更新的內容。
uj5u.com熱心網友回復:
make recipes 已經是 shell 腳本了。切勿shell在配方中使用 make 函式。在您的第一個簡單示例中,使用:
target1:
date > bar.txt
cat bar.txt
在您的其他示例中使用:
generated_report := name-of-generated-report
target1:
cae_tool_call
report_eval.py -text "`cat $(generated_report) | grep 'foo'`"
甚至更好:
generated_report := name-of-generated-report
target1:
cae_tool_call
report_eval.py -text "`grep 'foo' $(generated_report)`"
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/531348.html
標籤:壳变量生成文件
上一篇:使用BASH_REMATCH獲取每個字串的第一個字符
下一篇:如何使行程不在后臺運行?
