Possible to urlencode a variable in a shell script

2019-02-25 08:07发布

问题:

Is it possible to url encode a variable within a shell script?

#!/bin/bash 

now=$(date +"%T") 

DATA=$(wget -q -O -   "http://someurl.com/x.htm?callback=webRequest&exthrs=1&extMode=&fund=1&entitlement=0&skipcache=&extendedMask=1&partnerId=2&output=json&noform=1")

wget -q -O - "http://somewhere.com?abc=$1&responseData=$DATA"

echo "-----COMPLETE----- $now   $1 $RANDOM  
"

I want to url encode the DATA variable, since its results have & in it, it messes up the params in the second wget, is there a way to url encode that DATA variable without using PHP to url encode?

回答1:

Here is one method for URL encoding the shell string DATA:

DATA=$(python -c "import urllib, sys; print urllib.quote(sys.argv[1])"  "$DATA")

Here is another:

DATA=$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$DATA")


标签: shell sh wget