Getting Started with Linux Scripting

Linux scripting is one of the most powerful skills you can develop as you begin your journey into the Linux ecosystem.

What is Linux Scripting?

Linux scripting refers to writing a series of commands in a file that can be executed by the shell (command-line interpreter). Instead of typing commands one by one, you can combine them into a script file that performs multiple operations automatically.

Think of a script as a recipe - it contains step-by-step instructions that tell your computer exactly what to do.

Why Learn Linux Scripting?

  • Automation: Perform repetitive tasks with a single command
  • Efficiency: Save time by automating complex processes
  • Customization: Create your own tools tailored to your needs
  • System Administration: Manage your Linux system more effectively
  • Career Advancement: A valuable skill for IT professionals

Getting Started: Your First Script

Let’s create a simple “Hello World” script to get started:

  1. Open a text editor (like nano, vim, or gedit):

    1
    nano hello.sh
  2. Add the following lines:

    1
    2
    3
    4
    5
    #!/bin/bash

    # This is a comment
    echo "Hello, World! This is my first bash script."
    echo "Today's date is $(date)"
  3. Save the file and exit the editor.

  4. Make the script executable:

    1
    chmod +x hello.sh
  5. Run your script:

    1
    ./hello.sh

You should see a greeting message followed by the current date. Congratulations! You’ve just created and run your first Linux script.

Breaking Down the Script:

  • #!/bin/bash - This is called a “shebang” line. It tells the system which interpreter to use to execute the script.
  • # This is a comment - Comments help explain what your script does.
  • echo - This command outputs text to the screen.
  • $(date) - This is command substitution. It runs the date command and includes its output in our echo statement.

Practical Example: Backup Script

Let’s create something more practical - a simple backup script that copies important files to a backup directory:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/bash

# Simple backup script
# Usage: ./backup.sh

# Variables
BACKUP_DIR="/home/user/backups/$(date +%Y%m%d)"
SOURCE_DIR="/home/user/documents"

# Create backup directory if it doesn't exist
if [ ! -d "$BACKUP_DIR" ]; then
mkdir -p "$BACKUP_DIR"
echo "Created backup directory: $BACKUP_DIR"
fi

# Copy files to backup directory
echo "Backing up files from $SOURCE_DIR to $BACKUP_DIR..."
cp -r "$SOURCE_DIR" "$BACKUP_DIR"

# Check if backup was successful
if [ $? -eq 0 ]; then
echo "Backup completed successfully!"
else
echo "Backup failed."
fi

This script:

  1. Creates a backup directory with today’s date
  2. Copies files from your documents folder to the backup directory
  3. Checks if the operation was successful and reports back

To use this script, save it as backup.sh, make it executable with chmod +x backup.sh, and run it with ./backup.sh.

Basic Linux Scripting Concepts

Variables

Variables store data that can be used throughout your script:

1
2
3
NAME="John"
AGE=25
echo "Hello, my name is $NAME and I am $AGE years old."

Conditional Statements

Make decisions in your scripts using if-else statements:

1
2
3
4
5
6
7
8
9
#!/bin/bash

FILE="/etc/hosts"

if [ -f "$FILE" ]; then
echo "$FILE exists."
else
echo "$FILE does not exist."
fi

Loops

Perform repetitive tasks with loops:

1
2
3
4
5
6
7
8
9
10
11
# For loop
for i in 1 2 3 4 5; do
echo "Number: $i"
done

# While loop
count=1
while [ $count -le 5 ]; do
echo "Count: $count"
count=$((count + 1))
done
1
2
3
4
5
6
7
8
commands=("hospital" "flights" "soccer" "beers" "inpatient" "facilities" "rayyan")

for i in {1..3}; do
echo "=== Round $i ==="
for command in "${commands[@]}"; do
python cli.py "$command"
done
done

User Input

Make your scripts interactive by accepting user input:

1
2
3
4
5
#!/bin/bash

echo "What is your name?"
read NAME
echo "Hello, $NAME! Nice to meet you."

Advanced Script: System Information

Here’s a more advanced script that displays system information:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash

# System information script

echo "=== System Information ==="
echo "Hostname: $(hostname)"
echo "Kernel Version: $(uname -r)"
echo "System Uptime: $(uptime -p)"
echo "CPU Information: $(grep "model name" /proc/cpuinfo | head -1 | cut -d ':' -f2)"
echo "Memory Information:"
free -h
echo "Disk Usage:"
df -h | grep "^/dev/"
echo "=== End of Report ==="

This script collects and displays various pieces of information about your system, including hostname, kernel version, uptime, CPU, memory, and disk usage.

Tips for Effective Scripting

  1. Start Simple: Begin with basic scripts and gradually add complexity
  2. Add Comments: Document your scripts so you (and others) can understand them later
  3. Use Meaningful Names: Give your variables and scripts descriptive names
  4. Error Handling: Include error checking in your scripts
  5. Test Thoroughly: Test your scripts in a safe environment before using them for important tasks

Next Steps

As you become more comfortable with scripting, here are some areas to explore:

  • Functions: Create reusable blocks of code
  • Command-line Arguments: Make your scripts more flexible
  • Regular Expressions: Powerful pattern matching
  • Text Processing: Tools like awk, sed, and grep
  • Cron Jobs: Schedule your scripts to run automatically

Resources for Further Learning

  1. Bash Guide for Beginners
  2. Advanced Bash-Scripting Guide
  3. Linux Command Line Basics
  4. Shell Scripting Tutorial

Conclusion

Linux scripting is a powerful skill that opens up a world of automation and efficiency.
By starting with simple scripts and gradually tackling more complex tasks, you’ll develop the confidence and knowledge to solve real-world problems.
Remember that the best way to learn scripting is by doing - so try the examples in this guide, modify them, break them, and fix them. Happy scripting!


Getting Started with Linux Scripting
https://www.hardyhu.cn/2025/05/20/Getting-Started-with-Linux-Scripting/
Author
John Doe
Posted on
May 20, 2025
Licensed under