Script execution and permissions

3.3 Turning Commands into a Script (Weight: 4)

📘Linux Essentials (LPI 010-160)


Script Execution and Permissions

In Linux, a script is a text file that contains multiple commands. Instead of typing commands one by one in the terminal, they can be saved in a script file and executed together.

For a script to run properly, two important things must be understood:

  1. How scripts are executed
  2. Script file permissions

Understanding these topics is important for the Linux Essentials exam, because Linux uses permissions and interpreters to control how scripts run.


1. What is Script Execution?

Script execution means running a script file so that the commands inside the file are executed by the system.

When a script runs, the commands inside it are processed by a shell interpreter such as:

  • bash
  • sh
  • zsh

Most Linux scripts use Bash.

A typical script file example:

#!/bin/bash
echo "System status"
uptime
date

When this script runs, the shell executes each command from top to bottom.


2. The Shebang (#!)

At the top of most scripts you will see a line like this:

#!/bin/bash

This line is called the shebang.

Purpose of the Shebang

The shebang tells Linux which interpreter should run the script.

Examples:

ShebangInterpreter
#!/bin/bashBash shell
#!/bin/shPOSIX shell
#!/usr/bin/python3Python interpreter
#!/usr/bin/perlPerl interpreter

Example script:

#!/bin/bash
echo "Backup started"

Without the shebang, the system may not know which interpreter to use.


3. Ways to Execute a Script

There are three main ways to run a script in Linux.


Method 1: Run Script with Shell Interpreter

The script can be executed by specifying the shell explicitly.

Example:

bash script.sh

or

sh script.sh

In this method:

  • Execute permission is not required
  • The shell reads the file and executes it

Example:

bash backup.sh

This runs the script using the Bash shell.


Method 2: Execute Script Directly

A script can also run like a program.

Example:

./script.sh

However, this requires:

  1. Execute permission
  2. Shebang line

Example:

./backup.sh

The ./ tells the shell:

Run the file located in the current directory.

Linux does not automatically search the current directory for security reasons.


Method 3: Run Script Using Absolute or Relative Path

Scripts can also be executed using their full path.

Example:

/home/admin/scripts/backup.sh

or relative path:

../scripts/backup.sh

This method is common in system automation.


4. Understanding Linux File Permissions

Linux controls access to files using permissions.

There are three permission types:

PermissionSymbolMeaning
ReadrView file contents
WritewModify file
ExecutexRun file as program/script

Permissions are assigned to three groups:

User TypeMeaning
User (u)File owner
Group (g)Group members
Others (o)All other users

5. Viewing File Permissions

Permissions can be viewed using:

ls -l

Example output:

-rwxr-xr-- 1 admin admin 1200 Mar 10 backup.sh

Permission breakdown:

-rwxr-xr--

Meaning:

SectionMeaning
-Regular file
rwxOwner permissions
r-xGroup permissions
r--Others permissions

So in this example:

  • Owner can read, write, execute
  • Group can read and execute
  • Others can only read

6. Why Execute Permission is Required for Scripts

A script must have execute permission to run directly.

Without execute permission:

./script.sh

Result:

Permission denied

This protects the system from running unauthorized scripts.


7. Changing Script Permissions

Permissions can be modified using the chmod command.

Syntax:

chmod [permissions] filename

Method 1: Symbolic Mode

Permissions can be added or removed using symbols.

Examples:

Add execute permission for owner:

chmod u+x script.sh

Add execute permission for everyone:

chmod +x script.sh

Remove execute permission:

chmod -x script.sh

Add execute permission to group:

chmod g+x script.sh

Method 2: Numeric (Octal) Mode

Permissions can also be set using numbers.

PermissionValue
Read4
Write2
Execute1

Examples:

PermissionValue
rwx7
rw-6
r-x5
r–4

Example command:

chmod 755 script.sh

Meaning:

UserGroupOthers
7 (rwx)5 (r-x)5 (r-x)

This is a common permission for scripts.


8. Typical Script Permission Settings

Common script permissions used in IT environments:

PermissionMeaning
700Only owner can run
750Owner full, group execute
755Everyone can execute
644Not executable

Example:

chmod 755 deploy.sh

9. Script Location and PATH Variable

Linux only automatically runs programs that are in directories listed in the PATH variable.

To see PATH:

echo $PATH

Example output:

/usr/local/bin:/usr/bin:/bin:/usr/sbin

If a script is stored in one of these directories, it can run without specifying its path.

Example:

backup.sh

However, scripts stored in the current directory require:

./backup.sh

10. Practical IT Environment Example

Scripts are commonly used in system administration tasks.

Example script: check_disk.sh

#!/bin/bash
echo "Disk usage report"
df -h

Steps to run:

Create script:

nano check_disk.sh

Add execute permission:

chmod +x check_disk.sh

Execute script:

./check_disk.sh

This script could be used by a system administrator to quickly check server disk usage.


11. Security Considerations for Scripts

Scripts should be secured properly.

Important practices:

1. Avoid giving execute permission to everyone unnecessarily

Example:

chmod 700 script.sh

Only the owner can run the script.


2. Store administrative scripts in protected directories

Examples:

/usr/local/bin
/opt/scripts

3. Restrict write permissions

Example:

-rwxr-xr-x

Only the owner can modify the script.


12. Common Script Execution Errors

Permission Denied

Error:

Permission denied

Cause:

Script does not have execute permission.

Fix:

chmod +x script.sh

Command Not Found

Error:

script.sh: command not found

Cause:

Current directory is not in PATH.

Fix:

./script.sh

Incorrect Interpreter

Error occurs if the interpreter in the shebang does not exist.

Example:

#!/bin/bash

If bash is missing or path is incorrect, the script will fail.


13. Best Practices for Script Execution

For reliable script execution:

  1. Always include a shebang
  2. Use clear script names
  3. Set correct execute permissions
  4. Store scripts in standard directories
  5. Limit permissions to prevent unauthorized modification

14. Key Exam Points (Very Important)

For the Linux Essentials exam, remember:

  • A script is a file containing commands.
  • Scripts usually begin with a shebang (#!).
  • Scripts can be executed in three ways:
    • bash script.sh
    • ./script.sh
    • /path/script.sh
  • Execute permission (x) is required to run a script directly.
  • Use chmod to change permissions.
  • Use ls -l to view permissions.
  • Numeric permissions example:
    • 755
    • 700
  • The PATH variable controls where the system searches for executable files.
Buy Me a Coffee