Create your own scripts using bash

Learning Linux

Regardless of the Linux distribution that we are using, ―no doubt that my favorite is Ubuntu― as soon as we get into the use of this system, certain automation needs. That is: create our own commands that perform certain commands in a personalized way. This need can be due to certain causes:

  • Simplify the syntax of the commands that we usually execute.
  • Take actions that cover any need that is not foreseen in the system operational.
  • Sequence orders that we assiduously repeat.

Although a bash script can be run in / from any directory, it is usually create a directory to host these scripts. In my case:

$ mkdir /home/pedro/.bin

I believe this directory (hidden by leading the period in front of the name) to hold all the scripts I use there. That the name of the directory is hidden has no other meaning than - unless explicitly specified otherwise - will not appear when viewing / home / pedro from the file viewer in graphical mode.

Now you have to inform Linux that it should also look there (/home/pedro/.bin) the orders that are executed from the terminal.

$ PATH=$PATH;/home/pedro/.bin

In this way the system will look for our orders there until we close the session. To make this association permanent:

$ sudo nano /etc/environment

and we add

:/home/pedro/.bin

at the end of the PATH line, it is very important not to forget the colon before the address of the directory that we are including, since this is the addition mechanism.

Our first step-by-step script

We create our file, like this in my case:

$ touch ~/.bin/donde

And to edit it, you can use your preferred editor or follow this indication:

$ gedit ~/.bin/donde &

And we add the following content:

#!/usr/bin/env bash

if [ $# -lt 1 ];
then
    echo "Necesitas pasar un parámetro"
else
    whereis $1
fi

Script analysis

Our first call line «shebang»(#! / Usr / bin / env bash) ask Linux to report where the bash shell is located and that what follows is executed according to bash's requirements. This precaution it is convenient to make sure that our scripts work in any installation. Another possible shebang he laughed:

#!/bin/bash

The difference between them can be very freaky, and I will explain it. In this last I assume that in our system the bash shell is at the / bin / bash address. However, where I propose in the script I guess I don't know where it is the bash interpreter. I ask the system for him to provide that address.

Third line: As you can see, the second line is an if. For bash the characters «$#« contain the number of parameters that we are passing from the command line. Therefore, »if [$ # -lt 1];» literally means "if the number of parameters is less than 1".

Fourth line: Then (literally translating from English: then), here it is indicated that what comes next will be executed when the condition evaluation if be true: in other words, the number of parameters is less than 1, that is, zero.

Fifth line: If we execute our script without any parameters, we will show in the terminal «You need to pass a parameter».

Sixth line: Indicates that what follows will be executed when the condition we have declared is not true.

Seventh line: Se run the command «where's« accompanied by the content that we have passed as first parameter.

Eighth line: with «fi»Indicates that the block ends if.

Testing our Script

It is important add write permissions to the script:

$ chmod -x ~/.bin/donde

Without this, a "permission denied" error would appear.. After that, we can run our script.

$ donde php

It should show us the location of the php binaries, their source files, and man pages. Something like that:

php: /usr/bin/php7.0 /usr/bin/php /usr/lib/php /etc/php 
/usr/share/php7.0-readline /usr/share/php7.0-json /usr/share/php7.0-opcache 
/usr/share/php7.0-common /usr/share/php /usr/share/man/man1/php.1.gz

Recapping

  • We enable a ".bin" directory to house our scripts.
  • We provide information to Linux to include this directory in its command searches.
  • We create our script.
  • Difference between different shebang.
  • Use of number of parameters passed with $ #.
  • Use of first parameter with $1.

I hope and wish that this script is useful for you.


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Miguel said

    Very good and well explained, but what does a parameter refer to?

    1.    Pedro Ruiz Hidalgo placeholder image said

      Thanks Miguel!

      I understand by parameter all complementary information that is supplied to a program, function or system. As this can be cumbersome, let me answer you with a few examples.

      In the linux command to copy the file a.txt to the file b.txt, we would write the following:

      $cp a.txt b.txt

      The cp program here receives two parameters which are the names of two files, the first (must exist) a.txt and the second b.txt.

      Another example: If you send to print from the console with the command

      $ lp file.pdf

      In this case "file.pdf" is a parameter for the lp program.

      I hope I have satisfied your doubts.

      regards

  2.   Miguel said

    My comments do not come out, it is a lack of respect, I do not return to this forum.

    1.    Pedro Ruiz Hidalgo placeholder image said

      I do not know what has happened, in any case this one has been published.

      Greetings.