Shell Scripting

Github link: https://github.com/Pardeep32/shell-script-for-devops.git

First understand Linux Architecture Overview:

Linux is an open-source operating system kernel originally developed by Linus Torvalds in 1991. It is the heart of various Linux distributions, which are complete operating systems that include the Linux kernel along with a collection of software applications and utilities.

Key Components:

  1. Kernel:At the core of Linux architecture is the kernel, which provides essential services such as process management, memory management, device drivers, and system calls.It interacts directly with hardware and manages system resources efficiently.

  2. System Libraries:Linux systems utilize various system libraries, such as the GNU C Library (glibc), to provide standard functions and interfaces for user-space programs.These libraries abstract low-level system functionalities, making them accessible to application developers.

  3. System Utilities:Linux distributions include a wide range of system utilities and commands for performing tasks such as file management, process control, network configuration, and system administration.Common utilities include ls, grep, ps, ifconfig, top, etc.

  4. Shell:The shell is a command-line interface that allows users to interact with the operating system by entering commands. Linux supports various shells, including Bash (Bourne Again Shell), Zsh (Z Shell), and Fish (Friendly Interactive Shell).

  5. Filesystem Hierarchy:Linux follows a hierarchical filesystem structure with directories organized in a tree-like format. Important directories include /bin (essential command binaries), /etc (system configuration files), /home (user home directories), /var (variable data), and /proc (virtual filesystem providing information about processes and system resources).

    Those are indeed the essentials for getting started with shell scripting:

    1. BASH (Bourne Again SHell):

BASH is one of the most widely used shell interpreters on Unix-like operating systems, including Linux. It provides a command-line interface for users to interact with the system and execute commands.

BASH also supports scripting, allowing users to write sequences of commands into text files (shell scripts) for automation and system management tasks.

  1. Linux Machine and SSH Access: Access to a Linux machine is essential for practicing shell scripting. You can either use a physical Linux system or set up a virtual machine using software like VirtualBox or VMware. SSH (Secure Shell) is a network protocol that allows secure remote access to the Linux machine over an encrypted connection. Ensure SSH is installed and properly configured on the Linux machine. You'll need the SSH credentials (username and password or SSH keys) to establish a connection.

    1. Text Editor: A text editor is necessary for writing and editing shell scripts. While you can use any text editor of your choice, some popular ones include:

      Nano: A simple and beginner-friendly text editor available on most Unix-like systems. It features basic functionalities and keyboard shortcuts.

      Vim: A powerful and highly customizable text editor with modes for navigation, editing, and command execution. It has a steep learning curve but offers efficient editing capabilities once mastered.

      Craete a git repo and clone that repo in your linux machine:

      To make shell script executalbe have to change the permission of file.

      "chmod 754 first.sh" sets permissions for "first.sh" to read, write, and execute for the owner (7), read and execute for the group (5), and read-only for others (4).

      A brief description of each component in a shell script:

      1. Variables:

        • Variables are used to store data values in a shell script. They can hold various types of data, such as strings, numbers, or arrays. Variable names are case-sensitive and can consist of letters, numbers, and underscores.

Now, ask user to enter input:

  1. Arguments: Arguments are values provided to a script when it is executed. They are accessed within the script using special variables like $1, $2, etc., representing the first, second, etc., arguments respectively. Arguments allow scripts to accept input from users or other programs.

    In the statement echo "the character in $0 $1 $2 $3", are placeholders representing command-line arguments. $0 represents the script name itself (argument.sh in this case). $1, $2, and $3 represent the first, second, and third command-line arguments respectively, which are typically provided when executing the script.

    If you want to add multiple user at once:

  2. Conditionals (if-else elif):

    • Conditionals are used to execute different code blocks based on certain conditions. The if statement checks a condition and executes a block of code if the condition is true. The else statement is used to execute a block of code if the condition in the if statement is false. The elif statement (short for "else if") allows for multiple condition checks in sequence.

  3. Loops (for and while loop):

    • Loops are used to execute a block of code repeatedly. The for loop iterates over a sequence of values (e.g., numbers, strings) and executes a block of code for each value.

The while loop continues executing a block of code as long as a specified condition is true. Loops allow scripts to perform repetitive tasks efficiently.

  1. Functions:

    • Functions are named blocks of code that can be defined once and reused multiple times within a script. They help in organizing and modularizing code, making scripts more readable and maintainable.Functions can accept parameters (arguments) and return values, allowing for flexible and reusable code blocks.

      Thank you for taking the time to read this article. Your interest and engagement mean the world to me. I hope you found the content informative, helpful, and perhaps even inspiring.

      Your support and encouragement drive me to continue creating valuable content, and I am immensely grateful for the opportunity to share my knowledge and insights with you.

      If you have any questions, feedback, or suggestions, please don't hesitate to reach out. I'd love to hear from you!

      Once again, thank you for being a part of this journey. Your presence and readership are deeply appreciated.

      Warm regards,