If you are into server side web programming, you will probably have had some exposure to PHP. Advantages of the language are that it is very widely supported, and that it can be freely mixed with HTML within the same file.
You might want to have some script running on the server for the purpose of generating PHP code. Unfortunately, you cannot throw everything at Bash's string functions as-is because some special characters are used to denote expansion or redirection. One totally unexciting but have-to-know bash feature comes into play here, called quoting.
Quoting is the art of using single or double quotes or backslashes to protect a range of characters in a string from being interpreted by Bash. In short, single quotes protect everything inbetween them so the input is taken 1:1, double quotes allow variable expansion (via $dollar sign) and command substition (via `backticks`) and protect mostly everything else, and the backslash protects the character following it. Mixed or nested quoting is often necessary and one of the most unintuitive things on earth, but for this howto, I will keep it as simple as possible.
Ok, let's do a "Hello World" in PHP, via Bash.
echo -e "<?php\n echo \"Hello World\";\n?>"
Output:
<?php
echo "Hello World";
?>
Why do I use the -e parameter? It allows the echo command to interpret C-style backslash escape sequences (like \n for a newline, \t for a tab). In addition, I'm enclosing the whole string in double quotes, as this allows for interpreting shell variables and escapes without messing up on most other stuff. The \n after the opening PHP tag is a line break, then two spaces follow for intendation. I have to escape the double quotes around "Hello World" ("protect" them) to not have them interpreted as string quoting, and output them literally instead. After that comes the semicolon that ends the PHP command, another newline and the closing PHP tag.
But, you might complain now, the PHP code itself might contain backslash quotes too! How to escape that, dude? Well, in that case we protect the backslash by writing a double backslash.
echo -e "<?php\n echo \"Hello \\\"World\\\"\";\n?>"
Output:
<?php
echo "Hello \"World\"";
?>
The parts with three backslashes in a row are a backslash protecting a backslash, followed by a backslash protecting a double quote.
Because of the "weak" double quoting, we can use shell variables and command substitution.
echo -e "<?php\n echo \"Script generated by $SHELL\";\n?>"
Output:
<?php
echo "Script generated by /bin/bash";
?>
<?php\n echo \"Script generated by $SHELL on `uname`\";\n?>"
Output:
<?php
echo "Script generated by /bin/bash on Linux";
?>
You can build on this from this point on: Just keep in mind that quoting with double quotes protects all characters except ", \, $, and `, so you have to escape them with a backslash if you want to output them literally.
Showing posts with label php. Show all posts
Showing posts with label php. Show all posts
24 November 2009
Subscribe to:
Posts (Atom)