4.11.1 Identify the basics of scripting Part 1

Introduction 

Imagine you're preparing for a big event—whether it’s organizing a sports tournament, coordinating a school project, or planning a community gathering. Just like setting up all the logistics to make sure everything runs smoothly, scripting is about organizing a computer’s tasks to ensure it performs reliably and efficiently.

In this lesson, you’ll learn how scripting helps solve problems, automate repetitive tasks, and manage systems more easily. Mastering these tools not only makes you more effective but also empowers you to help others—whether it’s streamlining team workflows, solving tricky tech issues, or keeping systems secure and productive. 

Understanding Coding and Scripting 

Coding Basics 

Coding is the process of writing instructions in a specific language that a computer can understand and follow to complete tasks. Each coding language has unique features, and languages can be grouped in several ways. Three important types of coding languages are: 

  • Shell Scripting Languages: These are specific to an operating system (OS), using commands designed for that system. 

  • General-Purpose Scripting Languages: These are not tied to any one OS. Scripts are run by an interpreter, which allows the same script to work across different systems. 

  • Programming Languages: Used to create an executable file, or app, that can run on an OS once installed.

Scripts are often called “glue languages” because they can connect functions from different software and operating systems, automating tasks across multiple tools without creating a new software application. 

Tools for Writing Code and Scripts 

You can write scripts in a simple text editor, but editors with script support are more efficient. These editors can: 

  • Parse Script Syntax: Highlight different elements of the script for easier reading and editing. 

  • Autocomplete Commands: Suggest possible commands as you type. 

  • Debugging: Help identify errors to make sure the script works as expected. 

For more complex coding, you can use an integrated development environment (IDE), which has many helpful features for both scripting and programming. 

Writing and Running a Shell Script in Linux 

In Linux, shell scripts commonly use the .SH file extension.

Here’s a simple guide to creating a shell script: 

  1. Begin with a Shebang Line: This line tells the OS which interpreter to use. For example, #!/bin/bash sets Bash as the interpreter. 

  2. Add Commands on Separate Lines: Each action the script performs should be on its own line. 

Example: 

bash 

Copy code 

#!/bin/bash 
echo 'Hello World' 
 

This script will display “Hello World” on the terminal. 

Setting Permissions and Running a Script 

  • Execute Permission: The script must have execute permissions to run. In Linux, permissions can be set for the user, group, or everyone (world). 

  • Running the Script: If the script is not set up in the system PATH variable, run it by navigating to its directory and typing ./filename.sh or by entering the full file path. 

Coding Syntax Essentials 

Understanding Syntax and Errors 

To develop a script in any language, it’s essential to understand that language’s specific syntax—the correct way to write commands. While many scripting languages share common constructs, each one has unique syntax rules. Errors in syntax prevent the script from running, while logical errors may cause the script to behave unexpectedly. 

Adding Comments 

Comments are lines of text within code that are ignored by the interpreter or compiler, used to explain parts of the code. Comments help with maintenance by describing what the code does. In many languages, including Bash, comments are marked by a # symbol

bash 

Copy code 

#!/bin/bash 
# This script greets the world 
echo 'Hello World' 

Arguments are a specific type of variable passed to the script when it starts. In Bash, positional arguments are referred to by $1, $2, etc., representing the order in which they are entered. 

Using Variables 

A variable is a label that stores a value which can change as the script runs. For example, a variable named FirstName could store a user’s first name. Variables are typically: 

  • Declared and defined at the start of a script 

  • Assigned a data type, such as text or number, and an initial value 

Controlling Execution with Branches and Loops 

In scripts, each statement generally runs sequentially from top to bottom. However, many tasks require more complex control structures to change the execution order, based on conditions. Two main types of control structures are branches and loops

Branches 

A branch allows the code to follow different paths based on the result of a test.

For example, the following Bash script prints "Hello Bobby" if run as ./hello.sh Bobby and "Hello World" if no argument is given:

bash 

Copy code 

#!/bin/bash 
# If statement example in Bash 
 
if [ -z "$1" ]; then 
    echo 'Hello World' 
else 
    echo "Hello $1" 
fi

Here, -z checks if $1 (the first argument) is unset or empty. Double quotes around variables help protect the input. 

Loops 

A loop repeats a block of statements based on a condition: 

  • For Loop: Runs a set number of times.

    The following example pings each address in a range: 

bash 

Copy code 

#!/bin/bash 
# For loop example in Bash 
 
for i in {1..254}; do 
    ping -c1 "192.168.1.$i" 
done

While and Until Loops: Repeat until a specific condition is met.

The example below pings an IP address until it receives a reply: 

bash 

Copy code 

#!/bin/bash 
# Until loop example in Bash 
 
until ping -c1 "$1" &>/dev/null; do 
    echo "192.168.1.$1 not up" 
done 
echo "192.168.1.$1 up"

Operators in Scripting 

Operators allow scripts to make decisions by evaluating logical expressions. Here are common comparison and logical operators: 

With branches and loops, operators guide the logic that determines if conditions are met, allowing scripts to follow complex instructions and execute tasks accordingly. 

Summary 

In this lesson, we learned the basics of coding and scripting, including different types of scripting languages, essential syntax, and key programming concepts like variables, branches, and loops. Mastering these fundamentals allows you to automate tasks and create efficient scripts for various systems.