Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


How to pass parameters from bash to php script?
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

How to pass parameters from bash to php script?

FreekFreek Member
edited February 2013 in Help

I have a php script that I want to call from a bash script, but it needs parameters. I googled and found that I need to call the php script like this:
php /path/to/script/script.php -- 'op=deletestats&id=1&verify=yes'
The 'normal' http link to call the script is: http://127.0.0.1/path/to/script/script.php?op=deletestats&id=1&verify=yes

But I also need to modify my PHP script to use parse_str(), like so:
parse_str($argv[1]);

But where/how do I modify my PHP script to use parse_str()? Do I just add it at the top of the file ?

This is the php script I want to pass the parameters to:
http://www.phpkode.com/source/p/ultrastats/ultrastats-0.3.16/ultrastats-0.3.16/src/admin/parser-shell.php

Thanks!

Comments

  • if you're storing the parameter into a variable, then yes

  • You can run make PHP scripts to 'act' like regular shell scripts by adding this to the beginning of the file:

    #!/path/to/php

    Then just use $argv like you'd normally use.

  • @Freek said: But I also need to modify my PHP script to use parse_str(), like so:

    parse_str($argv[1]);

    What about using explode() on & for the correct $argv value instead?

  • I'm working on a project that uses a few PHP scripts. I've set up my scripts to have more of a standard POSIX command line format (option names start with --, a space after the option name, then the value). Like this:

    /path/to/script --op deletestats --id 1 --verify yes
    

    Here's the code that I use for parsing the input then...

    #!/usr/bin/php
    $argv = $_SERVER['argv'];
    $totalArgv = count($argv);
    if( $totalArgv > 1 ){
            for( $x = 1; $x < $totalArgv; $x++ )
            {
                    switch($argv[$x])
                    {
                            case '--op':
                                    $_GET['op'] = trim($argv[$x+1]);
                            break;
                            case '--id':
                                    $_GET['id'] = trim($argv[$x+1]);
                            break;
                            case '--verify':
                                    $_GET['verify'] = trim($argv[$x+1]);
                            break;
                    }
            }
    }
    

    I know it's not exactly what you want (but I thought I would share since it could be useful to you or someone else) - (parse_str($argv)) at the top should handle your usage scenario properly. Beware that if you want the script to work properly from the command line AND from the browser, you may have to check whether PHP is running under the cli or the browser. I would do that as follows:

    if (php_sapi_name() === "cli") {
        parse_str($argv[1]);
    }
    

    Hope all of that was helpful :)

  • FreekFreek Member

    Sorry to give this a bump, but I am still struggeling with this. I just picked up this project again, since the log file of the server is so huge, it currently takes about 30 mins to process.
    Therefore I really need a way to automate the deletion of the stats. I am still confused how the whole $argv thing works in my case. Examples from scratch are nice, but how do you modify an existing script?

    Thanks

  • xmobxmob Member
    edited April 2013

    It doesn't answer your specific question, but you could put the following in your BASH script:

    curl "http://somehost/path/to/script/script.php?op=deletestats&id=1&verify=yes"

  • FreekFreek Member

    @xmob Thanks, I'll try it out :)

  • bnmklbnmkl Member
    edited April 2013

    You can still use argc @NickM and logical to increment past used arguments.

    
    if ( substr( php_sapi_name(), 0, 3 ) == 'cgi' )
    {
        for ( $i = 1; $i < $_SERVER['argc']; $i++ )
        {
            switch ($_SERVER['argv'][$i])
            {
                case '--op':
                        $_GET['op']     = $_SERVER['argv'][++$i];
                break;
                case '--id':
                        $_GET['id']     = $_SERVER['argv'][++$i];
                break;
                case '--verify':
                        $_GET['verify'] = $_SERVER['argv'][++$i];
                break;
            }
        }
    }
    
    if ($_GET)
    {
        //
    }
    
    

    EDIT: I just ran the code above. It highlighted the cgi typo and to check if the next argument exists.

    
    if ( substr( php_sapi_name(), 0, 3 ) === 'cli' )
    {
        for ( $i = 1; $i < $_SERVER['argc']; $i++ )
        {
            switch ($_SERVER['argv'][$i])
            {
                case '--op':
                case '--id':
                case '--verify':
                    $i++;
                    $_GET[ trim($_SERVER['argv'][$i - 1], '-') ] = $i < $_SERVER['argc'] ? $_SERVER['argv'][$i] : '';
                break;
            }
        }
    }
    
    if ($_GET)
    {
        //
    }
    
    
  • FreekFreek Member

    I had a brainfart and tried to comment out the complete part that was asking for a confirmation in the php file, and that actually worked! Thanks guys :)

Sign In or Register to comment.