005 - Essential Linux Skills for DevOps: Understanding Pipes, Redirects, and Shell Scripting

Pipes
In Linux, when you need to execute multiple commands where each command relies on the output of the previous one, you can use pipes. The pipe symbol (|
) allows you to connect the output of one command directly into the input of another. This is particularly useful for processing data in a streamlined manner.
For example, if you want to search through your command history to find specific entries that include the word "sudo," you can use the following command:
history | grep sudo
Here's how it works:
The
history
command outputs a list of all the commands you've previously executed in the terminal.The pipe (
|
) takes this output and feeds it as input to the next command.The
grep sudo
command searches through the input it receives for lines containing the word "sudo."
As a result, this command sequence will display all the commands from your history that include "sudo." This method is efficient for filtering and finding specific information from a larger dataset.
Another example of using pipes is when you want to count the number of lines in a file that contain a specific word. Suppose you have a file named example.txt
and you want to count how many lines contain the word "error":
grep error example.txt | wc -l
In this example:
grep error example.txt
searches for the word "error" inexample.txt
and outputs the matching lines.The pipe (
|
) passes these lines to thewc -l
command.wc -l
counts the number of lines, effectively giving you the number of occurrences of "error" in the file.
Using pipes in Linux is a powerful way to combine simple commands to perform complex tasks efficiently.
Redirects
Redirects in Linux are similar to pipes in that they allow you to manage the flow of data between commands. However, while pipes are used to pass the output of one command directly into another, redirects are used to send the output to a file or another destination.
For instance, if you want to save the output of a command to a file, you can use the >
operator. Consider the following example:
history | grep sudo > sudo-commands.txt
In this command:
history
outputs a list of all the commands you've previously executed in the terminal.The pipe (
|
) passes this output togrep sudo
, which filters the list to include only the commands containing the word "sudo."The
>
operator redirects this filtered output to a file namedsudo-commands.txt
, effectively saving it.
If you want to append the output to an existing file instead of overwriting it, you can use the >>
operator:
history | grep sudo >> sudo-commands.txt
This command will add the filtered output to the end of sudo-commands.txt
without erasing its current contents.
Redirects are versatile and can be used in various scenarios. For example, you can redirect error messages to a separate file using 2>
:
command 2> error-log.txt
Here, 2>
redirects the standard error output of command
to error-log.txt
. This is useful for debugging and logging errors separately from standard output.
By mastering redirects, you can efficiently manage and organize the output of your Linux commands, making it easier to process and analyze data.
Shell Scripting
Imagine you have a series of commands to execute for a specific task, such as creating a new user, assigning permissions, or checking file permissions. Instead of repeatedly typing or copying and pasting these commands, you can streamline the process by writing a shell script. A shell script allows you to automate tasks by executing a sequence of commands in a single file. This approach is particularly beneficial if you have a background in programming, as shell scripting languages share similarities with other high-level programming languages.
Before diving into shell scripting, it's important to understand the terminology. You might encounter terms like "bash scripting," "shell script," or "zsh script." Bash and Zsh are both shell programs that provide enhanced scripting capabilities on top of the basic shell. Most Linux distributions come with Bash by default, but shell scripts can be executed on any Unix-based system.
Every shell script begins with a line called the "shebang," which specifies the script's interpreter. For example, a Bash script starts with #!/bin/bash
. The shebang tells the system which interpreter to use to execute the script. This is crucial because it ensures that the script runs in the intended environment, with the correct syntax and features.
Once the shebang is in place, you can write your script using familiar programming constructs such as conditionals, loops, and functions. You can also handle user input through parameters or input streams. Here's a simple example of a shell script that creates a new user and assigns a default password:
#!/bin/bash
# Check if the script is run as root mean id is not equal to 0 then re run it as sudo
if [ "$(id -u)" -ne 0 ]; then
echo "Please run as root"
exit 1
fi
# Create a new user
read -p "Enter the username: " username
useradd "$username"
# Set a default password
echo "Setting default password for $username"
echo "$username:password123" | chpasswd
echo "User $username created and password set."
In this example, the script checks if it's being run as the root user, prompts for a username, creates the user, and sets a default password. By mastering shell scripting, you can automate complex tasks, improve efficiency, and reduce the potential for human error.
Loops
Here's an other example of a shell script that uses loops to read user input from a stream and continuously sums the input until the user decides to quit by pressing 'q':
#!/bin/bash
# Initialize sum variable
sum=0
echo "Enter numbers to add to the sum. Press 'q' to quit."
# Loop to read user input
while true; do
read -p "Enter a number: " input
# Check if the user wants to quit
if [ "$input" = "q" ]; then
echo "Final sum: $sum"
break
fi
# Check if the input is a valid number
if [[ "$input" =~ ^-?[0-9]+$ ]]; then
sum=$((sum + input))
echo "Current sum: $sum"
else
echo "Invalid input. Please enter a number or 'q' to quit."
fi
done
In this script:
The
sum
variable is initialized to zero.A
while
loop is used to continuously prompt the user for input.If the user enters 'q', the script breaks out of the loop and displays the final sum.
If the input is a valid number, it is added to the
sum
, and the current sum is displayed.If the input is not a valid number, the script prompts the user to enter a valid number or 'q' to quit.
This example demonstrates how to use loops and conditionals in shell scripting to handle user input and perform calculations.
Function
Here's an example of a shell script that takes two numbers as parameters when the script is executed, passes these parameters to a sum function, and then echoes the result:
#!/bin/bash
# Function to calculate the sum of two numbers
sum() {
local num1=$1
local num2=$2
echo $((num1 + num2))
}
# Check if two parameters are provided when script execute
if [ "$#" -ne 2 ]; then
echo "Usage: $0 num1 num2"
exit 1
fi
# Call the sum function with the provided parameters
result=$(sum "$1" "$2")
# Output the result
echo "The sum of $1 and $2 is: $result"
In this script:
A function named
sum
is defined to calculate the sum of two numbers. It takes two arguments and echoes their sum.The script checks if exactly two parameters are provided. If not, it displays a usage message and exits.
The
sum
function is called with the two parameters passed to the script, and the result is stored in theresult
variable.The script then echoes the result, displaying the sum of the two numbers.
This example demonstrates how to use functions and parameters in shell scripting to perform calculations and output results.
I hope this article helps you understand the basics of Linux pipes, redirection, and shell scripting. Feel free to reach out to me if you have any questions.
Summary
Learn about essential Linux command line techniques, including using pipes to pass output between commands, redirects to manage data flow, and crafting shell scripts for automation. Master the power of command linking, output management, and scripting with examples of searching and filtering data, saving command outputs, and automating tasks with shell scripts such as user creation or sum calculations.
Subscribe to my newsletter
Read articles from Hamza Iqbal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Hamza Iqbal
Hamza Iqbal
Hi, Hamza Iqbal here. I'm a MERN Stack developer with 3+ years of experience. I'm a tech enthusiast who love to learn new skills and read tech related news. Currently, I'm learning DevOps.