Bash - build URL query string by parsing script command arguments -
i don't use bash need work on bit of bash has make curl request web service query string tail built command arguments contained in script command arguments variable $@
. if command argument string -e -v -s somethingelse
, query string must produced e=something&v=blank&s=somethingelse
.
at moment test script looks this:
#!/bin/bash set -e echo "${@}" query_string="" arg in "${@}" ; if [[ "${arg}" =~ "-" ]] ; query_string+="${arg}=" else if [ -z "${arg}" ] ; query_string+="blank&" else query_string+="${arg}&" fi fi done echo "${query_string}" | tr -d '-'
and produces incorrect output
e=something&v=s=somethingelse&
i'm not sure i'm going wrong. suggestions appreciated.
how about:
#!/bin/bash set -e echo "${@}" option=true query_string="" arg in "${@}" ; if $option ; query_string+="${arg}=" option=false else if [[ "${arg}" =~ "-" ]] ; query_string+="blank&${arg}=" else query_string+="${arg}&" option=true fi fi done echo "${query_string::-1}" | tr -d '-'
Comments
Post a Comment