echo "This is all a dream..." > dream.txt
echo "Wake up!" >> dream.txt
~$ echo "99 bottles of beer on the wall..." > beer.txt
~$ echo "Take one down,pass it around,98 bottles of beer on the wall..." >> beer.txt
~$ nano beer.txt
99 bottles of beer on the wall...
Take one down,pass it around,98 bottles of beer on the wall...
上面一个例子是从命令重定向到文件,现在我们看看怎么从文件重定向到命令,这牵扯到从文件的标准输出到命令的标准输入。
Linux排序命令sort将文件中的每行数据按字母顺序排列的。如果添加-r标识,将会反向排列。
~$ sort -r < beer.txt
Take one down,pass it around,98 bottles of beer on the wall...
99 bottles of beer on the wall...
~$ echo "Coffee is almost as good as beer,But I could never drink 99 bottles of
it" > coffee.txt
beer1.txt
beer2.txt
beer1.txt:beers are lots of beer
beer2.txt:I do not like beer
管道输出(Piping Output)字符 | 使得一个命令的标准输出可以通过管道传送到另一个命令的的标准输入,在链接命令时很有用。
tail -n 10 logs.txt | grep "Error"
import random
for i in range(10000):
print(random.randint(1,10))
echo -e "import random\nfor i in range(10000):\n print(random.randint(1,10))\n" > rand.py
python rand.py | grep 9
echo "All the beers are gone" >> beer.txt && cat beer.txt
~$ echo "beer is not a goo thing" >> 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...
beer is not a goo thing
echo ""Get out of here," said Neil Armstrong to the moon people." >> famous_quotes.txt
echo "\"Get out of here,\" said Neil Armstrong to the moon people." >> famous_quotes.txt
~$ echo "\"\"" > zm.txt
~$ cat zm.txt
""