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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35465851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:15:20  来源:igfitidea点击:

Define function in unix/linux command line (e.g. BASH)

linuxbashshellunix

提问by abalter

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:

有时我会为一项特定任务重复多次,但可能永远不会以完全相同的形式再次使用。它包括我从目录列表中粘贴的文件名。介于两者之间并创建一个 bash 脚本,我想也许我可以在命令行中创建一个单行函数,例如:

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

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).

我尝试了一些方法,例如使用 eval 和 using numresults=function...,但没有偶然发现正确的语法,并且到目前为止还没有在网上找到任何东西。(接下来的一切都只是关于 bash 函数的教程)。

回答by muru

Quoting my answerfor a similar question on Ask Ubuntu:

在 Ask Ubuntu 上引用对类似问题的回答

Functions in bashare 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.

中的函数bash本质上是命名的复合命令(或代码块)。来自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.

没有给出任何理由,这只是语法。

Try with a semicolon after wc -l:

尝试在 之后使用分号wc -l

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

回答by anubhava

Don't use ls | wc -las it may give you wrong results if file names have newlines in it. You can use this function instead:

不要使用,ls | wc -l因为如果文件名中有换行符,它可能会给你错误的结果。您可以改用此功能:

numresults() { find "" -mindepth 1 -printf '.' | wc -c; }

回答by chepner

You can also count files without find. Using arrays,

您还可以计算没有find. 使用数组,

numresults () { local files=( ""/* ); echo "${#files[@]}"; }

or using positional parameters

或使用位置参数

numresults () { set -- ""/*; echo "$#"; }

To match hidden files as well,

为了匹配隐藏文件,

numresults () { local files=( ""/* ""/.* ); echo $(("${#files[@]}" - 2)); }
numresults () { set -- ""/* ""/.*; echo $(("$#" - 2)); }

(Subtracting 2 from the result compensates for .and ...)

(从结果中减去 2 补偿...。)

回答by Boris

You can get a

你可以得到一个

bash: syntax error near unexpected token `('

error if you already have an aliaswith the same name as the function you're trying to define.

如果您已经有一个alias与您尝试定义的函数同名的,则会出错。

回答by thepasto

The easiest way maybe is echoing what you want to get back.

最简单的方法可能是呼应您想要返回的内容。

function myfunc()
{
    local  myresult='some value'
    echo "$myresult"
}

result=$(myfunc)   # or result=`myfunc`
echo $result

Anyway hereyou can find a good how-to for more advanced purposes

无论如何,在这里您可以找到用于更高级目的的好方法