在UNIX/Linux命令行中定义函数(例如BASH) [英] Define function in unix/linux command line (e.g. BASH)

查看:211
本文介绍了在UNIX/Linux命令行中定义函数(例如BASH)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候我会重复执行一次特定任务,但是可能永远不会以完全相同的形式重复使用.它包含我要从目录列表中粘贴的文件名.在这之间的某个地方并创建一个bash脚本,我想也许我可以在命令行上创建一个单行函数,例如:

Sometimes I have a one-liner that I am repeating many times for a particular task, but will likely never use again in the exact same form. It includes a file name that I am pasting in from a directory listing. Somewhere in between and creating a bash script I thought maybe I could just create a one-liner function at the command line like:

numresults(){ ls "$1"/RealignerTargetCreator | wc -l }

我已经尝试了一些方法,例如使用eval,使用numresults=function...,但是并没有迷失在正确的语法上,并且到目前为止还没有在网上找到任何东西. (即将出现的所有内容只是有关bash函数的教程.)

I've tried a few things like using eval, using numresults=function..., but haven't stumbled on the right syntax, and haven't found anything on the web so far. (Everything coming up is just tutorials on bash functions).

推荐答案

引用我的答案,以询问关于Ask的类似问题Ubuntu:

Quoting my answer for a similar question on Ask Ubuntu:

bash中的功能本质上是复合命令(或代码) 块).来自 man bash :

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; }

这篇关于在UNIX/Linux命令行中定义函数(例如BASH)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆