Home > Software engineering >  Bash script convert none integer variable to integer
Bash script convert none integer variable to integer

Time:09-30

I have a bash script where I make a request like

GET "/_cat/indices?h=docs.count&format=json" | jq `.[]."docs.count"` 

SAVEIFS=$IFS
IFS=$'\n'
response=($response)
IFS=$SAVEIFS

for (( i=0; i<${#response[@]}; i   ))
do

echo "${response[$i]}"

done

The result I get is like the following:

"188"
"363"
"346224"

I would like to summarize these ones into one big number but I only get some kind of fault since they are not numbers. I have tried several different solutions I have found when I google but I do not succed to convert to integers (numbers). My skills in bash are not that good. Does anyone have a solution for how I should do?

The IFS was something I found when I googled. Perhaps there is a better way to do it?

CodePudding user response:

You can tell jq to do the maths for you:

jq '[ .[].docs.count | tonumber ] | add'
  • Related