Unix map function

I have an array of $dates values ​​that I convert:

 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 that I can connect it to something else without writing it to a file on disk?

+7
source share
6 answers

Create a function:

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

Then you can, for example, send the output to a standard error:

 echo `foo $dates` >&2 
+6
source

Since you say "conversion", I assume that you want you to do the loop output 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) 
+11
source

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

 for ... do ... done | ... 
+3
source

You can write it in FIFO - "named pipe", which looks like a file.

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

+2
source

Edit, did not see the whole file thing:

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

If you use bash, you can 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 

Then you can echo / pass this array to another program. Note that arrays have a bash value, which means that it is not portable (well, besides systems with bash).

+1
source

All Articles