Forum Programmation.shell probleme script bash avec curl et jq

Posté par  . Licence CC By‑SA.
Étiquettes :
2
14
juil.
2026

Bonjour.
J'ai un souci avec mon script pour récupérer des datas de mon onduleur photovoltaïque:

> status=`curl --location --request GET 'https://openapi-eu.solaxcloud.com/openapi/v2/device/realtime_data?snList=XBXXXXXX015008&deviceType=1&businessType=1' \
--header 'Authorization: bearer secrettoken' \
--header 'Content-Type: application/json' \
 | jq '.result[0].acCurrent1' |tr -d \"`
echo "acCurrent1= " "${status}"

Ça fonctionne.Le résultat est stocké dans acCurrent1.
Mais je voudrais récupérer plusieurs variables en même temps afin de ne pas multiplier les requêtes curl
J'ai passé pas mal de temps à chercher, sans succès.
Un peu d'aide?

Merci d'avance

  • # Filtrer à posteriori

    Posté par  . Évalué à 8 (+7/-0). Dernière modification le 14 juillet 2026 à 15:48.

    Il faut récupérer toutes les données lors du curl et filtrer avec jq ensuite :

    D'abord le curl :

    status=`curl --location --request GET 'https://openapi-eu.solaxcloud.com/openapi/v2/device/realtime_data?snList=XBXXXXXX015008&deviceType=1&businessType=1' \
        --header 'Authorization: bearer secrettoken' \
        --header 'Content-Type: application/json'`

    Puis :

    acCurrent1=`echo "$status" | jq '.result[0].acCurrent1' |tr -d \"`
    acCurrent2=`echo "$status" | jq '.result[0].acCurrent2' |tr -d \"`
    echo "acCurrent1= $acCurrent1"
    echo "acCurrent2= $acCurrent2"
    • [^] # Re: Filtrer à posteriori

      Posté par  . Évalué à 9 (+7/-0).

      On peut aussi utiliser l'option -r de jq (ça affiche la valeur sans décoration) pour éviter le tr.

      On peut aussi utiliser le read du shell (au moins dans bash) pour assigner plusieurs variables dans un tableau ou des variables individuelles.

      • [^] # Re: Filtrer à posteriori

        Posté par  (site web personnel) . Évalué à 7 (+5/-0).

        C'est défini dans POSIX donc également disponible en dehors de bash.

        Debian Consultant @ DEBAMAX

        • [^] # Re: Filtrer à posteriori

          Posté par  (site web personnel, Mastodon) . Évalué à 2 (+0/-0).

          Comme l’indique la page que tu pointes, POSIX défini les options -r et -d ; mais cg fait allusion à -a :

          $ help read
          read: read [-ers] [-u fd] [-t timeout] [-p prompt] [-a array] [-n nchars] [-d delim] [name ...]
              One line is read from the standard input, or from file descriptor FD if the
              -u option is supplied, and the first word is assigned to the first NAME,
              the second word to the second NAME, and so on, with leftover words assigned
              to the last NAME.  Only the characters found in $IFS are recognized as word
              delimiters.  If no NAMEs are supplied, the line read is stored in the REPLY
              variable.  If the -r option is given, this signifies `raw' input, and
              backslash escaping is disabled.  The -d option causes read to continue
              until the first character of DELIM is read, rather than newline.  If the -p
              option is supplied, the string PROMPT is output without a trailing newline
              before attempting to read.  If -a is supplied, the words read are assigned
              to sequential indices of ARRAY, starting at zero.  If -e is supplied and
              the shell is interactive, readline is used to obtain the line.  If -n is
              supplied with a non-zero NCHARS argument, read returns after NCHARS
              characters have been read.  The -s option causes input coming from a
              terminal to not be echoed.
          
              The -t option causes read to time out and return failure if a complete line
              of input is not read within TIMEOUT seconds.  If the TMOUT variable is set,
              its value is the default timeout.  The return code is zero, unless end-of-file
              is encountered, read times out, or an invalid file descriptor is supplied as
              the argument to -u.
          readonly: readonly [-af] [name[=value] ...] or readonly -p
              The given NAMEs are marked readonly and the values of these NAMEs may
              not be changed by subsequent assignment.  If the -f option is given,
              then functions corresponding to the NAMEs are so marked.  If no
              arguments are given, or if `-p' is given, a list of all readonly names
              is printed.  The `-a' option means to treat each NAME as
              an array variable.  An argument of `--' disables further option
              processing.
          
          

          “It is seldom that liberty of any kind is lost all at once.” ― David Hume

  • # ,

    Posté par  (courriel, site web personnel) . Évalué à 3 (+2/-1).

    https://jqlang.org/manual/#comma

    $ jq -r '.foo, .titi' <<EOF
    {
    "foo": "bar",
    "baz": "quux",
    "titi": "toto"
    }
    EOF
    bar
    toto
    
    

    pertinent adj. Approprié : qui se rapporte exactement à ce dont il est question.

    • [^] # Re: ,

      Posté par  . Évalué à 1 (+0/-0).

      Merci beaucoup à tous!
      Cà marche
      Désolé de ne pas avoir répondu plus tot, mais j'étais en congés.
      Bonnes vacances à vous aussi!!

Envoyer un commentaire

Suivre le flux des commentaires

Note : les commentaires appartiennent à celles et ceux qui les ont postés. Nous n’en sommes pas responsables.