在 xargs 中使用用户定义的 bash 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11232782/
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 02:37:30  来源:igfitidea点击:

using a user defined bash function in xargs

bashxargs

提问by lonestar21

I have this self-defined function in my .bashrc :

我的 .bashrc 中有这个自定义函数:

function ord() {
  printf '%d' "'"
}

How do I get this function to be used with xargs ?:

我如何让这个函数与 xargs 一起使用?:

cat anyFile.txt | awk '{split(
ord() {
   printf '%d' "$@"
}
,a,""); for (i=1; i<=100; i++) print a[i]}' | xargs -i ord {} xargs: ord: No such file or directory

采纳答案by jordanm

First, your function only uses 1 argument, so using xargs here will only take the first arg. You need to change the function to the following:

首先,您的函数仅使用 1 个参数,因此在此处使用 xargs 只会采用第一个参数。您需要将函数更改为以下内容:

awk '{split(
arr=(awk '{split(
awk '{split(
xargs -I {} /usr/bin/printf '%d' "'{}"
,a,""); for (i=1; i<=100; i++) printf("%d",a[i])}' anyFile.txt
,a,""); for (i=1; i<=100; i++) print a[i]}' anyFile.txt) ord "${arr[@]}"
,a,""); for (i=1; i<=100; i++) print a[i]}' anyFile.txt | xargs bash -i -c 'ord $@' _

To get xargs to use a function from your bashrc, you must spawn a new interactive shell. Something like this may work:

要让 xargs 使用 bashrc 中的函数,您必须生成一个新的交互式 shell。像这样的事情可能会奏效:

##代码##

Since you are already depending on word splitting, you could just store awk's output in an array.

由于您已经依赖于分词,因此您可以将 awk 的输出存储在一个数组中。

##代码##

Or, you can use awk's printf:

或者,您可以使用 awk 的 printf:

##代码##

回答by Paused until further notice.

In the trivial case, you can use /usr/bin/printf:

在微不足道的情况下,您可以使用/usr/bin/printf

##代码##