linux - 在 unix/linux 命令行中定义函数(例如 BASH)

标签 linux bash shell unix

有时,我会为一项特定任务重复多次单行,但可能永远不会以完全相同的形式再次使用。它包括我从目录列表中粘贴的文件名。介于两者之间并创建一个 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). From man 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/

相关文章:

linux - shell 语法错误

linux - 复制到新文件夹后,如何仅将整个嵌套目录中的头文件复制到另一个保持相同层次结构的目录

java - 如何在 UNIX 中获取 Java 类的返回值

linux - 你能用Linux shell加载内存中的树结构吗?

java - Linux liveUSB 导致 init.d 脚本出错

linux - 使用循环解析ksh中的长参数和短参数

bash - FFmpeg 如何使用 bash 生成视频序列

c - 如何使用clone()让父进程和子进程同时运行?

linux - 如何将 "git add -all"限制为一次最多 100 个?

regex - 使用 sed 或 grep 在一行中计算正则表达式模式匹配?