SkillRary

Please login to post comment

Introduction to Linux Shell

  • Amruta Bhaskar
  • Dec 14, 2020
  • 0 comment(s)
  • 3821 Views

A shell is a special user program which provides an interface to a user to use operating system services. Shell accepts human-readable commands from the user and converts them into something which kernel can understand. It is a command language interpreter that execute commands read from input devices such as keyboards or files. The shell gets started when the user logs in or start the terminal.

Shell is a UNIX term for an interface between a user and an operating system service. Shell provides users with an interface and accepts human-readable commands into the system and executes those commands which can run automatically and give the program’s output in a shell script.

An Operating is made of many components, but its two prime components are

  • Kernel
  • Shell

A Kernel is at the nucleus of a computer. It makes communication between the hardware and software possible. While the Kernel is the innermost part of an operating system, a shell is the outermost one.

 

A shell in a Linux operating system takes input from you in the form of commands, processes it, and then gives an output. It is the interface through which a user works on the programs, commands, and scripts. A shell is accessed by a terminal which runs it.

Shell is broadly classified into two categories –

  • Command-Line Shell
  • Graphical shell

Shell can be accessed by user using a command-line interface. A special program called Terminal in Linux/macOS or Command Prompt in Windows OS is provided to type in the human-readable commands such as “cat”, “ls” etc. and then it is being executed. The result is then displayed on the terminal to the user.

In the above screenshot, “ls” command with “-l” option is executed.

It will list all the files in the current working directory in long listing format.

Working with command-line shell is a bit difficult for the beginners because it’s hard to memorize so many commands. It is very powerful, it allows user to store commands in a file and execute them together. This way any repetitive task can be easily automated. These files are usually called batch files in Windows and Shell Scripts in Linux/macOS systems.

Shell script programming has a bit of a bad press amongst some Unix systems administrators, and more recently with professional software developers, as the demands of Continuous Delivery and other cornerstones of the modern software industry increasingly depend upon features of shell scripting which "purer" languages cannot provide.

The shortcomings of shell programming normally fall into one of two categories:

1. Speed

The speed at which an interpreted program will run as compared to a C program, a Java program, or even an interpreted Perl program. And Go, Rust, Ruby, Node.js are all competing for attention too.

2. Quality

Since it is easy to write a simple batch-job type shell script, there are a lot of poor quality shell scripts around. This also makes it easy to suggest that all shell scripts are bad.

So it is very useful to be able to create good shell scripts. Scripts which can be used for deployments, for data transformation, for efficient file handling or effective manipulation of a low-level machine, network, or operating system details - which a Java, Python or Go program would be unable to access as easily or as efficiently. Tying together disparate parts of a build pipeline in a couple of line of shell script is quick, effective, and gets the job done, where other tools are not up to the task.

There are several factors which can go into good, clean, quick, shell scripts.

1. Formatting

The most important criteria for any code must be a clear, readable layout. This is true for all languages, but again where the major languages are supported by extensive IDEs, many shell scripts are written in far less forgiving editors, so the onus is on the scripter to ensure that code is well presented. Badly formatted code is so much harder to maintain than well-formatted code.

Something about shell scripts seems to make them particularly likely to be badly indented, and since the main control structures are if/then/else and loops, indentation is critical for understanding what a script does. Again, a "proper" software developer is likely to use a powerful but complicated IDE, whilst many shell scripts are written in vim.

2. Efficiency

It is easy to fall into a trap of using unnecessary commands. The shell is not a fast or efficient processor of its language, and there is no compiler to reinterpret your intentions into the most efficient implementation, so a few minor changes to a script can often make a huge difference (good or bad) to the efficiency of the code.

A clear layout makes the difference between a shell script appearing as "black magic" and one which is easily maintained and understood.

1. Feature Creep

First, a simple script will - more often than anticipated - grow into a large, complex one.

2. Maintainability

Secondly, if nobody else can understand how it works, you may be stuck with maintaining it yourself for the rest of your life!

So the humble shell script has many obstacles in its way, but it continues to be useful. And so we must learn to use it well and within its idioms. It won't handle multi-dimensional arrays gracefully, but it will handle I/O redirection so easily you won't even notice it. It will manage multitasking and job control in ways that more complex languages could never dream of. But its ability to deal with multi-byte integers is very poor. So we must learn the shell and embrace what it excels at, whilst always choosing the best tool for the job at hand, and there are many things which are best done with other tools.

Please login to post comment

( 0 ) comment(s)