Learn the UNIX/Linux command line

Parse JSON in BASH

Up to 5 or 6 years ago when they wanted to transmit information from one server to an application (whether an application like this or a dynamic web) was used XML, but it seems that this trend has changed completely betting most businesses and services JSON, a less restrictive alternative, light, dynamic and probably easier to read (for humans).

And is the simplicity that gives rise to some technologies based on JSON?

Revisiting JSON

Although I wanted to analyze the current situation of JSON, I can not help but devote a few lines to this notation, which has clear order to exchange data.

This object-based notation (which uses the native JavaScript object system) makes use of the keys and brackets to group data relating to lists or simple key and value, respectively lists.

For starters programming notation not familiar with them it will take a simple example to understand its operation.

JSON has emerged as standard?

The great opponent of JSON is XML, which can be defined as a much stricter language (due to the use of DTDs or document type definitions) and has some peculiarities such as XPath have to explore its structure. But has the disadvantage of being very rough, making it unwieldy or uncomfortable to write, not to mention the volume can have if we want to exchange large volumes of data.

In the case of passing fairly simple data, as would data to generate graphics, XML becomes a heavy burden, as the ideal way to pass this data is storing them in different tags ( 21 24 28 ), while in JSON we can store them all in a row as a simple array ([21,24,28]).

This advantage in terms of simplicity and great compatibility with multitudes of languages ​​has made it virtually the norm in web development, being somewhat trivial reception by AJAX JSON objects that are interpreted natively by any browser (with JSON)

Exchange storage

Although JSON nation to bring the native notation used to declare objects in JavaScript to other languages, has also gotten him to use as far from their source and store data in databases, MongoDB is the perfect example.

This manager NoSQL databases based on key-value bet on this notation to store data. MongoDB instead of having the typical tables, features collections, which are neither more nor less than JSON objects that can be defined at will and completely independent of how we do in any other collection (also speak more extensively about how to insert MongoDB data)

One of the advantages of Linux is the amount of options when customizing the system. One of the most interesting is the automation of tasks in Linux with Bash. Discover how to use from Linux Bash interpreter and how to create programs that help you automate repetitive tasks.

1: The first steps

To automate tasks in Linux is the best tool Bash. The first thing you have to do is create a 'script'. A 'script' is a plain text file containing a list of commands an interpreter, in this case Bash.

When you open the executable file, Linux automatically processes each of the commands within the 'script' saving you having to type these commands over and over again.

The best way to get to know and manage Bash is creating your first program and your first 'script'.

Run your first program

If you're on Ubuntu click on the logo and type in the search terminal box and click the icon.

To run the program writes the "echo" to display 'Hello World' command. This type of program is used as an example when learning a new language.

For this type on the terminal echo "Hello World". The command "echo" displays the written words in quotes.

Create your own 'script'

To run programs Bash you can do so directly from the console or terminal Linux, however, their greatest power is that you can write 'scripts' and run pieces of code to automate your tasks.

Create an executable file, or 'script' Linux commands. Open your text editor. In Ubuntu the default text editor is Gedit. Click the Ubuntu logo and type in the search box gedit or from the terminal of your console runs gedit &.

When you have opened your editor writes the following code:

#! / Bin / bash
echo "Hello World"

The program is similar to what has previously run with the slight difference that added the line #! / Bin / bash.

This line tells Linux that Bash is a program type and therefore should interpret the commands of the program. Written the code, save it by clicking on File, Save and name the holamundo.sh. Sh extension is renococida by many publishers and marked with color keywords.

Automate your program

Now you have saved the 'script' s time to run whenever you want to perform all tasks scheduled in that file. This allows you to automate your tasks avoiding you the tedious task of repeating again and again the series of commands you have determined.

The first step you have to do is change the permissions of your file and make it executable. For this type chmod u + x holamundo.sh command.

This command converts the file so that can perform the 'script' easily. To start the 'script' simply type the file name preceded by ./. For the above example would ./holamundo.sh.

As you can see, the same program that you started before playing. This will allow you to write to the 'holamundo.sh' file all the commands you need to run it by preventing one by one.

2: Bash Features

When you learned how to run and create 'scripts' you have to know the characteristics of Bash. This allows you to have control over the commands and to customize them as you wish to automate tasks in Linux.


Comments on Bash line by line are made with the # symbol at the beginning of it. This can be useful when your program is very large and want to put explanations of what each part.

An example would be the following code:

#! / Bin / bash
# Hello World commented
# Echo "Hello World"


In programming, variables are data structures that, as its name suggests, can change content throughout the execution of a program. Bash variables are untyped, ie, a variable can contain a string or number without which you define.

The syntax is:

variable_name = variable_value

It is mandatory that do not leave spaces before or after the symbol '=' because otherwise Bash interpret the variable as a Linux command.

To access simply type a variable $ prefix in the name of the variable. echo $ varname

Try this simple example:

#! / Bin / bash
# Assignment and output variables
myvar = "My name is Nacho"
echo $ myvar

As you can see the line echo $ myvar will execute everything contained quotes from the previous line.

Passing variables

When you run the 'script' from your terminal or console you can pass more arguments. For example:

hello ./miScript.sh 4

To collect these values, hello and 4, type $ and then the number of position last argument. The first argument is worth $ 1, it would be 'hello', and the second would be $ 2, an example would be the number '4'. The variable $ 0 is the name of the file.

For example:

#! / Bin / bash
# Step of variables
will echo "Your first argument is" $ 1
it echo "Your second argument is" $ 2

Note that $? stores the output value of the last command executed, $ * stores all arguments and $ # is the number of arguments passed.

3: Linux Commands

There is a lot of commands either to help, to manage files and directories for user management, processes, disk, system, network, printer, etc. This is a list of some of them.

Help Commands

The following commands provide you with information and help about the various commands.

man displays the command that you indicate manual. The command - help Displays a command help.

File and directory commands

There are other commands that allow you to move and manage files and directories on your Linux system. Here are some of them ls: list files and directories.
cd: change directory.
pwd displays the current directory path.
mkdir: create a directory.
rmdir: delete a directory.
rm -r: delete not empty directories.
cp: copy files.
rm: delete files.
mv: move or rename files and directories.