The pipe character, |
, allows you to send thestandard output from one command to thestandard input of another command. This can be very useful for chaining together commands.
For example, let's say we had a file calledlogs.txt
with 100000
lines. We only want to search the last 10
lines for the string Error
. We can use the tail -n 10 logs.txt
to get the last 10
lines of logs.txt
. We can then use the pipe character to chain it with a grep
command to perform the search:
tail -n 10 logs.txt | grep "Error"
The above command will search the last 10
lines of logs.txt
for the string Error
.
We can also pipe the output of a Python script. Let's say we had this script called rand.py
:
import random
for i in range(10000):
print(random.randint(1,10))
The above script will use the random library to generate a sequence of random integers, ranging in value from 0
to 10
, and will print them to the standard output.
This command will run the script, and search each line of output to see if a 9
occurs:
python rand.py | grep 9
Any lines that output a 9
will be printed.
Instructions
- Make a Python script that generates output.
- Use pipes and
grep
to search the output of the script
~$ echo -e "import random\nfor i in range(10000):\n print(random.randint(1,10))\n" > rand.py
~$ python rand.py | grep 9
#########################################################
7: Chaining Commands
If we want to run two commands sequentially, but not pass output between them, we can use&&
to chain them. Let's say we want to add some content to a file, then print the whole file:
echo "All the beers are gone" >> beer.txt &&
cat beer.txt
This will first add the string All the beers are gone
to the file beer.txt
, then print the entire contents of beer.txt
. The &&
only runs the second command if the first command doesn't return an error. If we instead tried this:
ec "All the beers are gone" >> beer.txt &&
cat beer.txt
We'd get an error, and nothing would be printed, because we used the command ec
instead ofecho
.
Instructions
- Add a line to
beer.txt
, and then print the contents of the file withcat
/home/dq$ echo "All the beers are gone" >> beer.txt && cat beer.txt
99 bottles of beer on the wall...
Take one down, pass it around, 98 bottles of beer on the wall...
All the beers are gone
######################################################
8: Escaping Characters
There are quite a few special characters that bash uses. A full list can be found here. When you use these characters in a string or a command, and you don't want them to have a special effect, you may have to escape them.
Escaping tells the shell to not treat the character as special, but to treat it as a plain character instead. Here's an example:
echo ""Get out of here," said Neil Armstrong
to the moon people." >> famous_quotes
.txt
The above command won't work as we intend because the quotes inside the string will be treated as special. But what we want to do is add the quotes into the file.
We use a backslash (\
) as an escape character -- if you add a backslash before a special character, the special character is treated like plain text.
echo "\"Get out of here,\" said Neil
Armstrong to the moon people." >>
famous_quotes.txt
The command above has the double quotes escaped with a backslash, so it will work as we intend.
Instructions
- Use the
echo
command to add a double quote character into a file
~$ echo "\"Get out of here,\" said Neil Armstrong to the moon people." >> famous_quotes.txt