在 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
Define function in unix/linux command line (e.g. BASH)
提问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:
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.
中的函数
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 -l
as 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 alias
with the same name as the function you're trying to define.
如果您已经有一个alias
与您尝试定义的函数同名的,则会出错。