0% found this document useful (0 votes)
5 views4 pages

How to Execute Shell Scripts

This document provides a comprehensive guide on executing shell scripts, detailing prerequisites, environment preparation, permission checks, execution methods, and troubleshooting. It also covers optional automation through cron jobs and Windows Task Scheduler, as well as the importance of checking exit codes and documenting script-specific details. The guide is aimed at users of Unix-based systems and Windows Subsystem for Linux (WSL).

Uploaded by

jehorod844
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

How to Execute Shell Scripts

This document provides a comprehensive guide on executing shell scripts, detailing prerequisites, environment preparation, permission checks, execution methods, and troubleshooting. It also covers optional automation through cron jobs and Windows Task Scheduler, as well as the importance of checking exit codes and documenting script-specific details. The guide is aimed at users of Unix-based systems and Windows Subsystem for Linux (WSL).

Uploaded by

jehorod844
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

How to Execute Shell Scripts

Here's a approach to documenting the steps:

1. Prerequisites
Before running the scripts, ensure the following:

A Unix-based operating system (Linux/Mac) or Windows Subsystem for Linux (WSL) is


installed.
The shell scripts are accessible (stored locally or available via a remote repository).
A terminal application (e.g., Terminal, Bash, or any preferred shell) is available.

2. Preparing the Environment


1. Open the Terminal:
On Linux/Mac: Open the terminal from the applications menu or press Ctrl +
Alt + T (Linux) / Cmd + Space and search for "Terminal" (Mac).
On Windows: Open the WSL terminal or Git Bash.
2. Navigate to the Directory: Use the cd command to navigate to the directory where
the shell scripts are stored. For example:

cd /path/to/scripts/

3. Verify Script Files: Use the ls command to list the scripts in the directory:

ls

3. Ensure Script Permissions


Before execution, ensure the scripts have the proper execute permissions.

1. Check Permissions: Run the following command:

ls -l script_name.sh
Look for the x (executable) flag in the file's permissions.
2. Add Execute Permission (if needed): If the x flag is missing, make the script
executable:

chmod +x script_name.sh

4. Executing the Script


1. Run the Script: Use one of the following methods:
Direct execution:

./script_name.sh

Using the shell explicitly:

bash script_name.sh

or

sh script_name.sh

2. Optional - Run with Sudo: If the script requires administrative privileges, prepend the
command with sudo :

sudo ./script_name.sh

Enter the password when prompted.

5. Handle Script Inputs (if any)


Some scripts may prompt for inputs during execution. Follow the on-screen instructions
and provide the required values.
If a configuration file is needed, ensure it is correctly set up before running the script.

6. Troubleshooting Errors
1. Check the Output: Review the terminal output for any error messages or prompts.
2. Common Issues:
Missing dependencies: Install them using your package manager (e.g., apt , yum ,
or brew ).
Incorrect paths: Double-check file and directory paths.
3. Debugging: Add -x to the script execution for debugging:

bash -x script_name.sh

7. Automating Script Execution (Optional)


Use a Cron Job (Linux/Mac): Automate the script to run at specified intervals.
Example:

crontab -e

Add the following line for daily execution at 10 AM:

0 10 * * * /path/to/script_name.sh

Windows Task Scheduler: Schedule the script using the Windows Task Scheduler for
automation.

8. Exit Codes
After execution, check the exit code to confirm success:

echo $?

0 indicates success.
Any non-zero value indicates an error.

9. Document Script-Specific Details


For each script, include:
Script Name
Purpose
Dependencies
Inputs/Outputs
Expected Outcome

Example:

Script: backup.sh
Purpose: Backup the home directory to an external drive.
Dependencies: rsync, external drive mounted at /mnt/backup.
Inputs: None.
Outputs: Backup logs stored in /var/log/backup.log.
Expected Outcome: The home directory is backed up without errors.

You might also like