Common mistakes when using Entity Framework

One trap is the tight coupling of your application to the Entity Framework, which violates the principle of abstracting away you underlying frameworks. I don’t think this principle always applies…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How to write a script that performs certain commands in Linux

It is no news that the majority of software engineers use GNU/Linux operating systems or any other Unix-based operating system. Two common reasons that you will find for this trend are:

For these basic reasons, every software engineer is required to master how to use the shell terminal.

Over the past two weeks in the ALX Software Engineering program, we have been learning a lot about the Shell terminal and shell commands. In this article, I want to share with you what I learned about writing scripts in Linux.

Shell Scripting basically involves you writing some lines of code that are used to automate certain processes in your Linux environment.

This comes in handy when there are tasks that you repeatedly do. Let’s imagine that our work as software engineers at a startup we just joined involved creating new directories with specific files inside them.

If we have to create a lot of such directories then this becomes a herculean task without any form of automation. You may be tempted to try doing it by creating the directories and files inside them one after the other.

But that is where a shell script comes in handy. Instead of creating the directories and files one after the other, you can rather convert the commands you use to create them into a shell script which will automatically create the required directories and files anytime the script is executed.

Let’s assume a task to create directories with the current date as the name of the directory on a daily basis.

The code to create a directory with the current date as the name of the directory will

This means that if you have to create this on a daily basis then you have to memorize this exact Linux command. Even though this seems easy to memorize, some daily task requirements can be so cumbersome that memorizing it wouldn’t even be an option you will want to consider.

In such a case, we have to be able to automate the process with a shell script. So, how do we convert the Linux command above into a shell script?

For a file in Linux to be considered as a script, you need to do these two things:

This is done by calling the ‘Shebang’ followed by the path for the specific shell that should be used for the execution.

This means that any Linux file that starts with the 2 characters (#!) will be treated as a script to be executed.

The typical first line of a shell script will be like this:

The line above just means that the Bourne Again Shell (Bash) should be used to execute the script that you have created.

After the declaration of the first line, you can then go ahead and add the various Linux commands that you want to run anytime the file is executed.

In our case, we only want to run a command to create a directory with today’s date as its name. For such a case the whole script will look like this:

This file can be saved with any name that you prefer. Let’s go ahead and name this file “Dgenerator.sh”

There are several ways to grant the execution permission on the file and I will cover those in detail in subsequent articles but for now, we will go with one of the easiest ways to do it.

In order to grant “Dgenerator.sh” the execution permission so that anyone in our organization can execute this file to create the new directory, you have to run the following command in the shell terminal.

Once, your script is ready, you can call it with the line of code below;

In conclusion, I have shared with you how you can automate any Linux command through the use of shell scripts and how to execute such scripts.

Watch out for subsequent articles where I take practical examples and try to create the scripts for them.

Add a comment

Related posts:

HOW TO BE YOUR SELF ALWAYS

Every living human be you male or female is entitled for one particular duty which is “Fighting for your own”but nowadays people find it very hard to depend on their self’s may be because of lack of…

A Guide To The Grape Hasandede

Welcome to this post about the grape variety known as Hasandede. One that comes from Turkey but unfortunately does not get a lot of love out there. This is going to be a short post compared to many…

Locked and Loaded

I wrote a few days ago about the travails of young Master Kyle Rittenhouse. At the time I published his fate was still in the hands of the jury, but now it’s a matter of public record, and the public…