php scripting and shell commands

Discussion in 'Programming/Scripts' started by alexnz, Jun 26, 2006.

  1. alexnz

    alexnz New Member

    just too start this off im a completely newbie when it comes too php scipting so im asking for a little help with a couple of things, once i have a working example of something ill then be able too learn from it then expand it too other things i would like done:

    firstly - the look of it:

    have a list of 10 fields from which i can enter text information too

    a list of 10 radio buttons from where i can only choose one radio button

    a "go" button at the bottom that then runs the script, and a "clear" button that resets the form


    backend:

    basicly i want too be able too run shell scripts from PHP once the information has been entered for example (i dont know the correct syntax if someone can correct me)

    once the information is entered, lets just use field1 and field2, and the user has choosen radio button1 it will run script1.sh with field1 information,

    field1 would usually be a username ie: web1_user1
    field2 would usually be a specified directory ie: custom1 or template1

    radio button 1 example:

    inside script1.sh i would like it too be copying files from one static directory ie /var/www/texmplate1 too a dynamic directory specified by field2ie: /var/www/$field2/web

    radio button 2 example

    if they choose button2 i would like a file too be created lets make it a .htaccess file, i would then like the following field1 and field2 entered into it when the .htaccess file is created example:

    AuthType Basic
    AuthName "Members Only"
    AuthUserFile /var/www/$field1/.htpasswd
    <limit GET PUT POST>
    require valid-user
    </limit>

    then once the file is created itll put it into a directory labelled by $field2 ie: copy .htaccess /var/www/$field1/web/$field2




    if someone could type up my examples i can then learn from it and build up my scripting from there thanks!



    thanks too anyone that can help!
     
    Last edited: Jun 26, 2006
  2. falko

    falko Super Moderator Howtoforge Staff

  3. alexnz

    alexnz New Member

    thanks falko they have alot

    i purchased me a book too : learning php5 by david sklar - published by o'reilly books

    see how its goes, wish me luck!

    oh once ive built my script ill post it up here so others can get a jump start into php with shell scripting
     
  4. themachine

    themachine New Member HowtoForge Supporter

    I believe the function you're looking for is 'shell_exec()':

    http://us3.php.net/shell_exec


    You can pass the 'field1' and 'field2' variable via command line to the shell script as well.

    Something like this is what you are going to be doing:

    The PHP Script:
    Code:
    <?php
    $field1 = $_POST['field1'];
    $field2 = $_POST['field2'];
    ...
    if ($something == "something") {
         if(shell_exec('/path/to/shell_script1.sh $field1 $field2')) {
              print "The shell script executed without error!";
         } else {
              print "The shell script returned with an error!";
         }
    } elseif ($something == "somethingelse") {
         if(shell_exec('/path/to/shell_script2.sh $field1 $field2')) {
              print "The shell script executed without error!";
         } else {
              print "The shell script returned with an error!";
         }
    }
    ...
    ?>
    

    What you are doing is passing '$field1' and '$field2' as a parameter to the shell script. Therefore, the shell script will look something like this:

    Code:
    #!/bin/bash
    
    # $1 is the first parameter passed to the shell script
    field1=$1
    field2=$2
    
    # Do whatever you want with the $Field1 and $field2 variable
    #
    # Such as /var/www/${field2}/web
    #
    # OR 
    #
    # AuthType Basic
    # AuthName "Members Only"
    # AuthUserFile /var/www/${field1}/.htpasswd
    # <limit GET PUT POST>
    #        require valid-user
    # </limit>
    

    Using brackets for the variables isn't necessary, but whenever I am embedding them like that in a line where there is no whitespace around the variable, I like to.

    Hope that helps
     
    Last edited: Aug 8, 2006

Share This Page