BASH:如何对管道中的数字执行算术运算 [英] BASH: how to perform arithmetic on numbers in a pipe

查看:89
本文介绍了BASH:如何对管道中的数字执行算术运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在管道中获得一连串数字,并且想要在将它们传递到下一部分之前执行一些操作,但是我对如何使用它而不破坏管道感到有些困惑. /p>

例如

> echo "1 2 3 4 5" | some command | cat 
1 4 9 16 25
>

您对如何制作类似作品有任何想法吗?我要执行的实际操作只是在每个数字上加一个.

解决方案

echo 1 2 3 4 5|{
  read line; 
  for i in $line;
  do
    echo -n "$((i * i)) "; 
  done; 
  echo
}

{}创建一个分组.您可以为此创建一个脚本.

I am getting a stream of numbers in a pipe, and would like to perform some operations before passing them on to the next section, but I'm a little lost about how I would go about it without breaking the pipe.

for example

> echo "1 2 3 4 5" | some command | cat 
1 4 9 16 25
>

Would you have any ideas on how to make something like this work? The actual operation I want to perform is simply adding one to every number.

解决方案

echo 1 2 3 4 5|{
  read line; 
  for i in $line;
  do
    echo -n "$((i * i)) "; 
  done; 
  echo
}

The {} creates a grouping. You could instead create a script for that.

这篇关于BASH:如何对管道中的数字执行算术运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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