有时,我会为一项特定任务重复多次单行,但可能永远不会以完全相同的形式再次使用。它包括我从目录列表中粘贴的文件名。介于两者之间并创建一个 bash 脚本,我想也许我可以在命令行中创建一个单行函数,例如:
numresults(){ ls "$1"/RealignerTargetCreator | wc -l }
我已经尝试了一些方法,例如使用 eval、使用 numresults=function...
,但还没有找到正确的语法,到目前为止还没有在网上找到任何东西. (接下来的一切都只是关于 bash 函数的教程)。
最佳答案
引用 my answer对于 Ask Ubuntu 上的类似问题:
Functions in
bash
are essentially named compound commands (or code blocks). Fromman bash
:Compound Commands A compound command is one of the following: ... { list; } list is simply executed in the current shell environment. list must be terminated with a newline or semicolon. This is known as a group command. ... Shell Function Definitions A shell function is an object that is called like a simple command and executes a compound command with a new set of positional parameters. ... [C]ommand is usually a list of commands between { and }, but may be any command listed under Compound Commands above.
There's no reason given, it's just the syntax.
尝试在 wc -l
后加一个分号:
numresults(){ ls "$1"/RealignerTargetCreator | wc -l; }
关于linux - 在 unix/linux 命令行中定义函数(例如 BASH),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35465851/