Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to urlencode a variable in a shell script?

Tags:

shell

sh

wget

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?

like image 915
Sven Kahn Avatar asked Apr 20 '15 18:04

Sven Kahn


People also ask

What is a variable in shell scripting?

Introduction on Variables in shell scripting Variables in Shell Scripting: The variables are of two types of user-defined variables and system-defined variables. A variable is nothing but a name representing a piece of memory in the system that stores a particular value, and that value no need to constant, which means it can change.

What are variables in the Bourne shell?

We will look at the different variables that we can use that are provided by the system for the Bourne Shell and how to create custom variables to use in a shell script. Variables in shell scripts are used to store values that you need to use frequently throughout a script.

What is a shell script and how to use it?

A shell script allows us to set and use our own variables within the script. Setting variables allows you to temporarily store data and use it throughout the script, making the shell script more like a real computer program.

Is it illegal to assign a variable in a shell script?

The variable assignment is considered illegal in the following cases: The correct way to assign a variable in shell scripting is without any whitespace on either side of the assignment operator (=) which is shown as follows: Apart from an underscore as discussed in the first rule, a variable name cannot have any special character.


1 Answers

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")
like image 71
John1024 Avatar answered Sep 20 '22 09:09

John1024