bash unix 映射函数

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

unix map function

bashunixmapsh

提问by pokerface

I have an array of values $datesthat I'm transforming:

我有一个$dates要转换的值数组:

for i in $dates
do
  date -d "1970-01-01 $i sec UTC" '+%a_%D' 
done

Is there a way to save the result of this operation so I can pipe it to something else without writing it to a file on disk?

有没有办法保存此操作的结果,以便我可以将其通过管道传输到其他内容而无需将其写入磁盘上的文件?

采纳答案by Oswald

Create a function:

创建一个函数:

foo () {
        for i in $@
        do
                date -d "1970-01-01 $i sec UTC" '+%a_%D'
        done
}

Then you can e.g. send the output to standard error:

然后你可以例如将输出发送到标准错误:

echo `foo $dates` >&2

回答by Paused until further notice.

Since you say "transforming" I'm assuming you mean that you want to capture the output of the loop in a variable. You can even replace the contents of your $datesvariable.

既然你说“转换”,我假设你的意思是你想在一个变量中捕获循环的输出。您甚至可以替换$dates变量的内容。

dates=$(for i in "$dates"; do date -d "@$i" '+%a_%D'; done)

回答by Ignacio Vazquez-Abrams

Your question is a bit vague, but the following may work:

你的问题有点含糊,但以下可能有效:

for ...
do
 ...
done | ...

回答by Jé Queue

Edit, didn't see the whole file thing:

编辑,没有看到整个文件:

for i in $dates ; do
    date -d "1970-01-01 $i sec UTC" '+%a_%D'
done |foo

回答by Tim Post

If using bash, you could use an array:

如果使用 bash,则可以使用数组:

q=0
for i in $dates
do
  DATEARRAY[q]="$(date -d "1970-01-01 $i sec UTC" '+%a_%D')"
  let "q += 1"
done

You can then echo / pipe that array to another program. Note that arrays are bash specific, which means this isn't a portable (well, beyond systems that have bash) solution.

然后,您可以将该数组回显/管道到另一个程序。请注意,数组是特定于 bash 的,这意味着这不是一个可移植的(好吧,超出具有 bash 的系统)解决方案。

回答by EmeryBerger

You could write it to a FIFO -- a "named pipe" that looks like a file.

您可以将其写入 FIFO——一个看起来像文件的“命名管道”。

Wikipedia has a decent example of its use: http://en.wikipedia.org/wiki/Named_pipe

维基百科有一个很好的例子:http: //en.wikipedia.org/wiki/Named_pipe