Linux Gate #5: How to increase your work efficiency by the use of programs?

 

Hello there! This article is the sixth article of our Linux series called: the Linux Gates. 

In this series of articles, we want to provide you with exactly what you'll need for you to start your way through Linux commands.

Our aim in Outofbox-GATE is to adapt Linux commands for everyone to help you increase your work efficiency in your daily job. 

We think that Linux is an incredible tool not only for programmers but for everyone, and we excel at helping you see the interest of it in your work.

In the previous article: Linux Gate #4: Everything you need to know about Linux directories! we introduced you to directories and how to create them.

In the present article we are going to see the third part of any program that exists out there: the language component!

More precisely, we are going to see how you can write a simple program from script, how to run this program, and how to modify you program for your needs.

Here is our plan to achieve this goal:

What is a script?

A script is a file where you can write commands instead of writing them directly in the terminal.

It's basically a file like any other file, but now we introduce the word script for it. You probably wondering why suddenly we call it like that?

The only difference between script and a simple file is that a script is executable, meaning that it's a file that behaves exactly as a program and can be run just like a program.

Why do i need to script?

Scripts are executable files that do something. The power of scripts is that they can be executed whenever you like once written.

To get you a closer picture on how you can use scripts in your daily job, let's consider the following scenario.

Suppose that your daily job involves working with sheets of data and lots of calculations you do on these data. 

For every day, you basically do the same tasks but in different ways.

Well, with scripts, you can write all the commands that allows you to achieve all your tasks: storing data, managing database, and all calculations. You precisely write all the commands in one or multiple scripts. 

Now every day, you won't need to do the same work again and again but you only need to run these scripts whenever you need them.

In other words, once written, tested and validated; scripts works for you and do all the repeated tasks you don't find interesting.

The 21th century's technology, is only made possible with scripts. All day long, you encounter them in your phone, in your car, when you make a purchase for you groceries, etc.

All these automatic operations are not magical. They hide lots of scripts prewritten that can be launched systematically and repeatedly for infinite number of times.

Why talking about scripts here?

We wanted to talk to you about scripts as soon as possible in order to help you begin writing your own simple programs.

This will save you a lot of times in your work.

How to create a simple script?

Now let's show you how you can write a script. As stated above, creating a script is basically creating a file like we showed you in Linux Gate #3 : The thing at the heart of Linux.

We can do it like:

The second step is to write the header : #!/bin/bash at the beginning of this file 

We can do it like this:

The final step, which is the most interesting one, is where we give the created script "newscript" additional powers: the executable power.

This can be done using the command chmod +x and apply it the our script:

Now the script "newscript" has become a mini program that can be launched from the terminal like any other program.

Furthermore, if you list the content of the path where you created this script "newscript", you can notice that it's color has changed to green, which is the default program's color in Linux.

What does the heading of this script means?

The heading simply means that the content of your file is written in bash language. 

This thing here: #!/bin/bash is called a pre-processor that understands the commands in your script.

If you want to write in python language inside your script, now you need to change the header to the pre-processor: #!/usr/bin/env python. 

You notice that now, the phrase has become a little longer, but basically it means that your script is ready to understand python language instructions.

How to run a script?

To run a script you simply need to call it from the terminal, and it will be executed by itself. Simple as that.

Now, our script "newscript" contains the header only and no other commands yet. 

If we run the script, nothing should happens:

Now, let's write a simple command inside this "newscript" program. 

For example, we write the command "pwd" in this script (in a new different line), which will show us in return the current working path:

Now let's go one step further, and suppose that our job involves creating (for every day) three directories: dir1, dir2, dir3 that contains three clients files filled with their personal information:


Running this script each time  now will do the following operations:

1. Shows the current working directory path

2. Create three Directories: dir1, dir2, dir3

3. Create three client's files: client1, client2, client3

4. Move each Client's file to it's corresponding directory.

Finally you can notice that phrases that start with the sharp symbol "#", are not executed because they are commented. 

We highly recommend to write some comments before each commands in order to make your scripts readable and understandable.

Where to go next?

There you go! you've learned how to create a simple program to help you increase your efficiency for the repeated daily tasks

In the next post, we are going to get in depth on how making arguments in a script  that will help you save your time.

Stay tuned for that !

Meanwhile, don't hesitate to follow our Blog in order to get notified on future Posts and Podcasts.  

You can also follow our LinkedIn page where we propose useful tips and trick about Linux : 

OutOfBox-GATE Linux LinkedIn Page

And until the next Post, SHEERS !

Comments

Read Also

Linux Gate #2 : Where to begin ?!

Linux Gate #0 : Is Linux terminal that scary ?