-
-
Save virtadpt/31f777db05748ba8e551dcb0a82f295f to your computer and use it in GitHub Desktop.
Returns json created from pipe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Example: | |
# > echo "key1 value1 key2 value2" | ./key_value_pipe_to_json.sh | |
# {'key1': value1, 'key2': value2} | |
arr=(); | |
while read x y; | |
do | |
arr=("${arr[@]}" $x $y) | |
done | |
vars=(${arr[@]}) | |
len=${#arr[@]} | |
printf "{" | |
for (( i=0; i<len; i+=2 )) | |
do | |
printf "\"${vars[i]}\": ${vars[i+1]}" | |
if [ $i -lt $((len-2)) ] ; then | |
printf ", " | |
fi | |
done | |
printf "}" | |
echo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment