Shell Scripting
Shreeja Raj
SDE-2 @Walmart | Former SDE Intern'23 @Amazon | Red Hat Certified Engineer | Leetcode 500+ | GEU'23
Shell Scripting is an open-source computer program designed to be run by the Unix/Linux shell. Shell Scripting is a program to write a series of commands for the shell to execute. It can combine lengthy and repetitive sequences of commands into a single and simple script that can be stored and executed anytime which, reduces programming efforts. As shell can also take commands as input from file we can write these commands in a file and can execute them in shell to avoid this repetitive work. These files are called Shell Scripts or Shell Programs. Shell scripts are similar to the batch file in MS-DOS. Each shell script is saved with .sh file extension eg. myscript.sh. Shell script have syntax just like any other programming language. If you have any prior experience with any programming language like Python, C/C++ etc. it would be very easy to get started with it.
A shell script comprises following elements –
- Shell Keywords – if, else, break etc.
- Shell commands – cd, ls, echo, pwd, touch etc.
- Functions
- Control flow – if..then..else, case and shell loops etc.
What is Shell?
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
What is Kernel?
The kernel is a computer program that is the core of a computer’s operating system, with complete control over everything in the system. It manages following resources of the Linux system –
- File management
- Process management
- I/O management
- Memory management
- Device management etc.
Shell is broadly classified into two categories –
- Command Line Shell
- Graphical shell
Command Line 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 execute.
Graphical Shells: Graphical shells provide means for manipulating programs based on graphical user interface (GUI), by allowing for operations such as opening, closing, moving and resizing windows, as well as switching focus between windows.
There are several shells are available for Linux systems like –
- BASH (Bourne Again SHell) – It is most widely used shell in Linux systems. It is used as default login shell in Linux systems and in macOS. It can also be installed on Windows OS.
- CSH (C SHell) – The C shell’s syntax and usage are very similar to the C programming language.
- KSH (Korn SHell) – The Korn Shell also was the base for the POSIX Shell standard specifications etc.
Why do we need shell scripts?
- There are many reasons to write shell scripts –
- To avoid repetitive work and automation
- System admins use shell scripting for routine backups
- System monitoring
- Adding new functionality to the shell etc.
Advantages of shell scripts
- The command and syntax are exactly the same as those directly entered in command line, so programmer do not need to switch to entirely different syntax
- Writing shell scripts are much quicker
- Quick start
- Interactive debugging etc
Some Basics of Shell Scripting
Shell Scripting She-bang: The sign #! is called she-bang and is written at top of the script. It passes instruction to program /bin/sh. To run your script in a certain shell (shell should be supported by your system), start your script with #! followed by the shell name.
Shell Scripting Comments: Any line starting with a hash (#) becomes comment. Comment means, that line will not take part in script execution. It will not show up in the output.
Shell Scripting Variables: Scripts can contain variables inside the script.
Shell Scripting if then else: The if then else condition loop states that if condition meets, output goes to if part otherwise it goes to else part. The word fi represents if loop termination .
Shell Scripting for loop: The for loop moves through a specified list of values until the list is exhausted. For example, if there are 10 variables in the list, then loop will execute ten times and value will be stored in varname.
Shell Scripting while loop: Linux scripting while loop is similar to C language while loop. There is a condition in while. And commands are executed till the condition is valid. Once condition becomes false, loop terminates.
Thank you for reading...?
Have a nice day!!!