LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-25-2013, 01:22 AM   #1
Dafydd
Member
 
Registered: Oct 2008
Posts: 344

Rep: Reputation: 29
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
 
Old 11-25-2013, 01:58 AM   #2
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
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.
 
Old 11-25-2013, 03:13 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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]);
    }
?>
 
1 members found this post helpful.
Old 11-26-2013, 03:00 AM   #4
Dafydd
Member
 
Registered: Oct 2008
Posts: 344

Original Poster
Rep: Reputation: 29
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);
?>
 
Old 11-27-2013, 11:02 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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);
 
Old 11-29-2013, 11:53 PM   #6
Dafydd
Member
 
Registered: Oct 2008
Posts: 344

Original Poster
Rep: Reputation: 29
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] passing variable through script (bash) ted_chou12 Programming 1 11-20-2011 10:17 AM
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
passing array and variable to function in bash script ajaypitroda Programming 2 07-07-2009 11:10 PM
Calling perl script and passing variable from php script hosea Programming 5 10-21-2008 08:01 AM
Bash Script Passing variable to Function nutthick Programming 2 02-02-2005 05:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration