I am writing a bash script where the curl command returns a JSON array of objects and then I need to filter the required values out of those objects. So I need to iterate through the objects to check then and parse them and finally get the result.
But inside my bash script if I do something like,
for i in 1 2 3 4
do
curl -XGET 'https://gitlab.com/user/api/v4/projects/1/pipelines/1/jobs' | python -c 'import sys, json; print(json.load(sys.stdin)[$i]["stage"])'
done
I get the following error:
File "<string>", line 1
import sys, json; print($i)
^
SyntaxError: invalid syntax
100 3016 100 3016 0 0 18748 0 --:--:-- --:--:-- --:--:-- 19210
(23) Failed writing body
Everything Ok if I remove the python part after the pipe, but why can't I use python in combination with curl ? what am I doing wrong here ?
In bash
(and other shells), variables are not expanded when put inside single quotes.
Better approach than my original (as @chepner pointed out), pass the shell variable as command line argument to python
command itself:
curl ... | python -c \
'import sys, json; print(json.load(sys.stdin)[sys.argv[1]]["stage"])' "$i"
Original:
You can get around by using double quotes like:
curl ... | python -c \
'import sys, json; print(json.load(sys.stdin)['"$i"']["stage"])'
i.e. terminating the single quote just before variable reference, and continuing afterwards.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments