24 November 2009

How to generate PHP code from Bash


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.

7 comments:

  1. Wow, I can't believe you made it already! Thanks! Does it matter where I put the -e codes? Also, I'm still not good with long PHP codes, where do I put the long codes?

    ReplyDelete
  2. Oh, I forgot to mention, here's my blog:

    http://mpscripts.omegasubs.com/blog/

    It'd be cool if you stop by and check it out. I might have more questions for you later about the PHP codes.

    Also, do you know anything about OpenSolaris Linux?

    ReplyDelete
  3. The parameters (denoted by the minus sign) always come before other arguments, so you have to put -e before the string.

    As for PHP, look for a good tutorial on the net. If you just want to write PHP scripts, all you need is a text editor (a GUI one or a console based one like nano or vim). If you want to generate a PHP script from a shell script, do that line for line, and use sed to insert backslash escapes where necessary.

    OpenSolaris is nice and robust if you want a server operating system, but hard to set up. It doesn't use a Linux kernel (it is a "real" Unix), but has nearly the same command line tools. Gnome and KDE are also available as an option.

    ReplyDelete
  4. What's a string? Hey, it would rock if you could leave a comment on my blog, and maybe tell my readers a little bit about OpenSolaris Linux and PHP! Your blog is in my favorites now!

    http://mpscripts.omegasubs.com/blog/

    ReplyDelete
  5. What is the point of shell scripts generating php code? It's could be easier done in the PHP itself. Just imaging spawning a process for each line!

    ReplyDelete
  6. I'm not sure, I wrote the article on a reader's request and assumed that is what he wanted to achieve. BTW it won't spawn a process because echo is a shell builtin. But using a more advanced scripting language for string manipulation is probably more efficient.

    @Anonoymous: I'm neither an expert for PHP nor OpenSolaris, but I'll check by on your blog from time to time. How about if you learn a bit of PHP and write a few beginner tutorials yourself on your blog? Lots of folks are interested in web programming.

    A string is any piece of data interpreted not as a numeric value or raw byte data, but as text. This distinction is important because things like character set conversion or consideration of writing direction may need to be done.

    ReplyDelete
  7. nice post of about how to generate php code from bash. It is nrw for me.

    php developer india

    ReplyDelete