[英]URL encoding a string in bash script
我正在編寫一個 bash 腳本,在其中嘗試提交一個 post 變量,但是 wget 將其視為多個 URL,我相信因為它不是 URLENCODED ......這是我的基本想法
MESSAGE='I am trying to post this information'
wget -O test.txt http://xxxxxxxxx.com/alert.php --post-data 'key=xxxx&message='$MESSAGE''
在CentOS上,不需要額外的軟件包:
python -c "import urllib;print urllib.quote(raw_input())" <<< "$message"
您希望$MESSAGE
用雙引號引起來,因此shell不會將其拆分為單獨的單詞:
ENCODEDMESSAGE=$(php -r "echo urlencode(\"$MESSAGE\");")
擴展Rockallite對Python 3和文件中多行輸入的非常有用的答案 (這次是在Ubuntu上,但這沒關系):
cat any.txt | python3 -c "import urllib.parse, sys; print(urllib.parse.quote(sys.stdin.read()))"
這將導致文件中的所有行連接到一個URL中,換行符替換為%0A
。
純重擊方式:
URL='rom%C3%A2ntico'
echo -e "${URL//%/\\x}"
回聲:
romântico
utf8十六進制中的'C3 A2'是'â'
utf8列表: http : //www.utf8-chartable.de/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.