Find the answer to your Linux question:
Results 1 to 4 of 4
Hey, I got stuck coding a small bash script which creates a php file. This is what I have so far but it seems not to be working: Code: #!/bin/bash ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2011
    Posts
    2

    Echo Bash Variable to create PHP file.


    Hey,

    I got stuck coding a small bash script which creates a php file.

    This is what I have so far but it seems not to be working:
    Code:
    #!/bin/bash
    userid=</dev/urandom tr -dc A-Za-z0-9 | head -c 16
    passid=</dev/urandom tr -dc A-Za-z0-9 | head -c 24
    echo '<?php
    $tempuid = "$userid";
    $temppid = "$userid";
    //php actions below.
    ?>' > userpass.php
    When I edit userpass.php I see:
    PHP Code:
    <?php
    $tempuid 
    "";
    $temppid "";
    //php actions below.
    ?>
    Instead of the value of the variables.

    Thanks

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Your first two commands are not putting the values into the variables, you need to use $() or `` to capture the command output.

    When you echo the php code you need to use double-quotes in order to allow the variable expansion to happen. You'll also need to escape the PHP variable names, too.

    e.g.:

    Code:
    #!/bin/bash
    #userid=</dev/urandom tr -dc A-Za-z0-9 | head -c 16
    userid=$(</dev/urandom tr -dc A-Za-z0-9 | head -c 16)
    #passid=</dev/urandom tr -dc A-Za-z0-9 | head -c 24
    passid=$(</dev/urandom tr -dc A-Za-z0-9 | head -c 24)
    echo "userid: $userid"
    echo "passid: $passid"
    echo  "<?php
    \$tempuid = \"$userid\";
    \$temppid = \"$userid\";
    //php actions below.
    ?>" > userpass.php

  3. #3
    Just Joined!
    Join Date
    Nov 2011
    Posts
    2
    Thanks, finally after hours of trying and failing.

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    np, been there done that. glad it got sorted.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •