How to pass RHCE/RHCT/RHCSA exam – part 6

In order to pass RCHE/RCHT/RHCSA exam, you also need to know how to use input/output text redirection. As a first example, let’s say you want to insert some text in a file. This can be done very symple using “>” (more than) sign.

echo "this is just a test" > /tmp/some_test_file 

This will EMPTY the contents of that file if it existed before and will insert “this is just a test” in it. If that file did not exist it will create it and will insert the given string. Now let’s say we want to append a new entry in our hosts file, so we keep the content of the file, and we just add new lines at the bottom.

echo "192.168.1.105 jane" >> /etc/hosts

After this, we will be able to use ping jane in order to see if Jane’s computer is reachable or not. In order to save the firewall rules in a custom file, output redirection can be used:

iptables-save > /root/firewall.rules

I think I gave enough example on how to use output text redirection, it’s time to move on and see how input redirection works, for this the “<” (less than) sign is used. In order to restore some firewall rules that we saved before, we can use input redirection:

iptables-restore < /root/firewall.rules

This will read all the contents af the firewall.rules file and will send it to iptables. The input redirection is not used very often, it’s mostly used in scripts (it is very unlikely that you will use it to pass RHCE/RHCT/RHCSA exam), but I will list another example anyway.

#!/bin/bash

cat > /etc/nginx/conf.d/$1.conf << EOF

server{
listen 80;
server_name $1 www.$1;
root /home/$1/www/;
index index.php index.html;
}

EOF

Running the above script with the argument example.com will create a virtual host for nginx webserver:

./script.sh example.com

This script reads everything that follows from the next line until it reaches EOF line and it writes it in /etc/nginx/conf.d/example.com.conf I hope I made you understand how input/output text redirection works, but I strongly suggest you to practice a lot, otherwise you may end up deleting the contents of important files!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.