LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Passing variable from bash script to php script. (https://www.linuxquestions.org/questions/programming-9/passing-variable-from-bash-script-to-php-script-4175485785/)

Dafydd 11-25-2013 01:22 AM

Passing variable from bash script to php script.
 
I am at my wits end on this one. What syntax error?

Quote:

dave@Dafydd:~/PlayingWhere$ ./build_search_data.sh 01/23/2013
./build_search_data.sh: line 2: ?php5: No such file or directory
./build_search_data.sh: line 5: syntax error near unexpected token `" {$_SERVER['argv'][1]}"'
./build_search_data.sh: line 5: ` $searchPattern=(" {$_SERVER['argv'][1]}");'
dave@Dafydd:~/PlayingWhere$
I want to pass a data string from a bash script to a php script. I have an application my computer that uses a line simular to line 5 that recieves an email address and send out the email. I've read through w3schools, php.org, phpfreaks and a bunch of google finds, but nothing seems to work.

Can someone tell me what I am doing wrong?
Thanks for any light you can shed on this.
Dave


Code:

#!/bin/bash
<?php5
function lookfor.php()
{
        $searchPattern=(" {$_SERVER['argv'][1]}");

        $file = fopen("WhereAmIPlaying", "r") or exit("Unable to open file!");
        while(!feof($file))
          {
                        $stringToSearch = fgets($file);

                    $locationOfString = stripos($stringToSearch , $searchPattern);

                        if("$locationOfString" !== false)
                        {
                                printf('%s', "$stringToSearch");
                                 
                        }
        }
        fclose($file);
}
?>

## Build search string
day=$1

main ()
{
cat out.txt | (
    while read city_to_find;
            do build_search_string "$day" "$city_to_find";
                done
    )
}

php_EXEC='php5 -r'

function build_search_string()
{
       
          giglocation=$2
        I_want_to_attend="$day"",""$giglocation"
## confirm the data  it correct and there
##        echo "$I_want_to_attend";
    php5 -f lookfor.php5 "$I_want_to_attend";
}
main


zhjim 11-25-2013 01:58 AM

You are using the wrong shebang! (The first line of a file starting with #!. It tells the calling program which program it should use to interpret the script.

So you can either remove the shebang line and call the script with php -e /path/to/file or put the path to the php binary in place of /bin/bash in the first line.

There also I a little trick and put #!$(which php) as first line. This calls which which returns the path to the binary past as the first argument to it.

NevemTeve 11-25-2013 03:13 AM

bash and php are different programs/languages! here is an example how to use $argv:

Code:

#!/usr/local/bin/php
<?php
    printf ("%s is running\n", $argv[0]);
    for ($i=1; $i<$argc; ++$i) {
        printf ("'%s'\n", $argv[$i]);
    }
?>


Dafydd 11-26-2013 03:00 AM

I got the syntax error gone. Thank you 'NevemTeve'.
Some printf statements show that the correct data is being passed. But it is not returning any results.

Anyone got any ideas why
Code:

$locationOfString = stripos($stringToSearch, $searchPattern)
does not work?
Is there a different PHP search I could use?
Quote:

$searchPattern will have this format: 11/02/2013,Galveston, TX
WhereAmIPlaying will have this format: 11/02/2013,Galveston, TX,Jana Pochop,artistwebsite.html,venuewebsite.html
Code:

#!/usr/local/bin/php
<?php

        $file = fopen("WhereAmIPlaying", "r") or exit("Unable to open file!");
                                     
        while(!feof($file))
          {
      for ($i=1; $i<$argc; ++$i) {
        $searchPattern = $argv[$i];
                   
                  $stringToSearch = fgets($file);

                    $locationOfString = stripos($stringToSearch, $searchPattern);

                        if($locationOfString !== false)
                        {
                                printf('%s', $stringToSearch);
                                 
                        }
      }
        }
        fclose($file);
?>


NevemTeve 11-27-2013 11:02 AM

That's what debugging is good for:

Code:

$locationOfString = stripos($stringToSearch, $searchPattern);
printf ("Yo-ho stripos ('%s','%s') returned %d\n",
        $stringToSearch, $searchPattern,
        $locationOfString);
var_dump ($locationOfString);


Dafydd 11-29-2013 11:53 PM

Want to thank you guys for the feed back. The problem I was having was my own goof up. My code was searching one part of the state for an event. My data base was for a different part of the state. Once that was fix, everything works as expected. Mark this solved.

Thanks again.


All times are GMT -5. The time now is 12:47 AM.