Skip to content

Latest commit

 

History

History
187 lines (116 loc) · 4.99 KB

README.md

File metadata and controls

187 lines (116 loc) · 4.99 KB

Shell Scripting Essentials: A Comprehensive Guide

Introduction

Shell scripting is a cool way to make your computer do things for you automatically. In this guide, we'll cover the basics of shell scripting, including what scripts are, how to use them, and some essential concepts like variables, tests, and decision-making.

What is a Script?

A script is a command-line program that comprises a series of commands executed by an interpreter. In simpler words, A script is like a recipe for your computer. It's a list of commands that your computer follows to get things done. You can automate tasks using scripts, making your computer work for you.

Let's start with a simple shell script example:

#!/bin/bash
# This is a simple shell script

echo "Hello, world!"

This script just says, "Hey computer, print 'Hello, world!'". Before executing a shell script, ensure it has executable permissions using chmod +x script.sh. If the script is in the system's path, executing it is as simple as typing its name. Otherwise, provide the relative or full path.

Shebang: The Script's Interpreter

The shebang (#!) is a critical element at the beginning of a script, followed by the path to the interpreter. For example:

#!/bin/bash

This indicates that the Bash shell should interpret and execute the script. It's like saying, "Hey computer, this is a Bash script!” The shebang is crucial for specifying the interpreter, ensuring compatibility and avoiding reliance on the user's default shell.

Using Variables in Shell Scripts

Variables in shell scripts are storage locations with names. To assign a value, use the syntax VARIABLE_NAME=Value. Variables are case-sensitive, and naming conventions often involve uppercase letters. Access a variable's value using $VARIABLE_NAME or ${VARIABLE_NAME}. The latter form is necessary when appending or preceding additional data.

Example:

MY_SHELL="bash"
echo "I am using ${MY_SHELL} shell."

Here, MY_SHELL is a container holding the value "bash". We use it later to say, "I am using bash shell."

You can also capture command output in a variable:

SERVER_NAME=$(hostname)
echo "Server name is ${SERVER_NAME}."

Performing Tests and Making Decisions

Shell scripts often involve making decisions based on certain conditions. Tests are created using conditional expressions enclosed in brackets ([ and ]). Common tests include checking file existence, readability, writability, and various string and numeric comparisons.

Example:

if [ -d "/path/to/directory" ]; then
    echo "Directory exists."
fi

Explore test options with help test or man test in Bash.

Using If Statements

Sometimes you want your script to decide what to do based on certain conditions. If statements help with that. The basic syntax is:

if [ condition ]; then
    # Commands to execute if condition is true
fi

Example:

#!/bin/bash
# Simple if statement

value=42

if [ $value -eq 42 ]; then
    echo "The value is 42."
fi

This script checks if the value is 42 and prints a message. Simple, right?

Adding Else and Elif Statements

Extend decision-making with else and elif statements:

if [ condition ]; then
    # Commands if condition is true
else
    # Commands if condition is false
fi

or:

if [ condition ]; then
    # Commands if condition is true
elif [ another_condition ]; then
    # Commands if another_condition is true
else
    # Commands if both conditions are false
fi

Implementing For Loops

For loops are like saying, "Hey computer, do this for every item in the list." The syntax is:

for variable in item1 item2 item3; do
    # Commands to execute for each item
done

Example:

#!/bin/bash
# Simple for loop

colors="red green blue"

for color in $colors; do
    echo "Color: $color"
done

This script prints each color in the list. Easy peasy!

Positional Parameters: Accessing Command-Line Inputs

Positional parameters represent values passed to a script. Access them using variables like $1, $2, etc., where $0 is the script's name. The special variable $@ captures all parameters.

Example:

#!/bin/bash
# Using positional parameters

echo "Script name: $0"
echo "First parameter: $1"
echo "Second parameter: $2"
echo "All parameters: $@"

Accepting User Input

Scripts can talk to you! You can ask for information using the read command. Here's an example:

#!/bin/bash
# Accepting user input

echo -n "Enter your name: "
read username

echo "Hello, $username!"

This script asks for your name and then says hello. It's like having a little chat with your computer.

Conclusion

Shell scripting is like giving your computer superpowers. With just a bit of code, you can make it do things automatically. Remember the basics: scripts are like recipes, use variables to remember things, make decisions with if statements, repeat tasks with for loops, and have a chat with your script using user input. Happy scripting!