12

Some programs needs their files to be seekable, for example objdump does.

$ objdump -D -b binary -m i8086 <(echo 0xea 0x5b 0xe0 0x00 0xf0|xxd -r -p)
objdump: Warning: '/proc/self/fd/11' is not an ordinary file

It would be convenient to have process substitution use temporary files.

I can see in the man page that bash can fallback to temporary files with process substitution, but can I explicitly ask him to use temporary files?

Like zsh's =().

$ objdump -D -b binary -m i8086 =(echo 0xea 0x5b 0xe0 0x00 0xf0|xxd -r -p)

/tmp/zsh1u1Nrw:     file format binary


Disassembly of section .data:

00000000 <.data>:
   0:   ea 5b e0 00 f0          ljmp   $0xf000,$0xe05b
Elazar Leibovich
  • 3,231
  • 5
  • 27
  • 28
  • I doubt it, but you can always use mktemp. – Wildcard Mar 14 '16 at 07:48
  • 1
    Maybe you should try compile bash with HAVE_DEV_FD set to 0. – cuonglm Mar 14 '16 at 07:54
  • @Wildcard I don't see how mktemp helps. It won't remove the file automatically. I might as well just do foo >/tmp/xxx;cmd /tmp/xx;rm /tmp/xxx It's not about bash programming, for which there are better solutiosn, but about one off one-line scripts. – Elazar Leibovich Mar 14 '16 at 08:16
  • @cuonglm that would make bash always use temporary files, which is not what I want (not to mention that home baked bash is not a great idea). – Elazar Leibovich Mar 14 '16 at 08:16
  • 7
    You could use a seekable here string instead: objdump -D -b binary -m i8086 /dev/stdin <<<$(echo 0xea 0x5b 0xe0 0x00 0xf0|xxd -r -p) – meuh Mar 14 '16 at 08:40
  • @Wildcard I might be, but I can't see why is it related to my question. Would you enlighten me? As I said, one can already do cmd >x;cmd2 x;rm x, and I don't see the difference – Elazar Leibovich Mar 14 '16 at 11:02
  • 2
    Oops. The here string version silently drops the null char! – meuh Mar 14 '16 at 13:32
  • @ElazarLeibovich for one thing, as mikeserv pointed out in that answer: "Both of the above versions ensure that they destroy the filesystem link to the pipes they create/use before ever making use of them. This means there is no cleanup required after the fact, and, more importantly, their streams are only available to the processes which initially open them - and so their filesystem links cannot be used as a means to snoop/hijack your logging activity. To leave their fs-links in the filesystem is a potential security hole." – Wildcard Mar 14 '16 at 17:20
  • 1
    @Wildcard using fifo is not possible with objdump, that's the whole point of the question. Otherwise <() was good enough. – Elazar Leibovich Mar 14 '16 at 21:40
  • I did get that. I am quite certain you could modify the code I linked to to make it use touch instead of mkfifo. (Just doing that change by itself would probably be enough.) – Wildcard Mar 14 '16 at 21:55
  • @Wildcard as I previously said. This is not a programming problem. I already said that cmd >x;objdump x;rm x is the best approach, mktemp is not really needed. I wanted a solution that is convenient for one off scripts. – Elazar Leibovich Mar 15 '16 at 08:02

1 Answers1

3

Based on meuh's comment; apparently bash here-strings can be abused as temporary files, try this:

( echo 0xea 0x5b 0xe0 0x00 0xf0 | 
  xxd -r -p >/dev/fd/9; objdump -D -b binary -m i8086 /dev/fd/9) 9<<<''
mr.spuratic
  • 9,901
  • 1
    The trick is that here documents and here strings use temporary files under the hood (replace objdump ... with stat /dev/fd/9 to see, it won't be a problematic FIFO or a pipe, but a symlink to a file in /tmp or $TMPDIR). – mr.spuratic Sep 21 '16 at 10:43
  • I used your answer's method for my objdump situation. Interestingly, it only worked for me with gcc -o /dev/fd/9 [ . . . ], not with gcc >&9 -o - [ . . . ]. I was going to write an answer (not to the objdump situation, but for a non-null-sensitive situation) that used a here document with a command substitution, but then I saw that you had already mentioned a here string in the question comments, which is even better. – clacke Oct 04 '16 at 09:41
  • 1
    In bash 5.2 this does not work any more for small heredoc strings, as bash may internally use a pipe, instead of a file. See function here_document_to_fd. – spawn Dec 19 '22 at 21:26