Introduction to Linux Shell Scripting
This lab provides a hands-on introduction to shell scripting in Linux using the script
command. Master the essentials of variables, command substitution, conditional logic, and loops. These are crucial skills for any systemadmin aiming to automate tasks and boost efficiency within a Linux environment. This comprehensive guide delivers practical examples to kickstart your shell scripting journey and unlock this potent tool.
Crafting and Running Your First Shell Script
This section guides you through creating and executing a basic shell script. Shell scripts are plain text files containing a sequence of commands that the shell (your command-line interface) can run.
Let's begin by making a new file for our shell script:
nano ~/project/hello.sh
Within the nano editor, insert the following code into the file:
#!/bin/bash
echo "Hello, World!"
The initial line, #!/bin/bash
, often referred to as the "shebang," instructs the operating system to use the Bash shell for script execution.
To grant the script execute permissions, use the following command:
chmod +x ~/project/hello.sh
Now, execute your script with this command:
~/project/hello.sh
Expected output:
Hello, World!
The ~/project/hello.sh
command tells the system to run the hello.sh
script residing in the ~/project
directory.
Leveraging Variables and Command Substitution in Scripts
Here, you'll discover how to effectively use variables and command substitution within shell scripts. Variables enable you to store and reuse values, while command substitution allows you to embed the results of commands directly into your scripts.
Let's create another new file for our shell script:
nano ~/project/variables.sh
Inside the nano editor, add the code below:
#!/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 your changes and make the file executable:
chmod +x ~/project/variables.sh
Execute the script now:
~/project/variables.sh
Anticipated output:
Hello, John Doe!
The current date is: Fri Apr 14 12:34:56 UTC 2023
This script demonstrates assigning a value to the name
variable using the =
assignment operator. Then it displays the variable's value using $name
within an echo
statement.
The script also demonstrates command substitution. The $(date)
command is executed, and its output is stored in the current_date
variable for later use in an echo
statement.
Implementing Conditionals and Loops in Shell Scripts
This section explores the use of conditional statements and loops within shell scripts. Conditional statements allow you to execute different code blocks based on specified conditions, while loops enable you to repeatedly execute code.
Let's create a new file for our shell script:
nano ~/project/conditional_loops.sh
In the nano editor, insert the following code:
#!/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 grant execute permissions:
chmod +x ~/project/conditional_loops.sh
Execute the script now:
~/project/conditional_loops.sh
Expected output:
You are a minor.
Counting from 1 to 5:
1
2
3
4
5
The script showcases the use of an if
statement to determine if the age
variable is greater than or equal to 18 and print corresponding messages.
It also demonstrates the use of a for
loop to iterate from 1 to 5, printing each number in the sequence.
Shell Scripting Summary and Next Steps
This lab provided a foundation for writing and executing basic shell scripts, including how to utilize variables and command substitution. You started by creating and running a script that prints "Hello, World!". You then learned how to store and reuse values using variables and incorporate the output of commands into your scripts using command substitution. These essential concepts are critical for building more complex shell scripts for automation on your Linux system as a systemadmin.