Cyber Security
Back Next

Basic Scripting


What Is a Script?

A script is:

A text file that contains a list of commands.

When you run the script, the system executes each command in order.

Think of it like:

Instead of telling someone step-by-step what to do every time, you write instructions once and hand them over whenever needed.

Why Use Scripting?

Scripting helps you:

  • Automate repetitive tasks
  • Save time
  • Reduce human error
  • Perform complex operations easily
  • Run tasks on multiple files at once

Example tasks:

  • Backing up files
  • Renaming many files
  • Starting multiple programs
  • Cleaning temporary files

Basic Bash Script (Linux/macOS)

On Linux or macOS, scripting is often done using Bash.

Create a file called:

myscript.sh

Example:

#!/bin/bash

echo "Hello, world!"
echo "This is my first script."

mkdir TestFolder
cd TestFolder
touch example.txt

echo "Script finished."

Explanation:

  • #!/bin/bash tells the system to use Bash.
  • echo prints text.
  • mkdir creates a folder.
  • cd changes directory.
  • touch creates a file.

To make it executable:

chmod +x myscript.sh

To run it:

./myscript.sh

Basic Batch Script (Windows)

On Windows, basic scripting is often done using batch files.

Create a file called:

myscript.bat

Example:

@echo off

echo Hello, world!
echo This is my first script.

mkdir TestFolder
cd TestFolder
type nul > example.txt

echo Script finished.
pause

Explanation:

  • @echo off hides command repetition.
  • echo prints text.
  • mkdir creates a folder.
  • cd changes directory.
  • type nul > file.txt creates a file.
  • pause waits for user input.

To run it:

Double-click the file

or run from Command Prompt:

myscript.bat

Variables in Scripts

Scripts can store information using variables.

Bash Example

#!/bin/bash

name="John"
echo "Hello, $name"

Windows Batch Example

@echo off

set name=John
echo Hello, %name%
pause

Variables allow your script to become dynamic.

Simple User Input

Bash

#!/bin/bash

echo "Enter your name:"
read name
echo "Hello, $name"

Windows Batch

@echo off

set /p name=Enter your name:
echo Hello, %name%
pause

Now the script interacts with the user.

Basic Conditional Logic

Scripts can make decisions.

Bash

#!/bin/bash

number=10

if [ $number -gt 5 ]; then
    echo "Number is greater than 5"
fi

Windows Batch

@echo off

set number=10

if %number% GTR 5 (
    echo Number is greater than 5
)

pause

This allows scripts to behave differently depending on conditions.

Basic Loop Example

Loops repeat commands.

Bash

#!/bin/bash

for i in 1 2 3
do
    echo "Number $i"
done

Windows Batch

@echo off

for %%i in (1 2 3) do (
    echo Number %%i
)

pause

How Scripts Work Internally

When you run a script:

  1. The shell reads the file.
  2. It interprets each line.
  3. It executes commands one by one.
  4. It stops if an error occurs (depending on settings).

A script is not compiled like a traditional program.

It is interpreted line by line.

Example Code:
# Bash Script Example
#!/bin/bash
echo "Hello, world!"
echo "This is my first script."
mkdir TestFolder
cd TestFolder
touch example.txt
echo "Script finished."

# Make executable
chmod +x myscript.sh
./myscript.sh

# Bash Variable Example
name="John"
echo "Hello, $name"

# Bash Input Example
echo "Enter your name:"
read name
echo "Hello, $name"

# Bash Conditional
number=10
if [ $number -gt 5 ]; then
    echo "Number is greater than 5"
fi

# Bash Loop
for i in 1 2 3
do
    echo "Number $i"
done

@echo off
echo Hello, world!
echo This is my first script.
mkdir TestFolder
cd TestFolder
type nul > example.txt
echo Script finished.
pause

set name=John
echo Hello, %name%
pause

set /p name=Enter your name:
echo Hello, %name%
pause

set number=10
if %number% GTR 5 (
    echo Number is greater than 5
)
pause

for %%i in (1 2 3) do (
    echo Number %%i
)
pause
Cyber Security
Introduction Types of Cyber Threats Cyber Security Domains CIA Triad (Confidentiality Integrity Availability) Career paths in Cyber Security Certifications Ethics and Responsible Disclosure Laws and Regulation (e.g. GDPR, NDPR) What is an OS? Types: Window, Linus, macOS Command-line vs GUI OS Internals Overview (filesystems, processes, permissions) Windows command prompt basics Linux Bash Basics File System Navigation Basic Scripting IP Addressing DNS, DHCP Mac Address OSI VS TCP/IP Models Ports and Protocols (TCP, UDP) Common Protocols (HTTPS, FTP, SSH, etc.) Packet structure Firewalls, IDS/IPS, VPNs
All Courses
Bootstrap Content Writing CSS Cyber Security Data Analysis Deep Learning Email Marketing Excel HTML Java Script Machine Learning MySQLi PHP Power Bi Python for Analysis SEO SMM SQL