該賞金到期in 4天。這個問題的答案有資格獲得 50聲望獎勵。 TheMaster正在從信譽良好的來源尋找答案。
我有數百個 Google Apps Script 專案,并且有各種 Bash 腳本用于使用clasp工具(一個 Node.js 應用程式)管理這些專案。許多腳本需要clasp pull在對本地檔案執行某些操作之前先在??本地拉取專案,所以我有一個腳本,它回圈遍歷本地扣專案檔案夾并clasp pull在每個檔案夾上運行。回圈按順序遍歷目錄,所以如果拉一個專案需要 3-4 秒,那么每 100 個專案運行它最終需要 5-6 分鐘。
我的目標是能夠clasp pull并行運行這些命令,以便它們同時啟動,并且能夠知道哪些專案已成功拉取,哪些專案未能拉取。
給定這樣的目錄結構:
├── project-1
│ ├── .clasp.json
│ ├── .claspignore
│ ├── _main.js
│ └── appsscript.json
├── project-2
│ ├── .clasp.json
│ ├── .claspignore
│ ├── _main.js
│ └── appsscript.json
├── project-3
│ ├── .clasp.json
│ ├── .claspignore
│ ├── _main.js
│ └── appsscript.json
└── pull_all.sh
這個pull_all.shBash 腳本:
#!/bin/bash
# use Node 14.17.5 to prevent "Error: Looks like you are offline." errors
# (see https://github.com/google/clasp/issues/872)
[ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh"
nvm install 14.17.5
nvm use 14.17.5
find . -name '.clasp.json' |
while read file; do
(
cd "$(dirname "$file")"
project_dir_name="$(basename "$(pwd)")"
echo "Pulling project ($project_dir_name)"
clasp pull
) &
done
運行此腳本時,它會為每個目錄輸出“Pulling project”行,然后給出 shell 提示,暗示腳本已完成執行。但是然后沒有用戶做任何事情,3-4 秒后它顯示了所有clasp pull命令的輸出(顯然是并行運行,因為命令的某些輸出亂序/重疊),然后掛起,并且沒有給出新的 shell 提示。此時我必須按 ctrl c 來終止腳本。
完整的輸出最終看起來像這樣:
$ ./pull_all.sh
v14.17.5 is already installed.
Now using node v14.17.5 (npm v6.14.14)
Now using node v14.17.5 (npm v6.14.14)
Pulling project (project-3)
Pulling project (project-2)
Pulling project (project-1)
$
Cloned 2 files.
? Pulling files…└─ appsscript.json
└─ _main.js
Cloned 2 files.
└─ _main.js
└─ appsscript.json
Cloned 2 files.
└─ _main.js
要強制其中一個腳本失敗,我可以將scriptId任何.clasp.json檔案中的更改為無效的腳本 ID 。在這種情況下,我確實看到了預期的輸出:
Could not find script.
Did you provide the correct scriptId?
Are you logged in to the correct account with the script?
... but it's still mixed in with the rest of the output and it's not clear which project that came from.
How can I make it so that:
- The script does not cause a new shell prompt to appear during the execution of the script.
- The script outputs a line indicating the success or failure of each
clasp pulloperation, referenced by the directory name of the project (where the.clasp.jsonfile was found). - Bonus: suppress the output of
clasp pullso the script only shows the success or failure result of each project (referenced by the directory name).
Note: I've mentioned clasp pull as an example command, but a valid solution would allow me to run any clasp command as a background process in a bash while loop, including, but not limited to clasp push, clasp deploy, etc.
uj5u.com熱心網友回復:
- 該腳本不會在腳本執行期間導致出現新的 shell 提示。
出現新的 shell 提示是因為您正在while回圈中創建一個新的子 shell (有關子 shell 如何在 bash 中作業的進一步指導,請參考 tldp.org 中的此頁面:鏈接)。為防止發生這種情況,請直接呼叫命令而不將它們放在括號內。
- 該腳本輸出一行指示每個 clasp pull 操作的成功或失敗,由專案的目錄名稱(找到 .clasp.json 檔案的位置)參考。
您通常可以通過||在命令后添加 來捕獲命令是否失敗(例如grep "foobar" file.txt || echo "Error: 'foobar' not found in file.txt")。您還可以將命令放在if/ 中else并為每個命令回顯相應的狀態訊息。
- 獎勵:抑制 clasp pull 的輸出,因此腳本僅顯示每個專案的成功或失敗結果(由目錄名稱參考)。
注意:此回復使用上述第二個問題的解決方案。您可以創建 2 個陣列——1 個表示成功,1 個表示失敗,然后在 if/else 陳述句內,將當前迭代元素添加到正確的陣列中。
如果以上任何部分不清楚,請隨時要求澄清!
uj5u.com熱心網友回復:
您可以強制腳本在完成之前等待輸出:
{
while IFS= read -d $'\0' -ru $find file; do
(
cd "$(dirname "$file")"
project_dir_name="$(basename "$(pwd)")"
echo "Pulling project ($project_dir_name)"
if clasp pull </dev/null 2>&1 ;then
echo "ExeClasp $project_dir_name: Success"
else
echo "ExeClasp $project_dir_name: Failed"
fi
) &
done {find}< <(find . -name '.clasp.json' -print0)
wait
} |
grep '^ExeClasp '
在哪里:
- 無互動
- 所有輸出都將被丟棄(由
grep) - 只會顯示結果。
uj5u.com熱心網友回復:
如果我正確理解您的問題,您想知道哪些專案沒有被取消。正如您在檔案中看到的那樣,Clasp不提供該選項,但您可以有效地解決該問題。您只需要更新該 bash 腳本即可將專案迭代檔案與最終目錄樹進行比較。在您的原始檔案中找到但不在目錄樹中的每個專案都代表一個錯誤的 id。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/334411.html
標籤:bash google-apps-script parallel-processing 后台进程 扣
上一篇:在作業表中插入我自己的聯系方式-GoogleApps腳本
下一篇:將字典格式化為表格
