Introduction

Linux is becoming more and more prominent in the networking industry. Many of us come from a mixed background and have varying levels of knowledge of Linux. I’ve been around Linux for a long time but really never got beyond the very most basic stuff. Looking back, I wish I had spent some more time learning Sed, Awk, regex, and Bash etc. I was doing some labs over at NRE Labs (great labs), and wanted to highlight some of the things I learned.

Appending To a File With Cat

Sometimes you want to append something quickly to a file or send several lines of text to a Linux command. That can be done using “here documents“.

First, look at this small configuration:

[email protected]:~/DevAsc$ cat config.txt 
interface GigabitEthernet0/1
 switchport mode access
 switchport access vlan 10
!
interface GigabitEthernet0/2
 switchport mode access
 switchport access vlan 10
!
interface GigabitEthernet0/3
 switchport mode access
 switchport access vlan 10
!

Now we want to append another interface to the end of this file. We can do that using cat:

[email protected]:~/DevAsc$ cat <<EOT >> config.txt 
> interface GigabitEthernet0/4
>  switchport mode access
>  switchport access vlan 10
> !
> EOT

After the cat command, the <<EOT is a label that means that we want to add things (append), as indicated by the >> pointing to config.txt, until we see EOT.

This is what the file looks like now:

[email protected]:~/DevAsc$ cat config.txt 
interface GigabitEthernet0/1
 switchport mode access
 switchport access vlan 10
!
interface GigabitEthernet0/2
 switchport mode access
 switchport access vlan 10
!
interface GigabitEthernet0/3
 switchport mode access
 switchport access vlan 10
!
interface GigabitEthernet0/4
 switchport mode access
 switchport access vlan 10
!

This saved us from opening an editor and doing the change there. Perhaps not a huge time saver, but yet somewhat more convenient.

Replacing Text With Sed

Sed is very useful when you want to search for a pattern in a file and replace it with something else. We have our existing configuration file:

[email protected]:~/DevAsc$ cat config.txt 
interface GigabitEthernet0/1
 switchport mode access
 switchport access vlan 10
!
interface GigabitEthernet0/2
 switchport mode access
 switchport access vlan 10
!
interface GigabitEthernet0/3
 switchport mode access
 switchport access vlan 10
!
interface GigabitEthernet0/4
 switchport mode access
 switchport access vlan 10
!

Now we have another device where the interface naming is slightly different. Instead of GigabitEthernet0/1, the name would be GigabitEthernet1/0/1. Sed can help us edit our file. First we need to understand what pattern to replace. We want to replace 0/ with 1/0/. We will use Sed to do this.

[email protected]:~/DevAsc$ sed 's/0\//1\/0\//g' config2.txt 
interface GigabitEthernet1/0/1
 switchport mode access
 switchport access vlan 10
!
interface GigabitEthernet1/0/2
 switchport mode access
 switchport access vlan 10
!
interface GigabitEthernet1/0/3
 switchport mode access
 switchport access vlan 10
!
interface GigabitEthernet1/0/4
 switchport mode access
 switchport access vlan 10
!

This looks good! So now let’s explain what’s going on.

We run Sed with s for substitute. The first / is where the string starts that we want to replace. Because we want to replace 0/, and / is a special character, we need to escape it with a \ first, ending up with /0\/.

Then there is another /, indicating the start of the string that should replace 0/. Once again, we need to escape the /, so we end up with /1\/0\/.

Finally, the final / ends the string that we are using to replace the previous string. We also supply g, for global, to replace all occurences.

Note that we haven’t actually edited the file yet:

[email protected]:~/DevAsc$ cat config2.txt 
interface GigabitEthernet0/1
 switchport mode access
 switchport access vlan 10
!
interface GigabitEthernet0/2
 switchport mode access
 switchport access vlan 10
!
interface GigabitEthernet0/3
 switchport mode access
 switchport access vlan 10
!
interface GigabitEthernet0/4
 switchport mode access
 switchport access vlan 10
!

This was a testrun to see if our regex worked out. If we want to actually change the file, we need to use the -i flag:

[email protected]:~/DevAsc$ sed -i 's/0\//1\/0\//g' config2.txt 
[email protected]:~/DevAsc$ cat config2.txt 
interface GigabitEthernet1/0/1
 switchport mode access
 switchport access vlan 10
!
interface GigabitEthernet1/0/2
 switchport mode access
 switchport access vlan 10
!
interface GigabitEthernet1/0/3
 switchport mode access
 switchport access vlan 10
!
interface GigabitEthernet1/0/4
 switchport mode access
 switchport access vlan 10
!

Suppressing Output From Scripts

There are times when you want to run a script and have no output at all. Linux has standard output (stdout) and standard error (stderr) where stdout is 1 and stderr is 2. These are actually file descriptors but that’s beyond the scope of this post.

For example, using echo, this is output to stdout:

[email protected]:~/DevAsc$ echo "hello world"
hello world

When you try to do something raising an error, such as trying to copy a file that is non-existant, that is output to stderr:

[email protected]:~/DevAsc$ cp config3.txt config4.txt
cp: cannot stat 'config3.txt': No such file or directory

Now, using an example from NRE Labs, you want to run this script silently:

git checkout master > /dev/null 2>&1
git merge change-124 master > /dev/null 2>&1
git checkout change-123 > /dev/null 2>&1

This script is running Git to merge a change, but it’s doing it silently. Let’s explain how this works.

The > indicates that stdout should be redirected to /dev/null, basically into oblivion. Note that when we don’t put a file descriptor, stdout is assumed, it could have written explicitly as 1>.

Then we have the 2>&1 part where we are redirecting 2, that is stderr, to the same place as 1 (stdout), as referenced by &1, namely /dev/null.

The effect is that both 1 and 2 are sent to /dev/null and the script is run silently.

Summary

A basic knowledge of Linux is useful in almost any IT role. Not everyone, including myself, have a strong background in Linux. In this post we learned some useful commands to edit files, replace text, and run scripts silently. See you in the next post.

DevAsc – Some Handy Linux Commands
Tagged on:                     

2 thoughts on “DevAsc – Some Handy Linux Commands

Leave a Reply

Your email address will not be published.