可以在shell脚本中对变量进行urlencode吗? [英] Possible to urlencode a variable in a shell script?

查看:196
本文介绍了可以在shell脚本中对变量进行urlencode吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 shell 脚本中对变量进行 url 编码?

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  
"

我想对 DATA 变量进行 url 编码,因为它的结果有 &在其中,它弄乱了第二个 wget 中的参数,有没有办法在不使用 PHP 进行 url 编码的情况下对该 DATA 变量进行 url 编码?

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?

推荐答案

这里是一种对 shell 字符串进行 URL 编码的方法 DATA:

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

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

这是另一个:

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

这篇关于可以在shell脚本中对变量进行urlencode吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆