1

I was trying to use php inside bash script, but I came across several problems. My first try failed with error about bash not being able to find end of here-document. The minimal version that still triggered the error is as follows (UPD Q: Why bash can't see end of here-document marker?):

$ type 1.sh
#!/bin/bash -eu
cat <(cat <<SCRIPT
{
SCRIPT)
$ ./1.sh
./1.sh: line 7: warning: here-document at line 5 delimited by end-of-file (wanted `SCRIPT')
{

UPD I've created separate questions for the rest of the post.

"Well, I can avoid using braces then," I decided. But it turned out the first symbol is cut off for some reason (UPD Q: Who and for what reason cuts off first symbol of the scripts? Has it something to do with a BOM?):

$ type 2.sh
#!/bin/bash -eu
php <(cat <<'SCRIPT'
<?php var_dump($_SERVER['argv']);
SCRIPT)
$ ./2.sh
?php var_dump($_SERVER['argv']);

Then I tried to find out who is at fault (UPD Q: How to open process substituted file from php?):

$ type 3.sh
#!/bin/bash -eu
php -r 'var_dump(file_get_contents($_SERVER["argv"][1]));' -- <(cat <<'SCRIPT'
<?php var_dump($_SERVER['argv']);
SCRIPT)
$ ./3.sh
PHP Warning:  file_get_contents(/dev/fd/63): failed to open stream: No such file or directory in Command line code on line 1

Warning: file_get_contents(/dev/fd/63): failed to open stream: No such file or directory in Command line code on line 1
bool(false)

The first symbol is probably cut off by php:

$ type 4.sh
#!/bin/bash -eu
php <(cat <<SCRIPT
12
SCRIPT)
$ ./4.sh
2
$ type 5.sh
#!/bin/bash -eu
cat <(cat <<SCRIPT
12
SCRIPT)
$ ./5.sh
12

Well, I've come up with simpler solution:

$ type 6.sh
#!/bin/bash -eu
cat | php -- 1 <<'SCRIPT'
<?php var_dump($_SERVER['argv']);
SCRIPT
$ ./6.sh
array(2) {
  [0]=>
  string(1) "-"
  [1]=>
  string(1) "1"
}

But what's up with all that madness?

I run the tests on Debian (php-5.4.14, bash-4.1.5) and Arch Linux (php-5.4.12, bash-4.2.42).

UPD I've explicitly stated all the questions, see above.

x-yuri
  • 3,373
  • Can you re-phrase the question(s)? There's a lot going on, but I suspect that part of your problem is making too many assumptions. For example: "Thr first symbol is probably cut off by php". Sure, that seems like what your 5.sh shows, but why would the PHP interpreter cut off a '<'? It doesn't cut off other leading characters. Also, you're missing the '?>' after the PHP code, in 2.sh and others. –  May 02 '13 at 00:57
  • I've stated all the questions explicitly, do take a look please. 4.sh and 5.sh shows that php cuts off the first symbol of the script no matter what it is. Also, it is preferable to omit the PHP closing tag at the end of the file – x-yuri May 02 '13 at 02:56
  • "If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file." You be the judge if your file is pure PHP code. –  May 02 '13 at 13:07
  • Let's put it this way: if a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file and if it's not, it's okay to do that. Agree? ;) – x-yuri May 02 '13 at 14:04
  • Is there a reason you don't simply use #!/usr/bin/php as the shebang in those files and rename them away from .sh, instead of these shenanigans? – Josip Rodin May 27 '16 at 19:53
  • Sure, the script was supposed to be written partially in bash, partially in php. I don't remember what exactly it was supposed to do, but I believe, both of those has their weak and strong parts, so using them alongside make sense occasionally. – x-yuri May 28 '16 at 14:06

1 Answers1

3
cat <(cat <<SCRIPT
{
SCRIPT)

Your here document should end with SCRIPT, but you wrote SCRIPT). The shell first needs to determine where the here-document ends, before it can start looking for the closing parenthesis for the <(…) construct. Put the closing parenthesis on a separate line.

Note that with an unquoted here document marker, the characters \`$ are interpreted inside the here document: the shell performs variable and command substitutions. To avoid this, quote the marker.

cat <(cat <<'SCRIPT'
{
SCRIPT
)
  • Well, if here-document contains no braces, bash finds end of here-document marker all right, even if it's followed with process substitution's closing parenthesis. But anyway placing closing parenthesis on a separate line seems like a good idea, thanks. I wonder about the other problems... – x-yuri May 02 '13 at 00:17
  • @x-yuri What other problems? If you fix these two errors, what else is wrong? – Gilles 'SO- stop being evil' May 02 '13 at 00:31
  • Probably I should create separate questions for each problem in the first place. Also, I do quote limit marker when needed. The other problems are: 1) first symbol of the script gets missing, 2) how to open file provided by process substitution (/dev/fd/...) from php – x-yuri May 02 '13 at 01:37
  • I would guess if you dont quote the here-document marker the character { is in interpreted as well. – Bananguin May 02 '13 at 13:07
  • No it's not. The error arises no matter if here-doc marker is quoted or not. Seems like a quirk of bash. There is probably no sensible reason. – x-yuri May 02 '13 at 14:13