我正在嘗試在運算式的結果和數字之間進行比較,但它正在嘗試執行某些操作并且它給出了錯誤。如果第一個包的流行度小于流行度變數,我想打破 while 回圈。
這類似于我從中得到的 json get_packages:
curl -sX "GET" -H "accept: application/json" "https://pkgstats.archlinux.de/api/packages?limit=2&offset=0" | jq "."
{
"total": 65871,
"count": 2,
"limit": 2,
"offset": 0,
"query": "",
"packagePopularities": [
{
"name": "attr",
"samples": 18330,
"count": 18330,
"popularity": 100,
"startMonth": 202112,
"endMonth": 202112
},
{
"name": "acl",
"samples": 18330,
"count": 18330,
"popularity": 100,
"startMonth": 202112,
"endMonth": 202112
}
]
}
offset=0
limit=1000
total=65871
res=()
pkgs=$(get_packages $popularity $limit $offset)
# Get 'limit' number of packages each time
while [ $offset -le $total ]; do
pkgs=$(get_packages $popularity $limit $offset)
res =$pkgs
if [ $($pkgs | jq ".packagePopularity[0].popularity") -lt $popularity ]; then
break
fi
offset=$(($offset $limit))
done
我得到的錯誤是:
./get_popular_packages.sh: line 63: "gnutls": command not found
./get_popular_packages.sh: line 63: [: -lt: unary operator expected
這是哪條線 if [ $($pkgs | jq ".packagePopularity[0].popularity") -lt $popularity ]; then
uj5u.com熱心網友回復:
錯誤似乎與這一行有關:
if [ $($pkgs | jq ".packagePopularity[0].popularity") -lt $popularity ]; then
將$pkgs | jq ".packagePopularity[0].popularity"你的命令替換中說:
- expand
$pkgs(變數擴展,并且由于變數沒有被參考,結果將受到幾個額外的擴展,包括分詞);然后 - 將結果作為命令運行并將輸出通過管道傳輸到指定的
jq命令。
但看起來您實際上想要將$pkgs自身的值發送到jq,而不是作為命令執行的結果。
使用 Bash,修改代碼的最簡單方法是將上面的行替換為
if [ $(jq ".packagePopularity[0].popularity" <<<"$pkgs") -lt $popularity ]; then
在<<<引入了從“這里字串”,這是一個特定的bash特征重定向。改用標準的heredoc會更便攜(但更難閱讀):
if [ $(jq ".packagePopularity[0].popularity" <<EOF
$pkgs
EOF
) -lt $popularity ]; then
另請注意,您在(缺乏)參考方面存在許多潛在問題。可能還有其他問題。 Shellcheck在識別所有這些方面會做得比我準備嘗試手動做的更好。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/407705.html
標籤:
