Linux Shell Scripting

Linux Shell Scripting

What is Shell Scripting?

Shell scripting refers to writing scripts or programs that are interpreted and executed by a shell, which is a command-line interpreter for operating systems like Unix, Linux, and macOS. The shell is an interface between the user and the operating system, allowing users to execute commands and perform various tasks.

A shell script is a text file containing a series of commands written in a scripting language specific to the shell being used, such as Bash (Bourne Again SHell) or C Shell (csh). These scripts can automate repetitive tasks, perform system administration tasks, or combine multiple commands into a single script for more complex operations.

Why do we need Shell Scripting?

  • Automation: Shell scripts enable the automation of repetitive tasks. Instead of manually executing a series of commands each time, you can write a script to perform those tasks automatically. This saves time and reduces the chances of human error.

  • Batch Processing: Shell scripting allows you to process large amounts of data or perform actions on multiple files simultaneously. By writing a script, you can automate the handling of multiple inputs or files, making batch processing efficient and consistent.

  • System Administration: Shell scripting is widely used for system administration tasks. It allows administrators to configure and manage systems, set up scheduled tasks, perform backups, monitor system resources, and more. Scripts provide a way to streamline administrative tasks and maintain system stability.

  • Customization: Shell scripts enable users to customize their environment and create personalized workflows. You can write scripts to set up specific configurations, install software, or tailor the behavior of your system according to your needs.

  • Rapid Prototyping: Shell scripting provides a quick and easy way to prototype ideas or test concepts. You can write scripts to experiment with commands, combine different tools, or test algorithms before implementing them in more complex programming languages.

About VIM Editor

Vim is a highly popular and powerful text editor available on Linux and other Unix-like operating systems. It is known for its efficiency, extensibility, and versatility. Vim stands for "Vi IMproved," as it is an enhanced version of the original Vi editor.

Some Key Features

Here are some key features and characteristics of Vim:

  • Modal Editing: Vim follows a modal editing paradigm, which means it has different modes for different purposes. The most commonly used modes are the command mode and the insert mode. In the command mode, you can navigate, search, and perform various editing operations, while in the insert mode, you can directly input and edit text.

  • Command-driven: Vim provides a wide range of commands for manipulating text, such as copy, paste, delete, search, replace, and more. These commands are executed by entering them in the command mode.

  • Extensibility: Vim is highly customizable and extensible. It supports plugins, and scripting in languages like Vimscript, Python, and Lua, and allows users to define their own key mappings, functions, and macros.

  • Syntax Highlighting: Vim can automatically highlight syntax elements in different programming and markup languages, making it easier to read and edit code.

  • Split Windows: Vim allows you to split the editing window into multiple panes, enabling you to view and edit multiple files simultaneously.

Some VIM Editor Shortcuts/Commands

  • w: It is used to save the edited text in the file.

  • q: It is used to exit the VI editor.

  • wq: It is used to save and exit the VI editor.

  • syntax on: Syntax highlighting.

  • set hlsearch: Highlight search results

  • set tabstop=4: Tab character space

  • set autoindent: Turns on auto-indent feature

Some Basic Syntax

  • echo - Echo is a Unix/Linux command tool used for displaying lines of text or string that are passed as arguments on the command line.

  • printf - The print command converts, formats, and writes its Argument parameters to standard output.

    ex - printf "First name: %s Last name %s" $firstname $lastname

  • \n - The \n is a newline character for Unix-based systems; it helps to push the commands that come after it onto a new line.

How to Create a Command?

  1. Create a Directory in home/ubuntu named bin.

  2. Create a shell script file.

  3. Move that file into the bin directory using the mv command to the directory which we have created.

  4. Copy the path of your Directory to the ~/.bashrc file. using (export PATH=/bin: "$PATH")

  5. Reload the /.bashrc file using the source ~/.bashrc command.

  6. Congratulations you have successfully created a file into command.

Creating Variables

What are Variables?

Variables in shell scripting are used to store and manipulate data within a script. They provide a way to assign values to names and use those values throughout the script. Shell scripting supports both global and local variables.

How to take Input using Variable

  • You can use the read command with Option -p and then write the variable name.

  • ex - read -p input1

  • You can add a string in between them

  • ex - read -p "write your name" input1

  • Then use the echo command with a dollar sign to show the output.

  • ex - echo $input1

Multiple Input

  • You can take multiple inputs in one line.

  • ex - Firstname="nidhish" Lastname="malav"

    echo $Firstname $Lastname

Special Variables

  • Individual arguments

    $1, $2, and all variables named as positive nonzero integers contain the values of the script parameters or arguments.

  • #!/bin/sh

    echo Argument: $1

    echo Argun $1

    echo Argemtot $1

    ./variables.no one two three

  • All arguments $@

    The $@ variable represents all of a script's arguments and is very useful for passing them to a command inside the script.

  • Script name $0

    The $0 variable holds the name of the script and is useful for generating diagnostic messages.