Shell Scripting Tutorial: Extended Shell Scripts

Introduction to Extended Shell Scripts

Hello, aspiring programmers! I'm thrilled to guide you through the exciting world of Unix/Linux shell scripting. As a computer science teacher with years of experience, I've seen countless students light up when they grasp these concepts. Today, we'll dive into extended shell scripts, building on the basics to create more powerful and flexible programs. Don't worry if you're new to this – we'll start from scratch and work our way up together!

Unix / Linux - Shell Scripting

What Are Extended Shell Scripts?

Extended shell scripts are like the Swiss Army knives of the Unix/Linux world. They allow us to create more complex and feature-rich programs using the shell's built-in commands and structures. Think of them as recipes that tell your computer how to cook up a delicious solution to your problems!

Basic Structure of an Extended Shell Script

Let's start with the skeleton of our script:

#!/bin/bash

# Your code goes here

exit 0

This might look simple, but it's the foundation of every script we'll write. The #!/bin/bash is called a shebang, and it tells the system which interpreter to use. The exit 0 at the end is like saying "goodbye" to your computer, indicating that the script finished successfully.

Variables and Data Types

In shell scripting, variables are like boxes where we store information. Let's see how to use them:

#!/bin/bash

name="Alice"
age=25
pi=3.14

echo "Hello, $name! You are $age years old."
echo "Pi is approximately $pi"

exit 0

When you run this script, it will output:

Hello, Alice! You are 25 years old.
Pi is approximately 3.14

Notice how we use $ to reference our variables. It's like telling the computer, "Hey, grab what's in this box for me!"

Control Structures

If-Else Statements

Now, let's make our script a bit smarter with some decision-making:

#!/bin/bash

age=18

if [ $age -ge 18 ]; then
    echo "You can vote!"
else
    echo "Sorry, you're too young to vote."
fi

exit 0

This script checks if the age is greater than or equal to 18. If it is, it tells you that you can vote. Otherwise, it says you're too young. The -ge means "greater than or equal to" in shell speak.

Loops

Loops are like a merry-go-round for your code. Let's see a for loop in action:

#!/bin/bash

for i in {1..5}
do
    echo "Round $i"
done

exit 0

This will count from 1 to 5, saying "Round" each time. It's like the script is a fitness instructor, counting your reps!

Functions

Functions are like mini-scripts within your script. They're reusable blocks of code:

#!/bin/bash

greet() {
    echo "Hello, $1! Nice to meet you."
}

greet "Bob"
greet "Alice"

exit 0

This script defines a greet function that says hello to whoever we tell it to. The $1 is a special variable that holds the first argument passed to the function.

Input and Output

Let's make our script interactive:

#!/bin/bash

echo "What's your name?"
read name

echo "Hello, $name! How old are you?"
read age

if [ $age -ge 18 ]; then
    echo "You can vote, $name!"
else
    echo "Sorry $name, you're too young to vote."
fi

exit 0

This script asks for your name and age, then decides if you can vote. It's like a friendly election official!

Working with Files

Shell scripts are great for file management. Let's create a script that backs up a file:

#!/bin/bash

original_file="important_data.txt"
backup_file="important_data_backup.txt"

if [ -f "$original_file" ]; then
    cp "$original_file" "$backup_file"
    echo "Backup created successfully!"
else
    echo "Error: $original_file not found."
fi

exit 0

This script checks if important_data.txt exists, and if it does, creates a backup. It's like having a personal assistant for your files!

Error Handling

Let's add some error handling to make our scripts more robust:

#!/bin/bash

divide() {
    if [ $2 -eq 0 ]; then
        echo "Error: Cannot divide by zero!"
        return 1
    fi
    result=$(($1 / $2))
    echo "Result: $result"
    return 0
}

divide 10 2
divide 10 0

exit 0

This script defines a divide function that checks for division by zero. It's like having a math tutor who catches your mistakes before you make them!

Command Line Arguments

Finally, let's create a script that uses command line arguments:

#!/bin/bash

if [ $# -eq 0 ]; then
    echo "Usage: $0 <name>"
    exit 1
fi

echo "Hello, $1! Welcome to shell scripting."

exit 0

Run this script with ./script.sh Alice, and it will greet Alice. The $# checks the number of arguments, and $0 is the script name itself.

Conclusion

Congratulations! You've just taken your first steps into the world of extended shell scripting. Remember, practice makes perfect. Try combining these concepts, experiment with your own ideas, and soon you'll be scripting like a pro!

Here's a table summarizing the key concepts we've covered:

Concept Description Example
Variables Store and retrieve data name="Alice"
If-Else Make decisions if [ $age -ge 18 ]; then ... fi
Loops Repeat actions for i in {1..5}; do ... done
Functions Reusable code blocks greet() { ... }
Input/Output Interact with users read name
File Operations Manage files cp "$original_file" "$backup_file"
Error Handling Manage potential issues if [ $2 -eq 0 ]; then ... fi
Command Line Args Use script parameters $1, $2, $#

Keep scripting, keep learning, and most importantly, have fun! The world of shell scripting is vast and exciting, and you've just begun your journey. Happy coding!

Credits: Image by storyset