Search code examples
phpbashshtee

Create a bash shell script that can create a PHP program


Looking for the ability to automate the ability for a bash script to take the contents of a .PHP program and create it in a specific directory with permissions of 755. I basically want to give the user this one .sh script that will install the appropriate programs and files to get a website up and running. The problem I'm running into is the PHP variables won't save in the output file. I'm using the following command:

echo "<?php
header('Content-Type: text/xml');
require_once '/var/www/osbs/PHPAPI/account.php';
require_once '/var/www/osbs/zang/library/Zang.php';
$To = $_POST['subject'];
$Body = $_POST['text'];
# If you want the response decoded into an Array instead of an Object, set 
response_to_array to TRUE, otherwise, leave it as-is
$response_to_array = false;
# Now what we need to do is instantiate the library and set the required 
options defined above
$zang = Zang::getInstance();
# This is the best approach to setting multiple options recursively Take note that you cannot set non-existing options
$zang -> setOptions(array(
'account_sid' => $account_sid,
'auth_token' => $auth_token,
'response_to_array' => $response_to_array ));
?>" | tee /var/www/output.php

The output.php file is missing all the variables that start with $ can you guys help?


Solution

  • The easiest way to handle the quoting issues here is to use a "here-doc":

    cat >/var/www/output.php <<"EOF"
    <?php
    header('Content-Type: text/xml');
    require_once '/var/www/osbs/PHPAPI/account.php';
    require_once '/var/www/osbs/zang/library/Zang.php';
    $To = $_POST['subject'];
    $Body = $_POST['text'];
    # If you want the response decoded into an Array instead of an Object,
    # set response_to_array to TRUE, otherwise, leave it as-is
    $response_to_array = false;
    # Now what we need to do is instantiate the library and set the
    # required options defined above
    $zang = Zang::getInstance();
    # This is the best approach to setting multiple options recursively.
    # Take note that you cannot set non-existing options
    $zang -> setOptions(array(
    'account_sid' => $account_sid,
    'auth_token' => $auth_token,
    'response_to_array' => $response_to_array ));
    ?>
    EOF
    

    There is no need for tee (unless you really want to dump all that stuff to the console, which seems unnecessary). Quoting the delimiter string (<<"EOF") effectively quotes the entire here-doc, preventing the expansion of variables.