Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I make the following bash code run

I aim to retrieve data from an API. The api returns,using curl, the nombre of sold graphic cards.
Here, the first code(#1) runs and active, but the second one and since I introduced a boocle for, it is not working.However, it seems to me that the both are similar. Please, is there any difference and why for the second one the program is not retrieving data, and if any advice to solve it. The Idea, I think that the output of the for loop must be declared to the call function so that the curl can do the job, I dont know.Thanks in advance. #1

#!/bin/bash
#Récupération des informations de vente des Carte graphiques

curl=$(which curl)

rtx3060=$(curl http://0.0.0.0:5000/rtx3060)
rtx3070=$(curl http://0.0.0.0:5000/rtx3070)
rtx3080=$(curl http://0.0.0.0:5000/rtx3080)
rtx3090=$(curl http://0.0.0.0:5000/rtx3090)
rx6700=$(curl http://0.0.0.0:5000/rx6700)

function graphic_card_count() {
 {
   echo "$(date)"
   echo "RTX3060: $rtx3060"
   echo "RTX3070: $rtx3070"
   echo "RTX3080: $rtx3080"
   echo "RTX3090: $rtx3090"
   echo "RX6700: $rx6700"
 }
}

graphic_card_count >> sales.txt

the output is similar to this:

Sat Nov 26 15:56:02 UTC 2022
RTX3060: 1
RTX3070: 10
RTX3080: 10
RTX3090: 18
RX6700: 13

Now the second code:
#2

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#!/bin/bash
#Retrieval of graphics card sales information

graphic_cards=("rtx3060" "rtx3070" "rtx3080" "rtx3090" "rx6700")
curl_var="curl http://0.0.0.0:5000/"

# a for loop that will iterarte through the 'graphic_cards'
# and add the name_card at the end of the curl_var.

function graphic_cards_names(){

  for name_card in "${graphic_cards[@]}"; do
     echo "$name_card = \$(${curl_var}${name_card})"
  done
}

function main() {
   echo "$(date)"
   echo "RTX3060: $rtx3060"
   echo "RTX3070: $rtx3070"
   echo "RTX3080: $rtx3080"
   echo "RTX3090: $rtx3090"
   echo "RX6700: $rx6700"
}
main >> sales.txt

by the way, I tried calling the graphic_card_function inside the the main, but the output is similar to an echo, the curl not doing the job

>Solution :

Do not create variables on the fly. Use arrays.

Do not store commands as variables. Use functions.

Just date. Do not use echo $(command). Just the command

Your code could look like this:

#!/bin/bash
#Retrieval of graphics card sales information

graphic_cards=("rtx3060" "rtx3070" "rtx3080" "rtx3090" "rx6700")

curl_it() {
   curl http://0.0.0.0:5000/"$1"
}

get_graphic_cards_names() {
    graphic_cards_names=()
    for name_card in "${graphic_cards[@]}"; do
       graphic_cards_names+=("$(curl_it "$name_card")")
    done
}

main() {
   date
   for idx in "${!graphic_cargs[@]}"; do
      echo "${graphic_cargs[idx]^^}: ${graphic_cards_names[idx]}"
   done
}

get_graphic_cards_names
main >> sales.txt
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading