Introduction to Shell Scripting Essentials

Aug 23, 2024

Notes from Learn with Mithiran: Introduction to Shell Scripting

Overview

  • Topic: Introduction to Shell Scripting (Path 3 in Linux series)
  • Previous topics: Linux fundamental commands and using regular expressions with grep and set commands.
  • Future topics: Advanced concepts including looping and condition statements.

Setting Up Environment

  • Installation on Windows:
    • Search and install Ubuntu from Microsoft Store.
    • Alternative: Create a Linux version on AWS EC2 (not demonstrated today).
  • Windows Subsystem for Linux (WSL):
    • Check if WSL is enabled on the Windows machine.
    • Enable WSL by navigating to: Settings > Turn Windows features on or off > Check "Windows Subsystem for Linux".
    • Restart the system if required.

Installing Tools

  1. Install Ubuntu:
    • Search Microsoft Store for Ubuntu and click on the 'Get' button.
  2. Install Visual Studio Code (VS Code):
    • Search for VS Code in the browser and download it for Windows.
    • Install the WSL extension in VS Code.
  3. Run VS Code:
    • Open VS Code from the Ubuntu terminal using the command code .

Basics of Shell Scripting

  • What is Shell Scripting?:
    • A method to communicate with the computer using a programming language.
    • Shells: Bash (Born Again SHell), Z-Shell, K-Shell, etc.
  • Interacting with Linux:
    • Use commands like pwd (print working directory) and ls (list files).
    • Shell scripts allow you to write commands in a file and execute them.

Writing a Shell Script

  1. Creating a Shell Script:
    • Create a file named main.sh.
    • Start with a shebang #!/bin/bash to specify the shell.
    • Write commands (e.g., pwd, ls) in the file.
  2. Running a Shell Script:
    • Make the script executable with chmod +x main.sh.
    • Run it using ./main.sh or sh main.sh.

Variables in Shell Scripting

  • Defining Variables:
    • Syntax: variable_name=value (e.g., a=10).
    • Print variable value with echo $variable_name.
  • Rules for Variables:
    • First character must be a letter or underscore.
    • Use uppercase for variable names as a convention.
  • Types of Variables:
    • Local Variables: Defined within scripts.
    • Environment Variables: Accessible system-wide.
    • Shell Variables: System variables available to the shell.

Input and Output

  • Using echo:
    • Display output in the terminal (e.g., echo Hello World).
  • Using read:
    • Capture user input (e.g., read name).
    • Combine with echo to greet users (e.g., echo Hey nice to meet you, $name).

Special Variables

  • Command-Line Arguments:
    • $0: Name of the script.
    • $1, $2: Arguments passed to the script.
    • $#: Number of arguments passed.
    • $$: Process ID of the current shell.
    • $?: Exit status of the last command.

Operators in Shell Scripting

  1. Arithmetic Operators:
    • Use expr to evaluate expressions (e.g., expr $a + $b).
  2. Relational Operators:
    • Compare values (e.g., -eq, -ne, -gt, -lt).
  3. String Operators:
    • Check if strings are empty or equal (e.g., -z, -n).
  4. File Operators:
    • Check if a file exists or is readable/writable (e.g., -e, -r, -w).

Conclusion

  • Today's focus: Basics of setting up Linux, shell scripting fundamentals, variables, and operators.
  • Future sessions will cover loops and conditionals.
  • Encouragement to like and subscribe to the channel for more content.