Linux script Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to write and execute simple shell scripts using the Linux script command. You will explore the use of variables, command substitution, conditional statements, and loops within these scripts, which are essential for automating tasks and enhancing your productivity in a Linux environment. The lab covers the fundamentals of shell scripting, providing practical examples to help you get started with this powerful tool.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") linux/VersionControlandTextEditorsGroup -.-> linux/nano("`Simple Text Editing`") linux/SystemInformationandMonitoringGroup -.-> linux/date("`Date/Time Displaying`") subgraph Lab Skills linux/echo -.-> lab-422903{{"`Linux script Command with Practical Examples`"}} linux/chmod -.-> lab-422903{{"`Linux script Command with Practical Examples`"}} linux/nano -.-> lab-422903{{"`Linux script Command with Practical Examples`"}} linux/date -.-> lab-422903{{"`Linux script Command with Practical Examples`"}} end

Write and Execute a Simple Shell Script

In this step, you will learn how to write and execute a simple shell script. Shell scripts are text files that contain a series of commands that can be executed by the shell (the command-line interface).

First, let's create a new file for our shell script:

nano ~/project/hello.sh

In the nano editor, add the following content to the file:

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

The first line #!/bin/bash is called the "shebang" and tells the system to use the Bash shell to execute the script.

To make the script executable, run the following command:

chmod +x ~/project/hello.sh

Now, you can execute the script using the following command:

~/project/hello.sh

Example output:

Hello, World!

The ~/project/hello.sh command runs the hello.sh script located in the ~/project directory.

Use Variables and Command Substitution in Shell Scripts

In this step, you will learn how to use variables and command substitution in shell scripts. Variables allow you to store and reuse values, while command substitution enables you to incorporate the output of a command into your script.

First, let's create a new file for our shell script:

nano ~/project/variables.sh

In the nano editor, add the following content to the file:

#!/bin/bash

## Assign a value to a variable
name="John Doe"

## Use the variable in an echo statement
echo "Hello, $name!"

## Perform command substitution
current_date=$(date)
echo "The current date is: $current_date"

Save the file and make it executable:

chmod +x ~/project/variables.sh

Now, you can execute the script:

~/project/variables.sh

Example output:

Hello, John Doe!
The current date is: Fri Apr 14 12:34:56 UTC 2023

In the script, we first assign a value to the name variable using the assignment operator =. We then use the variable in an echo statement by prefixing it with a $.

The second part of the script demonstrates command substitution. The $(date) command is executed, and its output is stored in the current_date variable. We then use this variable in another echo statement.

Implement Conditional Statements and Loops in Shell Scripts

In this step, you will learn how to use conditional statements and loops in shell scripts. Conditional statements allow you to execute different code based on certain conditions, while loops enable you to repeat a block of code multiple times.

Let's create a new file for our shell script:

nano ~/project/conditional_loops.sh

In the nano editor, add the following content to the file:

#!/bin/bash

## Conditional statement
age=18
if [ $age -ge 18 ]; then
  echo "You are an adult."
else
  echo "You are a minor."
fi

## Loop
echo "Counting from 1 to 5:"
for i in 1 2 3 4 5; do
  echo "$i"
done

Save the file and make it executable:

chmod +x ~/project/conditional_loops.sh

Now, you can execute the script:

~/project/conditional_loops.sh

Example output:

You are a minor.
Counting from 1 to 5:
1
2
3
4
5

In the first part of the script, we use an if statement to check if the age variable is greater than or equal to 18. Based on the result, we print different messages.

In the second part, we use a for loop to iterate from 1 to 5, printing each number.

Summary

In this lab, you learned how to write and execute a simple shell script, as well as how to use variables and command substitution in shell scripts. You started by creating a shell script that prints "Hello, World!" and made it executable. Then, you explored using variables to store and reuse values, and incorporated the output of commands into your script using command substitution. These fundamental concepts provide a solid foundation for building more complex shell scripts to automate various tasks on your Linux system.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like