Linux Script Executing

LinuxLinuxBeginner
Practice Now

Introduction

In the far reaches of the cyber desert, there is a server known only as "The Wasteland." In this desolate yet strangely interconnected landscape, a solitary figure known as the Storm Prophet roams, seeking to understand and master the powerful scripts that govern the workings of the servers like the unpredictable desert storms. The Storm Prophet is a revered character amongst the tech pilgrims who visit these parts, and they often seek the Prophet's guidance to harness the scripts and wield their hidden capabilities effectively.

The goal of this lab is to follow in the footsteps of the Storm Prophet and learn the ways of executing scripts in Linux using the source command. The source command is used to execute commands from a file in the current shell, and mastering it is key to managing complex environments like the Wasteland. By the end of this lab, you will be like a storm yourselfโ€”unleashing the hidden powers of scripts upon the vast deserts of your Linux environments.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux/BasicSystemCommandsGroup -.-> linux/source("`Script Executing`") subgraph Lab Skills linux/source -.-> lab-271387{{"`Linux Script Executing`"}} end

Creating and Sourcing a Basic Script

In this step, we're going to create a simple script and use the source command to run it in our current shell environment. The purpose here is to understand how source can be used to execute commands within the same shell context without spawning a new subprocess.

Let's create a script called storm.sh within our project directory ~/project. The script will contain simple commands to create an environmental variable and output a message.

First, navigate to the project directory:

cd ~/project

Next, create the script file storm.sh using your favorite text editor. Here's the content of the script:

#!/bin/bash
export STORM_POWER="Immeasurable"
echo "The storm's power is $STORM_POWER"

After saving the file, make it executable:

chmod +x storm.sh

Now, let's source the script:

source storm.sh

If everything is executed correctly, you should see the message:

The storm's power is Immeasurable

Additionally, the environmental variable STORM_POWER will now be available in your current shell. Verify this by echoing it:

echo $STORM_POWER

Using Source with Parameters

This step focuses on passing parameters to a script while sourcing it. The Storm Prophet often encounters situations where the power of the storm must be harnessed differently depending on the nature of the challenge. Similarly, we will pass parameters to our script to modify the behavior dynamically.

Create a new file called desert_storm.sh in the ~/project directory with the following content:

#!/bin/bash
export STORM_LEVEL=$1
echo "Summoning a level $STORM_LEVEL storm!"

Don't forget to make the script executable:

chmod +x desert_storm.sh

Let's source the script, passing a parameter for the storm's level:

source desert_storm.sh "Sandstorm"

You should see an output similar to this:

Summoning a level Sandstorm storm!

Check the STORM_LEVEL variable by echoing it:

echo $STORM_LEVEL

Summary

In this lab, we ventured into the desolate landscapes of Linux scripting, guided by the ways of the Storm Prophet. We explored the power of the source command to execute scripts within the same shell context. Our journey began with creating and sourcing a basic script and evolved into passing parameters while sourcing to manipulate script behavior dynamically.

Our path mimicked the stormsโ€”unpredictable, yet when understood, incredibly powerful and essential for mastering Linux environments. This lab was designed not just to impart knowledge, but also to inspire exploration in the expansive world of scripting, much like the prophetic journeys across the cyber deserts. May the wisdom of the Storm Prophet guide you on your future Linux endeavors.

Other Linux Tutorials you may like