管道输出到bash函数 [英] Pipe output to bash function

查看:134
本文介绍了管道输出到bash函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有简单的功能在bash脚本,我想管标准输出到它作为一个输入。

I have as simple function in a bash script and I would like to pipe stdout to it as an input.

jc_hms(){
  printf "$1"
}

我想以这种方式来使用它。

I'd like to use it in this manner.

var=`echo "teststring" | jc_hms`

当然,我用多余的功能呼应和printf简化问题,但你的想法。现在,我得到一个未找到的错误,我以为我的意思参数分隔是错误的(在$ 1的一部分)。有什么建议?

Of course I used redundant functions echo and printf to simplify the question, but you get the idea. Right now I get a "not found" error, which I assume means my parameter delimiting is wrong (the "$1" part). Any suggestions?

原本是用来像这样jc_hms功能:

Originally the jc_hms function was used like this:

echo `jc_hms "teststring"` > //dev/tts/0

但我想以存储在用于进一步处理的变量的结果首先,将其发送到串行端口之前

but I'd like to store the results in a variable for further processing first, before sending it to the serial port.

编辑:
因此,要澄清,我不是要打印的东西到串行端口,我想接口到我的bash功能宜|管性格,我想知道如果这是可能的。

So to clarify, I am NOT trying to print stuff to the serial port, I'd like to interface to my bash functions should the "|" pipe character, and I am wondering if this is possible.

编辑:好吧,这里的全部功能。

Alright, here's the full function.

jc_hms(){
  hr=$(($1 / 3600))
  min=$(($1 / 60))
  sec=$(($1 % 60))

  printf "$hs:%02d:%02d" $min $sec
}

我使用的功能,形成一个字符串,来这条线​​code的

I'm using the function to form a string which come this line of code

songplaytime=`echo $songtime | awk '{print S1 }'`
printstring="`jc_hms $songplaytime`"  #store resulting string in printstring

其中$ songtime是pssed为玩耍TOTALTIME用空格分隔字符串前$ P $。

Where $songtime is a string expressed as "playtime totaltime" delimited by a space.

我想我可以做到这一条线,管它AWK后

I wish I can just do this in one line, and pipe it after the awk

printstring=`echo $songtime | awk '{print S1 }' | jc_hms`

像这样

推荐答案

要回答你的问题的实际,当一个shell函数是在管道的接收端,标准输入是由函数内部执行的第一个命令读取。由于的printf 是你的函数的第一个也是唯一的命令,标准输入被忽略。有几种方法解决这个问题,包括使用内置读取标准输入到一个可以传递给的printf

To answer your actual question, when a shell function is on the receiving end of a pipe, standard input is read by the first command executed inside the function. Since printf is the first and only command in your function, standard input is ignored. There are several ways around that, including using the read built-in to read standard input into a variable which can be passed to printf:

jc_hms () {
    read foo
    hr=$(($1 / 3600))
    min=$(($1 / 60))
    sec=$(($1 % 60))
    printf "$hs:%02d:%02d" $min $sec
}

不过,因为你需要一个管道似乎取决于您认为需要使用 AWK ,让我提出以下选择:

printstring=$( jc_hms $songtime )

由于 songtime 由一个空格分隔的一对数字的,外壳上 songtime jc_hms 看到两个不同的参数。这需要在jc_hms的定义没有变化,并通过标准输入无需任何管道进入它。

Since songtime consists of a space-separated pair of numbers, the shell performs word-splitting on the value of songtime, and jc_hms sees two separate parameters. This requires no change in the definition of jc_hms, and no need to pipe anything into it via standard input.

如果您还有 jc_hms 不同的原因来读取标准输入,请告诉我们。

If you still have a different reason for jc_hms to read standard input, please let us know.

这篇关于管道输出到bash函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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