1

Say I have a set of piped commands in bash:

mysqldump mydatabase  |   head -1100   | tail -n 100   

What is the correct / best way to treat that as one command, so I can capture the output.

I tried enclosing the commands in backticks (which as I understand opens a new shell, which seems unnecessary), but got an error.

`mysqldump mydatabase  |   head -1100   | tail -n 100 ` > output.txt  


error mysqldump: Got errno 32 on write

Not sure if the error is related to the bash commands or the mysql (it worked before I added the backticks).

Anyway, I am more interested in how I can treat the piped commands as one / group them for capturing.

4

3 回答 3

2

左手命令的标准输出充当右手命令的标准输入,所以mysqldump mydatabase | head -1100 | tail -n 100 > output.txt应该可以工作。

如果你真的要“说清楚”,这应该可行。 ( mysqldump mydatabase | head -1100 | tail -n 100 ) > output.txt

于 2013-10-10T09:03:51.773 回答
2

行中的每个命令都接受一些输入并返回一个输出,因此您可以将其视为传递数据的链。

如果您希望结果显示在控制台中,您只需编写

mysqldump mydatabase | head -1100 | tail -n 100

否则要将其保存在文件中,您将输出重定向到文件,就像这样

mysqldump mydatabase | head -1100 | tail -n 100 > output.txt

有关重定向的更多信息

于 2013-10-10T09:06:44.143 回答
1

Instead of creating two pipes you can use sed to find sections of line numbers

    mysqldump mydatabase | sed -n '1000,1100p' > output.txt
于 2013-10-10T09:12:59.537 回答