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.
#!/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