Shell Scripting

Shell Scripting

What is Shell Scripting?

Shell Scripting refers to the process of writing scripts using a command-line Shell such as Bash (Bourne Again Shell ), which is commonly used in Unix like Operating Systems like Linux. A Shell Script is a series of commands and instructions written in a scripting language that can be executed by the shell.

Types of Shell

Flavours of UNIX

1. Bash (Bourne Again SHell)

Bash is the default shell for most Linux distributions and is also available on macOS. It is a powerful and widely used shell that provides a rich set of features, including scripting capabilities.

2. C Shell (csh)

C Shell is a Unix shell created by Bill Joy, one of the co-founders of Sun Microsystems. It has a C-like syntax and includes features such as command-line editing and history.

3. Korn Shell (ksh)

The Korn Shell is an enhancement of the original Bourne Shell (sh) and incorporates features from the C Shell (csh) and the TC Shell (tcsh). It is available on various Unix-like systems and provides advanced scripting capabilities.

4.Zsh (Z Shell)

Zsh is an extended version of the Bourne Shell (sh) with additional features and improvements. It offers advanced command-line editing, completion, and customization options. Zsh is often used as an interactive shell and can be found on many Unix-like systems.

5.PowerShell

PowerShell is a shell developed by Microsoft and primarily used in Windows environments. It provides a command-line interface with a scripting language. PowerShell is designed to automate administrative tasks and offers extensive support for managing Windows systems.

6. Fish Shell (Friendly Interactive SHell)

Fish Shell is known for its user-friendly features and interactive capabilities. It offers syntax highlighting, autosuggestions, and a powerful scripting language. Fish Shell aims to provide a more intuitive and pleasant command-line experience.

What is Shebang?

Before writing the actual script first we have to write the Shebang. Shebang, also known as hashbang , is a character sequence at the beginning of a script file that specifies the interpreter to be used for executing the script. In Unix-like operating systems, including Linux and macOS, the shebang is defined by the characters "#!" (hash followed by an exclamation mark).

The shebang line is typically placed as the first line of a script file and is used by the operating system to determine how to interpret and execute the script. When a script file is invoked, the operating system reads the shebang line and launches the specified interpreter, passing the script file as an argument.

How to write and execute the script?

  1. First start your Linux machine Flavours of UNIX

  2. Use Vim editor to write the script Flavours of UNIX The "echo" command is commonly used in command-line interfaces and scripting languages to output text or variables to the standard output (usually the console or terminal). It allows you to display information or messages to the user or to redirect the output to a file or another command.

  3. Give file execution permissions and execute the file. Flavours of UNIX

Write a Shell Script to create a folder with the name DevOps and Inside that folder create a file and write "I love Scripting".

Flavours of UNIX

output:- Now a folder named devops is created, and we have the file inside that folder. Flavours of UNIX

Write a script to print the name and roll number by taking input from the user.

Flavours of UNIX

output:- Flavours of UNIX

Write a script to take input as an argument while running the script.

Flavours of UNIX

output:- Flavours of UNIX

Write a script to check whether the file exists in the system or not.

Flavours of UNIX

output:- Flavours of UNIX

Write a script to show the demonstration of the loop in scripting.

Flavours of UNIX

output:- Flavours of UNIX

Write a script to list all the files present in the Ubuntu directory.

Flavours of UNIX

output:- Flavours of UNIX