Linux: Shell Scripting basics

Writing your first script and getting it to work

To successfully write a shell script, you have to do three things:

  1. Write a script
  2. Give the shell permission to execute it
  3. Put it somewhere the shell can find it

Writing a script

A shell script is a file that contains ASCII text. To create a shell script, you use a text editor. A text editor is a program, like a word processor, that reads and writes ASCII text files. There are many, many text editors available for your Linux system, both for the command line environment and the GUI environment. Here is a list of some common ones:

Name Description Interface
vi, vim The granddaddy of Unix text editors, vi, is infamous for its difficult, non-intuitive command structure. On the bright side, vi is powerful, lightweight, and fast. Learning vi is a Unix rite of passage, since it is universally available on Unix/Linux systems. On most Linux distributions, an enhanced version of the traditional vi editor called vim is used. command line
emacs The true giant in the world of text editors is emacs by Richard Stallman. emacs contains (or can be made to contain) every feature ever conceived for a text editor. It should be noted that vi and emacs fans fight bitter religious wars over which is better. command line
nano nano is a free clone of the text editor supplied with the pine email program. nano is very easy to use but is very short on features. I recommend nano for first-time users who need a command line editor. command line
gedit gedit is the editor supplied with the Gnome desktop environment. graphical
kwrite kwrite is the “advanced editor” supplied with KDE. It has syntax highlighting, a helpful feature for programmers and script writers. graphical

Now, fire up your text editor and type in your first script as follows:

#!/bin/bash
# My first script

echo "Hello World!"

If you have ever opened a book on programming, you would immediately recognize this as the traditional “Hello World” program. Save your file with some descriptive name. How about my_script?

The first line of the script is important. This is a special clue given to the shell indicating what program is used to interpret the script. In this case, it is /bin/bash. Other scripting languages such as perl, awk, tcl, Tk, and python can also use this mechanism.

The second line is a comment. Everything that appears after a “#” symbol is ignored by bash. As your scripts become bigger and more complicated, comments become vital. They are used by programmers to explain what is going on so that others can figure it out. The last line is the echo command. This command simply prints what it is given on the display.

Setting permissions

The next thing we have to do is give the shell permission to execute your script. This is done with the chmod command as follows:

[ub@linux ub]$ chmod 755 my_script

The “755” will give you read, write, and execute permission. Everybody else will get only read and execute permission. If you want your script to be private (i.e., only you can read and execute), use “700” instead.

Putting it in your path

At this point, your script will run. Try this:

[ub@linux ub]$ ./my_script

You should see “Hello World!” displayed. If you do not, see what directory you really saved your script in, go there and try again.

Before we go any further, I have to stop and talk a while about paths. When you type in the name of a command, the system does not search the entire computer to find where the program is located. That would take a long time. You have noticed that you don’t usually have to specify a complete path name to the program you want to run, the shell just seems to know.

Well, you are right. The shell does know. Here’s how: the shell maintains a list of directories where executable files (programs) are kept, and just searches the directories in that list. If it does not find the program after searching each directory in the list, it will issue the famous command not found error message.

This list of directories is called your path. You can view the list of directories with the following command:

[ub@linux ub]$ echo $PATH

This will return a colon separated list of directories that will be searched if a specific path name is not given when a command is attempted. In our first attempt to execute your new script, we specified a pathname (“./”) to the file.

You can add directories to your path with the following command, where directory is the name of the directory you want to add:

[ub@linux ub]$ export PATH=$PATH:directory

A better way would be to edit your .bash_profile file to include the above command. That way, it would be done automatically every time you log in.

Most modern Linux distributions encourage a practice in which each user has a specific directory for the programs he/she personally uses. This directory is called bin and is a subdirectory of your home directory. If you do not already have one, create it with the following command:

[ub@linux ub]$ mkdir bin

Move your script into your new bin directory and you’re all set. Now you just have to type:

[ub@linux ub]$ my_script

and your script will run.

Opencart 2.x.x Event System Tutorial

Overview

OpenCart 2.x introduces a number of new features for the module developers and one of them is the event system. This is going to be a quick tutorial teaching you how to make use of it in your modules.

The concept is simple, yet very effective. It allows you to write code which will get executed when something interesting happens in OpenCart. Like when an order is made, or a customer is created without the need of using vQmod or OCMOD. By using the events system, you can avoid collisions which happen when you are using a modification system like vQmod or OCMOD. Additionally since you write the event handlers in PHP file, you get to enjoy proper code coloring/completion/linting from your editor/IDE.

Basics

Use of events involves only two steps:

  1. Register an event handler
  2. Implement the event handler

The event handlers are simply methods defined in controller files. You can use one file for all your methods or you can create separate controller for your event handlers. To register your event handlers you need to use the extension/event model (OpenCart 2.0.1+) or the tool/event model for OpenCart 2.0.0.0. The extension/event model has 2 methods: addEvent($code, $trigger, $action) and deleteEvent($code). As you can guess the addEvent() method is used to register event handlers and the deleteEvent() is used to unregister event handlers. It is a good practice to register your event handlers in the install() method of your module’s controller in the admin/ directory. It is also advised to unregister your event handlers when your module is being uninstalled, which can be done in the uninstall() method of the same controller file.

The $code argument is used to group your event handlers. It is a good idea to set this to the name of your module, much like you have named your main controller file.

The $trigger argument is the event name which you would like to set your handler to. A list of all available triggers can be found here: https://github.com/opencart/opencart/wiki/Events-(script-notifications).

The $action argument is the route to your handler function. It is written in the form of a standard route to a controller. For example module/a2bizz/on_user_created.

Example

Steps of implementing an event handler with simple code examples are given below.

The example assumes that we are working with OpenCart 2.0.1+. Now let’s assume that we are creating a module called “a2bizz”. The admin controller file for the module will be admin/controller/module/a2bizz.php. The catalog controller file for the module will be catalog/controller/module/a2bizz.php.

The module will achieve 2 simple tasks – send e-mail messages to the administrator when a store is deleted and upon a customer registration. The triggers that we are going to use are pre.admin.store.delete and post.customer.add.

First we will register our event handlers in the install() method of our module:

public function install() {
    $this->load->model('extension/event');
    $this->model_extension_event->addEvent('a2bizz', 'pre.admin.store.delete', 'module/mymodule/on_store_delete');
    $this->model_extension_event->addEvent('a2bizz', 'post.customer.add', 'module/mymodule/on_customer_add');
}

Uninstall process like this:

public function uninstall() {
    $this->load->model('extension/event');
    $this->model_extension_event->deleteEvent('a2bizz');
}

Next, we need to implement the event handlers. The pre.admin.store.delete event is emitted in the admin area, so its handler must be implemented in the admin controller file for our module. We would like our event handler method to notify the administrator that a store Cheap Manchester United football shirts has been deleted. Events starting with pre are emitted before the action is executed, and events starting with post are executed after the action has been taken. We would also like to include the store domain in our message, which is why we have chosen the “pre” event instead of the “post”. If we register for the post.admin.store.delete event, we will not be able to read the store’s domain.

Our event handler should look something like this:

public function on_store_delete($store_id) {
    $this->load->model('setting/store');
    $store_info = $this->model_setting_store->getStore($store_id);
    $admin_mail = $this->config->get('config_email');
    mail($admin_mail, "A store has been deleted", "The store " . $store_info['url'] . " was deleted.");
}

All that is left now is to implement the handler method for the post.customer.add event. We will do this in the catalog controller of our module. The method will notify the administrator when a new customer is registered. The method should look similar to this:

public function on_customer_add($customer_id) {
    $this->load->model('account/customer');
    $customer_info = $this->model_account_customer->getCustomer($customer_id);
    $admin_mail = $this->config->get('config_email');
    mail($admin_mail, "New Customer", "A new customer has just registered with the following e-mail: " . $customer_info['email']);
}

Note: We are using the mail() function for sending e-mails for simplicity. In a real module you need to use OpenCart’s Mail class for sending e-mails.

That’s it! Our module is ready to rock. The final cheap football shirts versions of our files is as follows:

admin/controller/module/a2bizz.php

<?php class ControllerModuleA2bizz extends Controller { public function install() { $this->load->model('extension/event');
        $this->model_extension_event->addEvent('a2bizz', 'pre.admin.store.delete', 'module/a2bizz/on_store_delete');
        $this->model_extension_event->addEvent('a2bizz', 'post.customer.add', 'module/a2bizz/on_customer_add');
    }
    
    public function uninstall() {
        $this->load->model('extension/event');
        $this->model_extension_event->deleteEvent('a2bizz');
    }
    
    public function on_store_delete($store_id) {
        $this->load->model('setting/store');
        $store_info = $this->model_setting_store->getStore($store_id);
        $admin_mail = $this->config->get('config_email');
        mail($admin_mail, "A store has been deleted", "The store " . $store_info['url'] . " was deleted.");
    }
}

catalog/controller/module/a2bizz.php

<?php class ControllerModuleA2bizz extends Controller { public function on_customer_add($customer_id) { $this->load->model('account/customer');
        $customer_info = $this->model_account_customer->getCustomer($customer_id);
        $admin_mail = $this->config->get('config_email');
        mail($admin_mail, "New Customer", "A new customer has just registered with the following e-mail: " . $customer_info['email']);
    }
}

Apart from the standard use, the event system can be used to create cross-module integrations. Using the Event object ($this->event), you can trigger any event at any point. You can use it to trigger your own events as well. Imagine that you cheap football kits are developing a module for customer comments. You can trigger events on a comment post, for example. This will allow other module developers to create handler methods for your events and execute code without the need of vQmod or OCMOD. This will ensure better system stability and better customer experience. You can also use the system to register and trigger your own cheap football tops events at runtime.

cheap Barcelona football shirts  |
cheap Real Madrid football shirts  |
cheap Manchester United football shirts  |
cheap England football shirts  |
cheap AC Milan football shirts  |
cheap Inter Milan football shirts  |
cheap Liverpool footbal lshirts  |
cheap France football shirts  |
cheap Juventus football shirts  |
cheap Celtic football shirts  |
cheap Celtic football shirts  |
cheap Italy football shirts  |
cheap Inter Milan football shirts  |
cheap Barcelona football shirts  |
cheap Chelsea football kits  |
cheap Arsenal football kits  |
cheap Liverpool football shirts  |
cheap Real Madrid football shirts  |
cheap Portugal football shirts  |
cheap Belgium football shirts  |
cheap Barcelona football shirts  |
cheap France football shirts  |
cheap Germany football shirts  |
cheap North Ireland football shirts  |
cheap Juventus football shirts  |
cheap AC Milan football shirts  |
cheap Manchester United football shirts  |
cheap Italy football shirts  |
cheap Manchester United football shirts  |
cheap Real Madrid football shirts  |
cheap Real Madrid football shirts  |
cheap Chelsea football shirts  |
cheap Germany football shirts  |