![](/img/trans.png)
[英]How to make an alias myscript=“./myscript” from inside the bash script to use it outside
[英]How to execute (./myscript) inside awk or bash script?
我想在 awk 命令中運行一個腳本。 我有一個代碼。/myscript 計算兩個實體之間的可能性; 結果列在 listfile.txt 中。
./myscript 的輸入是一個包含隨機生成的兩列的文件。
此處的目的是了解哪個輸入文件(值)最適合計算。 如果條件 (0.01<$8<0.5) 未被驗證,代碼將繼續運行,直到給出最佳值(隨機輸入)
我確實嘗試過,但它不會繼續執行代碼
./rand_input_generator
./myscript
rms=` awk ' NR==1 {print $8}' listfile.txt`
echo $rms
awk 'BEGIN {
rrr=$rms;
min=0.01;
max=0.5;
while(rrr > max) {
while(rrr < min) {
system("./rand_input_generator");
system("./myscript.cmd");
}
}
} '
我似乎根本沒有 go 進入循環。 請問有什么建議嗎?
使用 awk 的 system() function:
這里有一個例子
awk '{printf("%s ",$1); system("myscript " $2)}' file
該示例來自此站點https://unix.stackexchange.com/questions/72935/using-bash-shell-function-inside-awk
使用awk的system
function。 返回值是退出代碼。
!($8 >= 0 && $8 <= 0.05) { system("./myscript") }
我想我看到了你的困境。 嘗試這個:
function getvalue()
{
local -a rms
./rand_input_generator
./myscript
# No need for invoking gawk -- use bash array
read -a rms < listfile.txt
# Output eighth column/word
echo ${rms[7]}
# Echo to stderr/terminal
echo ${rms[7]} 1>&2
}
rrr=$(getvalue)
min=0.01;
max=0.5;
# Let awk do the comparison, and print out "true" command or "false" command,
# evaluate the command, and loop based on return code
while $(awk -v rms="${rrr}" -v min="${min}" -v max="${max}" 'BEGIN {if (rms < min || rms > max) print "true"; else print "false"}'); do
# Refresh the value
rrr=$(getvalue)
done
實際上awk
確實是一種字符串處理語言,而不是數學語言,因此如果您有bc
,我建議對最后三行進行此更改:
# Call bc to evaluate expression, returning 1 or 0 based on result, and check
while [[ $(echo "(${rrr} < ${min}) || (${rrr} > ${max})" | bc) -eq 1 ]]; do
rrr=$(getvalue)
done
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.