One of the most important tasks in dealing with data is string manipulation. We already saw how to use awk and grep to efficiently sift through text files using command line tools instead of developing ad-hoc code. To step it up a notch, we can also do some heavier preprocessing of the data, such as selecting only the subset of information that matches a particular pattern, to ensure data coming out of our pipeline is of good quality.
In this case, we use a Bash feature called parameter expansion. Let’s assume we have the text data in a variable
TEXT_LINE and an expression
pattern to match (in file-name matching format), this is a summary of the possible expansion:
- Delete shortest match of pattern from the beginning
- Delete longest match of pattern from the beginning
- Delete shortest match of pattern from the end
- Delete longest match of pattern from the end
- Get substrings based on position using numbers
- Replace particular strings or patterns
So for example, to extract only the file name without the extension:
or to extract user name from an email:
or extract the file name from an absolute path:
NOTE: You can’t combine two operations, instead you have to assign to an intermediate variable.
A lot of things can be done using just the command line.
In fact, Bash shell scripting language is a touring-complete language, so anything can be done!
One important feature is the ability to slice an array (i.e. select a contiguous subset of elements of a collection).
So let’s say for example we stored the list of installed packages into a variable
PACKAGE_LIST as a bash array:
and for some reason we want to select elements from 4 to 10:
Let me explain. Here, we are using Bash parameter expansion:
[@]following the array name returns the whole content of the array.
:X:Ypart is doing the slicing by taking a slice of length
Ystarting at position
X. Note that if
Xis negative, that is we start at
Xelements from the end, we must put a space between the colon and the number.
You can use the cURL library and the
curl command to design your own Request and explore the Response. There are many possible uses like e.g., API debug, web hacking, pen testing.
curl is a tool to transfer data from or to a server, using one of the supported protocols (e.g., FTP, GOPHER, HTTP, HTTPS, IMAP, LDAP, POP3, RTMP, SCP, SFTP, SMTP, TELNET). The command is designed to work without user interaction.
curl offers a busload of useful tricks like proxy support, user authentication, FTP upload, HTTP post, SSL connections, cookies, file transfer resume, Metalink, and more. As you will see below, the number of features will make your head spin!
curl is a truly powerful command, however it does at the cost of complexity. Here I will show some real-world use cases.
The URL syntax is protocol-dependent. If you specify URL without
curl will attempt to guess what protocol you might want. It will then default to HTTP but try other protocols based on often-used host name prefixes. For example, for host names starting with “ftp.” curl will assume you want to speak FTP.
You can specify multiple URLs or parts of URLs by writing part sets within braces as in:
or you can get sequences of alphanumeric series by using
[ ] as in:
Nested sequences are not supported, but you can use several ones next to each other:
You can specify any amount of URLs on the command line. They will be fetched in a sequential manner in the specified order.
You can specify a step counter for the ranges to get every Nth number or letter:
In order to analyze in depth what we send and receive we might save everything on a file, this is as easy as:
Save To Disk
If you want save the Response to disk you can use option
-o <file>. If you are using
 to fetch multiple documents, you can use ‘
#‘ followed by a number in the specifier. That variable will be replaced with the current string for the URL being fetched. Remember to protect the URL from shell by adding quotes if you receive the error message
internal error: invalid pattern type (0). Examples:
-O writes output to a local file named like the remote file we get (only the file part of the remote file is used, the path is cut off). The remote file name to use for saving is extracted from the given URL, nothing else. Consequentially, the file will be saved in the current working directory. If you want the file saved in a different directory, make sure you change current working directory before you invoke curl:
Only the file part of the remote file is used, the path is cut off, thus the file will be saved as
Set HTTP Request Method
curl default HTTP method, GET, can be set to any method you would like using the
-X <command> option. The usual suspects POST, PUT, DELETE, and even custom methods, can be specified:
Normally you don’t need this option. All sorts of GET, HEAD, POST and PUT requests are rather invoked by using dedicated command line options.
Forms are the general way a web site can present a HTML page with fields for
the user to enter data in, and then press some kind of ‘submit’
button to get that data sent to the server. The server then typically uses
the posted data to decide how to act. Like using the entered words to search
in a database, or to add the info in a bug track system, display the entered
address on a map or using the info as a login-prompt verifying that the user
is allowed to see what it is about to see.
-d option we can specify URL encoded field names and values:
A very common way for HTML based application to pass state information between pages is to add hidden fields to the forms. Hidden fields are already filled in, they aren’t displayed to the user and they get passed along just as all the other fields. To
curl there is no difference at all, you just need to add it on the command line.
Set Request Headers
Request headers allow clients to provide servers with meta information about things such as authorization, capabilities, and body content-type. OAuth2 uses an
Authorization header to pass access tokens, for example. Custom headers are set in curl using the
Note that if you should add a custom header that has the same name as one of the internal ones curl would use, your externally set header will be used instead of the internal one. You should not replace internally set headers without knowing perfectly well what you’re doing. Remove an internal header by giving a replacement without content on the right side of the colon, as in:
If you send the custom header with no-value then its header must be terminated with a semicolon, such as
-H "X-Custom-Header;" to send
curl will make sure that each header you add/replace is sent with the proper end-of-line marker, you should thus not add that as a part of the header content: do not add newlines or carriage returns, they will only mess things up for you.
A HTTP request may include a
referer field (yes it is misspelled), which can be used to tell from which URL the client got to this particular resource. Some programs/scripts check the referer field of requests to verify that this wasn’t arriving from an external site or an unknown page. While this is a stupid way to check something so easily forged, many scripts still do it.
This can also be set with the
-H, --header flag of course. When used with
-L, --location you can append
";auto" to the
--referer URL to make curl automatically set the previous URL when it follows a
Location: header. The
";auto" string can be used alone, even if you don’t set an initial
To specify the User-Agent string to send to the HTTP server you can use
--user-agent flag. To encode blanks in the string, surround the string with single quote marks. This can also be set with the
-H, --header option of course. Many applications use this information to decide how to display pages. At times, you will see that getting a page with curl will not return the same page that you see when getting the page with your browser. Then you know it is time to set the User Agent field to fool the server into thinking you’re one of those browsers:
The way the web browsers do “client side state control” is by using cookies. Cookies are just names with associated contents. The cookies are sent to the client by the server. The server tells the client for what path and host name it wants the cookie sent back, and it also sends an expiration date and a few more properties.
When a client communicates with a server with a name and path as previously specified in a received cookie, the client sends back the cookies and their contents to the server, unless of course they are expired.
Many applications and servers use this method to connect a series of requests into a single logical session. To be able to use curl in such occasions, we must be able to record and send back cookies the way the web application expects them. The same way browsers deal with them.
It is supposedly the data previously received from the server in a
"Set-Cookie:" line. The data should be in the format
= symbol is used in the line, it is treated as a filename to use to read previously stored cookie lines from, which should be used in this session if they match. Using this method also activates the “cookie parser” which will make curl record incoming cookies too, which may be handy if you’re using this in combination with the
-L, --location option. The file format of the file to read cookies from should be plain HTTP headers or the Netscape/Mozilla cookie file format. NOTE that the file specified with
-b, --cookie is only used as input. No cookies will be stored in the file. To store cookies, use the
-c, --cookie-jar option or you could even save the HTTP headers to a file using
Work In Progress…
Ok there are many more options, but I will stop here for now. I will add something in the future, so if you have any request (like using more real urls) just leave a comment.
Second, reference system: Ubuntu Linux LTS 12.04.x
32bit 64bit (see this comment).
Third, [NEW] installation:
Done. It’s that simple now 😎
If it fails to compile, see THIS comment.
If you still do not succeed see THIS comment.
With recent version of Ubuntu (14.04 and 16.04) you might need to downgrade Bison, see THIS comment.
- install needed packages:
- create a directory, then download software to compile:
- compile mysql-proxy:
- build CryptDB on MySQL:
- Build CryptDB:
- now, it’s time to read
cryptdb/doc/README, enjoy! 😉
NOTE1: you should create a user
mysql to run DBMS for security reasons:
sudo groupadd mysql
sudo useradd -r -g mysql mysql
NOTE2: be very careful on each step and you wont fail.
Ubuntu guest instances in VMware sometimes come up with the boot error message:
This error is being caused because VMware doesn’t actually provide that level interface for CPU access, but Ubuntu try to load the kernel module anyway.
How to fix it:
- add the line:
NOTE: for older versions use
blacklist i2c_piix4 instead.
NOTE: it works both in VMWare Fusion 5 and 6, and Ubuntu LTS 12.04 and 14.04