A shell script is nothing but a text file comprising a sequence of commands for a Unix-dependent Operating System (OS). It is termed as shell scripting since it includes a sequence of instructions in a single file, which otherwise would have to be typed one after another in a single script. Keep in mind that essentially whenever you are communicating with an operating system, you are communicating with its shell.
While using major operating systems like Ubuntu or any Linux distribution, we interact with its shell through a terminal. Now, shell scripts are mainly used to automate specific system actions, tracking system resources, backing up files, and handling user accounts. By compiling a series of commands in shell scripts, system administrators can significantly improve accuracy, save time, and streamline complex tasks.
In this blog, let us understand shell scripting in detail. However, before that, let us delve into a few fundamental concepts.
What Is Kernel?
A kernel can be defined as a single program that forms the core element of an operating system. It has comprehensive control over all aspects present in the operating system such as I/O management, file management, process management, device management, memory management, etc. A complete kernel system in Linux comprises the Kernel, other libraries and utilities, installation scripts, and management scripts.
What Is Shell?
A shell is a special type of user program that ensures an interface through which a user can leverage the operating system services. Shell receives human-understandable commands from the user and then transforms them into codes that the Kernel can interpret. It is a type of command language interface that directly executes instructions retrieved from files or input devices like a keyboard.
The shell starts whenever the user starts a terminal or logs in. A terminal is basically a text-based interface that allows users to enter their text-based instructions and see the output of the provided instructions. All the large and complex scripts for automation are implemented via this terminal.
Now, let’s get started with the Shell scripts-
What Is Shell Scripting?
Generally, users interactively use shells. It means that shells receive commands from the users and then execute them sequentially. However, sometimes, system administrators need to execute a bunch of instructions, which they have to manually type each time in the terminal. This is a repetitive action and takes time.
Instead, users can compile these instructions in a file and then give the file as input to avoid this repetitive task. These files are generally referred to as Shell Programs or simply Shell Scripts. They work similar to a batch file in MS-DOS. After entering all the instructions in the file, users can save the file with a .sh extension. For example, they can give the name of the file as myscript.sh.
How Shell Scripting Works in Linux?
The basic methodology of creating and implementing shell scripts is very simple:
- Creating a Shell Script
- Ensuring that the script is accessible to the Shell
- Providing the necessary permissions to the shell to execute instructions.
Shell scripts can be created via a word processor, GUI, text editor, etc. The script can contain variables, ASCII codes, operators, loops, conditional statements, positional arguments, shortcuts, arrays, etc. You can understand the script as nothing but a series of instructions that can be understood by the shell. Though you can save the file as .txt, for shell scripting purposes, it is recommended to save it with a .sh extension. It is important to store the script file at a location from which the shell can access the file.
How to Create Shell Scripts in Linux?
The following steps explain the detailed steps through which you can effectively create and execute shell scripts in Linux-
- Start your Linux system and then open the terminal window.
- Now, go to the folder where you can start the terminal.
- Proceed to the folder where you want to save your Shell script. Now, let’s get started with the Shell Scripting process.
- Type the simple command- touch script.sh
- By default, the above command is not runnable, it requires necessary permission for execution.
- Type this command for permission: chmod +x script.sh
- The next step is to give commands to this shell script.
- Open this shell script with any preferred text editor (which can be GUI or CLI-based).
- For the sake of example, let us consider nano text editor (commonly found in Unix or Linux OS).
- Give this command: nano script.sh
- Now, test the shell script by providing the below-mentioned commands:
##echo Hello World! This is a shell script
##Touch testfile
##ls
##echo The shell script ends here - Save your changes and don’t forget to add the script.sh extension.
- Execute the shell script through this command in the terminal: ./script.sh
Who Uses Shell Scripts?
Shell scripting is a ubiquitous method for automation actively used by professionals from across the industries due to its power, flexibility, and extensive support of the operating system. The major users of shell scripts are listed as follows:
- System Administrators: They generally use shell scripts for automating routine administrative actions like system tracking, backups, and creation of user accounts and their management. This makes their tasks easier and ensures more accuracy, efficiency, and consistency.
- Developers: For developers, the use of these scripts comes while automating development processes, automating file manipulation, running test iterations, and deploying the software to servers.
- DevOps Professionals: In the evolving landscape of DevOps, shell scripting can be beneficial for processes like configuration management, automation, troubleshooting, and fast iteration. Such scripts are compatible across diverse operating platforms, which can be advantageous for those who work in different environments.
Real-life Examples of Using Shell Scripts
As per your professional roles, you might utilize shell scripts for a variety of purposes. Shell scripts can be valuable in the below-mentioned real-life scenarios:
- You have numerous databases on a single system, or your needs cannot be met by a single database: Shell scripts enable users to meet diverse needs that are quite difficult to fulfill by one database.
- You need to perform actions when the database is not currently running: In these scenarios, you can implement a shell script to initialize or stop a database as well as associated processes.
- You are required to track the status of the database to make sure that it is operating correctly and can process queries: A script can carry out this process, tracking not only the database but also the processes and resources of the system. It gives a complete view of the operations of the system.
- You must automate your database backups: For example, with the help of Oracle’s Recovery Manager, you can generate backup scripts that can operate on any platform. A shell script can thus work on this RMAN for different backup and recovery processes.
Conclusion
The blog covers a detailed overview of Shell scripting, explaining all the associated fundamental concepts, how shell scripts work, how to create shell scripts in Linux and real-life examples of using shell scripts. Creating shell scripts is a simple process if you have basic knowledge of programming like variables, ASCII, loops, conditional statements, etc. Go deep into shell scripts and practice more to be able to create more advanced shell scripts.