Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在xargs中使用用户定义的bash函数_Bash_Xargs - Fatal编程技术网

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

在xargs中使用用户定义的bash函数,bash,xargs,Bash,Xargs,我的.bashrc中有一个自定义函数: function ord() { printf '%d' "'$1" } 如何使此函数与xargs一起使用 cat anyFile.txt | awk '{split($0,a,""); for (i=1; i<=100; i++) print a[i]}' | xargs -i ord {} xargs: ord: No such file or directory cat anyFile.txt | awk'{split($0,a,”);

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

function ord() {
  printf '%d' "'$1"
}
如何使此函数与xargs一起使用

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

cat anyFile.txt | awk'{split($0,a,”);对于(i=1;i首先,您的函数只使用1个参数,因此在此处使用xargs将只接受第一个参数。您需要将函数更改为以下内容:

ord() {
   printf '%d' "$@"
}
要让xargs使用bashrc中的函数,必须生成一个新的交互式shell。类似的操作可能会奏效:

awk '{split($0,a,""); for (i=1; i<=100; i++) print a[i]}' anyFile.txt | xargs bash -i -c 'ord $@' _

awk'{split($0,a,”);对于(i=1;i,在普通情况下,您可以使用
/usr/bin/printf

xargs -I {} /usr/bin/printf '%d' "'{}"

为什么会被否决?可能是UUOC?我不是所有awk解决方案的否决者,因为它无法正确转换。数字被打印为数字,而字母被转换为“0”。我希望将ASCII值转换为整数。或者您可以导出函数(
export-f
),而不使shell交互。
awk '{split($0,a,""); for (i=1; i<=100; i++) printf("%d",a[i])}' anyFile.txt
xargs -I {} /usr/bin/printf '%d' "'{}"