sh Command in Linux

Introduction to Linux Shell Scripting

This lab provides a comprehensive introduction to shell scripting within a Linux environment. You'll gain practical experience understanding shell variables, mastering parameters, and implementing conditional statements and loops. By creating and executing various shell scripts, you'll learn to automate tasks and optimize your workflow. This hands-on tutorial covers creating, executing, and manipulating shell scripts, using shell variables effectively, and leveraging command-line arguments. Ultimately, you will acquire the essential skills to effectively utilize the Linux shell for a wide range of scripting and systemadmin tasks.

Understanding Shell Scripting Fundamentals

This section covers the foundational elements of shell scripting on Linux systems. Shell scripts are essentially small programs written in a shell language that are used to automate repetitive tasks and streamline systemadmin workflows.

Let's begin by creating a new shell script file using the nano text editor:

cd ~/project
nano hello.sh

Now, add the following code to the file:

#!/bin/bash
echo "Hello, World!"

The first line, #!/bin/bash, known as the "shebang," instructs the system to use the Bash shell interpreter to execute the script.

To grant execute permissions to the script, run the following command:

chmod +x hello.sh

Execute the script using the following command:

./hello.sh

Expected output:

Hello, World!

The echo command is used to display the "Hello, World!" message on the console.

Next, let's explore basic shell variables. Create a new script named variables.sh:

nano variables.sh

Enter the following content:

#!/bin/bash
NAME="John Doe"
echo "My name is $NAME"

Execute the script:

chmod +x variables.sh
./variables.sh

Expected output:

My name is John Doe

In this example, we defined a shell variable NAME and utilized it within the echo command to display its value.

Working with Shell Variables and Parameters

In this part, you'll discover how to effectively utilize shell variables and parameters in your scripts to handle dynamic data and input.

First, create a new script called args.sh. This script will demonstrate how to work with command-line arguments:

cd ~/project
nano args.sh

Add the following content to the file:

#!/bin/bash
echo "Positional parameter 1: $1"
echo "Positional parameter 2: $2"
echo "All parameters: $@"
echo "Number of parameters: $#"

Save the file and make it executable:

chmod +x args.sh

Now, run the script with some example arguments:

./args.sh apple banana cherry

Expected output:

Positional parameter 1: apple
Positional parameter 2: banana
All parameters: apple banana cherry
Number of parameters: 3

This example demonstrates accessing command-line arguments using special variables such as $1, $2, $@, and $#. These variables provide access to the arguments passed when executing the script.

Next, let's create a script that demonstrates the usage of environment variables:

nano env_vars.sh

Add the following content:

#!/bin/bash
echo "User's home directory: $HOME"
echo "Current working directory: $PWD"
echo "User's shell: $SHELL"

Save, make executable, and run the script:

chmod +x env_vars.sh
./env_vars.sh

Expected output:

User's home directory: /home/labex
Current working directory: /home/labex/project
User's shell: /bin/bash

This example showcases the use of predefined environment variables like $HOME, $PWD, and $SHELL, which provide information about the user's environment and the system.

Implementing Conditional Logic and Loops in Shell Scripts

This section will teach you how to incorporate conditional statements and loops into your shell scripts, allowing for more complex and dynamic behavior.

Let's start by creating a script to demonstrate if-else conditional statements:

cd ~/project
nano if_else.sh

Add the following code:

#!/bin/bash
NUM=10
if [ $NUM -gt 0 ]; then
  echo "The number is positive."
else
  echo "The number is non-positive."
fi

Save the file, make it executable, and run it:

chmod +x if_else.sh
./if_else.sh

Expected output:

The number is positive.

This example uses an if-else statement to check if the value of the NUM variable is greater than 0, demonstrating how to execute different code blocks based on conditions.

Next, let's create a script that demonstrates the use of for loops:

nano for_loop.sh

Add the following content:

#!/bin/bash
for i in 1 2 3 4 5; do
  echo "Iteration $i"
done

Save the file, make it executable, and run it:

chmod +x for_loop.sh
./for_loop.sh

Expected output:

Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5

This example uses a for loop to iterate through a list of numbers from 1 to 5, showcasing how to repeat a block of code for a set number of times or for each element in a collection.

Shell Scripting Summary and Next Steps

This lab introduced the fundamentals of Linux shell scripting, starting with the creation of a "Hello, World!" script and progressing to working with shell variables. You then learned how to utilize shell variables and parameters, including using command-line arguments. These basic skills provide a foundational understanding of shell scripting, empowering you to automate various tasks and optimize systemadmin workflows.

The next step is to master implementing conditional statements and loops, enabling the creation of more sophisticated scripts capable of adapting to diverse scenarios and fulfilling complex systemadmin requirements. This will empower you to write more robust and flexible automation scripts.

400+ Linux Commands