开发者

unix map function

开发者 https://www.devze.com 2023-02-06 07:14 出处:网络
I have an array of values $dates that I\'m transforming: for i in $dates do date -d \"1970-01-01 $i sec UTC\" \'+%a_%D\'

I have an array of values $dates that I'm transforming:

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?


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 $dates variable.

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


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


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

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


If using bash, you could use an array:

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.


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

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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消