Question

I'm trying to use named pipes with Chef's shell_out! to prepend and append lines to the beginning and end of a file before piping it into another process. Ruby always encounters an error when I attempt to execute the code, but the error appears to be sh's not Ruby's. Below is a basic variation to my code that results in the same error along with an equivalent command that does not fail. Thoughts? Explanation? Fix?

[1] pry(main)> a = "cat <(echo foo) <(cat file) <(echo bar) | less"
=> "cat <(echo foo) <(cat file) <(echo bar) | less"
[2] pry(main)> `#{a}`
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `cat <(echo foo) <(cat file) <(echo bar) | less'
=> ""
[3] pry(main)> b = "echo 'foo\nfile contents\nbar' | less"
=> "echo 'foo\nfile contnents\nbar' | less"
[4] pry(main)> `#{b}`
=> "foo\nfile contents\nbar\n"
[5] pry(main)> exit
[vagrant@centos-65-developer ~]$ cat <(echo foo) <(cat file) <(echo bar) | less
[vagrant@centos-65-developer ~]$ cat <(echo foo) <(cat file) <(echo bar) | less
foo
file contents
bar
(END)
Was it helpful?

Solution

Aha, the <() format for named pipes is a feature of bash, not unix or sh. So in order to use this feature, the command must be interpreted by bash, and thus my code must be converted to:

[1] pry(main)> a = "bash -c 'cat <(echo foo) <(cat file) <(echo bar)' | less"
=> "bash -c 'cat <(echo foo) <(cat file) <(echo bar)' | less"
[2] pry(main)> `#{a}`
=> "foo\nfile contents\nbar\n"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top