6: Piping Output(Piping and redirecting output )

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 10lines 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 with cat

 

/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

转载于:https://my.oschina.net/Bettyty/blog/747163

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值