Linux For DevOps (Part-4)

Bhanuprathap Reddy
5 min readOct 28, 2021

Piping and redirecting

As part of this DevOps series in part-3, we have discussed Linux commands, file permissions, and file systems. In this part, we are going to discuss piping, redirecting, and some of the of the basics of shell scripting.

Contents

  1. Piping (|)
  2. Redirecting(>)
  3. Basic bash scripting

Piping(|)

In Linux, the output of the previous command is input of the next command. It is denoted with “|” symbol. For instance, if we use the history command, we will get lots of commands, as shown in the below image.

history

we got so many commands that we had never used before. Now, if we want to see only sudo commands, then we need to filter the output.

  • grep: It is a command used to filter the output.
history | grep sudo

If we want to see only sudo apt commands, then we need to use double quotes, as shown in the below image.

history | grep “sudo apt”
  • less: This command will be used to see less input. For instance, if we have lots of output, like history, we can use this command. Inside this, we have sub-commands: to go back, we need to use b; to quit, we need to use q, and to move forward, use the space button on your keyboard. It won’t display on everything on your keyboard; it has a different interface.
less coomand

That’s how you can use less command.

Redirecting(>)

In general, redirecting is a diversion. For instance, if you log in to your Facebook account, that will redirect you to your home page, right! Here is also the same concept but here we are redirecting output into a file by using the “>” symbol.

For instance, if we want to see what process is running on our system, then we use the ps command, right? for seeing all the processes, then we use ps-aux

ps -aux

Now we want to save the output into a file, then ps -aux > file_name

ps -aux > fileName

Now, we want only the processes running from the user, and then we need to filter the output by using ps -aux | grep userName > file

ps -aux | grep bhnau > bhanu.txt

Now we want to include root running processes in that file, we need to use >> this symbol. for that, you need to run ps -aux | grep root >> file.txt

ps -aux | grep root >> file.txt

That’s how you can redirect the output into a file. There are a lot of techniques to redirect the traffic; I can’t explain all the commands! that’s why I shared the notes on LinkedIn

Basic Bash Scripting

There are different types of shells in Linux. Generally, people will use bash, zsh, and shell.

Shell is a program that interprets and executes various commands that we type in the terminal. The file type is sh, which means Bourne shell. The location of the shell is /bin/sh

Bash, which means bourne again shell. It is an advanced version of the shell. Nowadays, it is the default shell for Unix-like systems. The location of bash is /bin/bash. I just filter by using sh.

Shebang

shebang

As you can see in that file, in the starting #!/bin/bash. That will tell the kernel its bash script. Why is it called shebang? because of the first two characters, it's called shebang.

# = In musical notation it’s called “Sharp”

! = It’s also called “bang”

shebang became a short form of sharp-bang.

Variables: to store some data so we can reference it multiple times later. we can also store command output into a file by using file=$(ls filename)

If and If else: In general and every programming language, it’s common. If this is a condition, then do this; if not, do this; if not, exit.

if [condition]
then
statement
else
statement
fi

example of if-else.

In the above script, I just wrote a simple config file.

  • First, I cleared the screen
  • Then I used some text by using the echo command
  • Then I used a variable file_name
  • Next, I used the if statement in that -d is for a directory If the directory config exits, then read the files and I’m defining a variable config_files.
  • Otherwise, make a directory.
  • Then exit
  • Lastly, it displays some text by using the echo command.

That’s all bout this part. I know I haven’t covered all the topics in a bash script, I shared a post on LinkedIn about bash scripting, please refer to those notes.

Don’t forget to follow me on Medium😊😊

Thank you guys for reading this module. If you find any mistakes let me know on LinkedIn or else let me know in the comment section. Don’t forget to share the Feedback👏👏

About me, My name is Bhanuprathap Reddy. I’m a cybersecurity researcher, CTF Player, Student. I’m having certifications like CompTIA Security+ and Azure Fundamentals Certification etc., You can connect with me on LinkedIn and Twitter🤗🤗

If you would like to buy me a coffee ☕ that will be appreciated. https://www.buymeacoffee.com/bhanuprathap

Thanks again for you’re time.🤩🤩

--

--