如何使用一个linux函数的输出并将其传递给另一个linux函数

时间:2022-07-21 16:55:01

Manual steps.

  1. I run command that lists data about my applications. There are over 1200 commands/jobs.
  2. 我运行命令列出有关我的应用程序的数据。有超过1200个命令/作业。

  3. One of the lines has a location where logs can be found. I want to run a "more" on this file location.
  4. 其中一条线路有一个可以找到日志的位置。我想在这个文件位置运行“更多”。

Is this possible with Unix scripting using one function or with one function calling another function?

使用一个函数或一个函数调用另一个函数的Unix脚本可以实现吗?

1 个解决方案

#1


3  

Yes!

There's quite a few ways to combine operations! There's pipes, that let you send the output of one command to another command. There's commands like grep (search), sed (for find/replace) and awk (computation, and more) to help you process the output (and send it to some other programs with pipes). There's operations like in-line evaluation ($(...)) to help you run a command and give it to another command as an argument.

有很多方法可以结合运营!有管道,可以将一个命令的输出发送到另一个命令。有像grep(搜索),sed(用于查找/替换)和awk(计算等)的命令可以帮助您处理输出(并使用管道将其发送到其他程序)。有一些操作,如内联评估($(...)),可帮助您运行命令并将其作为参数提供给另一个命令。

Concretely, lets say your program list-my-data produces output for your program. It looks like this (stuff after $ is what you type, rest is the output):

具体来说,假设您的程序list-my-data为您的程序生成输出。它看起来像这样($之后的东西是你输入的,休息是输出):

$ list-my-data  
line 1
line 2
line 3
log file: /path/to/a/file.log
line 5
....
line 100000

You can extract the line the contains the log file by piping (|) it to grep and telling grep what to search for:

您可以通过管道(|)将其包含到日志文件中以提取grep并告诉grep要搜索的内容:

$ list-my-data | grep 'log file:'
log file: /path/to/a/file.log

From this, you can extract the path to the log file by piping the output to sed and asking sed to remove the extra stuff in the line:

从这里,您可以通过将输出传递给sed并要求sed删除行中的额外内容来提取日志文件的路径:

$ list-my-data | grep 'log file:' | sed -e 's|log file: ||'
/path/to/a/file.log

You can now pass this line to more (or better, less) by evaluating it and passing it as an argument:

现在,您可以通过评估它并将其作为参数传递,将此行传递给更多(或更好,更少):

$ less $(list-my-data | grep 'log file:' | sed -e 's|log file: ||')

#1


3  

Yes!

There's quite a few ways to combine operations! There's pipes, that let you send the output of one command to another command. There's commands like grep (search), sed (for find/replace) and awk (computation, and more) to help you process the output (and send it to some other programs with pipes). There's operations like in-line evaluation ($(...)) to help you run a command and give it to another command as an argument.

有很多方法可以结合运营!有管道,可以将一个命令的输出发送到另一个命令。有像grep(搜索),sed(用于查找/替换)和awk(计算等)的命令可以帮助您处理输出(并使用管道将其发送到其他程序)。有一些操作,如内联评估($(...)),可帮助您运行命令并将其作为参数提供给另一个命令。

Concretely, lets say your program list-my-data produces output for your program. It looks like this (stuff after $ is what you type, rest is the output):

具体来说,假设您的程序list-my-data为您的程序生成输出。它看起来像这样($之后的东西是你输入的,休息是输出):

$ list-my-data  
line 1
line 2
line 3
log file: /path/to/a/file.log
line 5
....
line 100000

You can extract the line the contains the log file by piping (|) it to grep and telling grep what to search for:

您可以通过管道(|)将其包含到日志文件中以提取grep并告诉grep要搜索的内容:

$ list-my-data | grep 'log file:'
log file: /path/to/a/file.log

From this, you can extract the path to the log file by piping the output to sed and asking sed to remove the extra stuff in the line:

从这里,您可以通过将输出传递给sed并要求sed删除行中的额外内容来提取日志文件的路径:

$ list-my-data | grep 'log file:' | sed -e 's|log file: ||'
/path/to/a/file.log

You can now pass this line to more (or better, less) by evaluating it and passing it as an argument:

现在,您可以通过评估它并将其作为参数传递,将此行传递给更多(或更好,更少):

$ less $(list-my-data | grep 'log file:' | sed -e 's|log file: ||')