Mastering Tools in Linux Shell Scripting

0
2869
Linux Shell Scripting

Linux Shell ScriptingIn the previous article, we had an introduction to Shell Scripting. To write good shell scripts, a Linux admin should have in hand all the necessary keys and tools. These tools are:

– Linux commands.
– Text Editor.
– Understanding the Basic Linux concepts.

In the previous article, we have gone through a list of basic Linux commands that every Linux admin must know. Now, the remaining two keys needs to be caught, for you to master and control all the necessary keys in your hand. So, let’s go and catch them!!

Using Text Editor
To write a shell script, you should be able to use a text editor. The most common text editor in use in UNIX/Linux systems is the vi editor. Linux distributions usually come with the improved version vim. vi/vim is not easy to learn, and will require some effort from you to get used to. But, when you master this editor, you will see how powerful it is.

Starting vim Editor
To open the vim editor, type the command vim

The following screen should appear.
1
To start inserting text, press i or Insert.
2
Notice the word — INSERT -– that appeared in the lower left corner. This indicates that you are in the insert mode.

To save what you have wrote, press Esc to exit the insert mode. This takes you back to the command mode, where you can save the file (among other actions available).
To save the file, type :w hello.txt , and press Enter.
3
The file is saved with name hello.txt
4
To exit, type :q , and press Enter.

Editing an Existing File
To edit an existing file, type vim filename (where filename is the name of the file). If the file doesn’t exist, it will be created on saving.

Discarding Changes in a File
If you have made some changes in a file and you want to discard these changes,  press Esc to return to the command mode, then type :q! and press Enter.

* * * * * *

Basic Linux Concepts
The following sections will cover briefly the basic concepts of Linux systems. Such concepts constitute the necessary base to build upon.

File Types
Everything in Linux (and UNIX) is a file. So, it is very important to know the various file types, and how to deal with files.

Files can belong to one of the following types:

  • Ordinary (normal) files: these include text, data and executable files.
  • Directories: directories are files of special type, that acts like containers to store files and subdirectories.
  • Links: Links are shortcuts to other files or directories.
  • Special Files: sometimes called device files. As I told you (several lines earlier) that Everything in Linux is a file; hardware devices, physical volumes, volume groups, logical volumes, file systems, cd-rom devices, and printers all are represented by files of a special type (device files).

Input/Output Redirection
When you execute a command at the shell, three file descriptors are associated with this operation: stdin, stdout, and stderr. The default standard input stdin is the keyboard, while both standard output stdout and standard error stderr default to the screen.
This default behavior can be changed using I/O Redirection.
Learn the Basics of C Programming Language
Input Redirection
Instead of waiting the input from the normal standard input stdin, the command can accept the input from a file. The standard input is redirected using the ‘<‘ operator.
5
Output Redirection
Instead of being displayed on the screen, the output can be directed to a file, either in overwrite mode > or in append mode >>
6
Redirecting Standard Error
If an error occurred in the execution of a command, the error is printed on the screen. Instead, it can be redirected to file.
7
The Black Hole
The special file /dev/null acts like a black hole; i.e. anything directed to this file is totally discarded.
In the following example, the output fired an error telling the user that there is no file or directory named foo.txt. We can discard the error message completely by redirecting the standard error to /dev/null.
8
Combining the Output and Error
The stdout and stderr can be combined and directed together to one destination using the composite operand 2>&1
9
Command Exit Status
The shell maintains a variable $? which stores a code that represents the exit status of the last command executed. An exit status of 0 means successful execution, while a non-zero value means not. This is very useful when we come to test conditions and decision making.
10
Piping
Piping means to redirect the standard output of a command to become the input to another command.
The syntax:
command1 | command2
11
Command Substitution
There will be cases you will need to assign the output of a command to a variable, or to insert the output inside a string of text for printing or sending by email, or any other purpose. This is called command substitution. Command substitution is done using either $( ) or
12
Changing File permissions
File Permissions are not in the scope of our series, but in this section I will show quickly how to change (set) permissions for a file. For a file to be executable, it needs to be both readable and executable. The command chmod changes the permissions of files and directories.

Consider we have the following file:
13
We need to make it executable. We can use either of the following two methods:
14
Or
15
This makes the file executable.
16

* * * * * *

In this article, we have talked about various tools and basic concepts that Linux admin should be aware of. We have learned how to use the vim editor to create a new text file, edit an existing one, then save or discard changes. We have also learned I/O redirection, piping, command exit status, command substitution, and how to change file permissions.

Have already mastered your tool, you are now ready to write your first script.

LEAVE A REPLY

Please enter your comment!
Please enter your name here