Run PHP as normal shell script

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Run PHP as normal shell script

Post by alex.barylski »

Edit: Turns out that I have to invoke the script like: ./myscript.php inorder for it to work...why can I not just ignore the prefix? Especially when I'm in the CWD of the script itself.

Always something with Linux :P

I read the direction off PHP site on running scripts from CLI:

Code: Select all

#!/usr/bin/php
<?php 
  // chmod +x myscript.php
  echo 'Hello';
?>
When I invoke the script like so:

Code: Select all

myscript.php
I get a 'command not found' error??? :banghead:

I have marked the file as executable via chmod +x on the script and I have the PHP interpreter located at the first line of file and yet I still need to call:

php myscript.php???

Ideas?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Run PHP as normal shell script

Post by Benjamin »

You get it figured out?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Run PHP as normal shell script

Post by alex.barylski »

No...I gave up...do you happen to know why? :)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Run PHP as normal shell script

Post by Benjamin »

Yeah I have a good guess :lol:
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Run PHP as normal shell script

Post by Christopher »

PHP is usually not installed in /usr/bin/php, so you either need to copy it there or just change that path to point to where PHP is actually installed.

Also in Unix you need to type ./foo if the current directory is not in the current set of execution paths. But just like like any OS, if the command is in the path then you can just type foo and it will run.
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Run PHP as normal shell script

Post by alex.barylski »

arborint wrote:PHP is usually not installed in /usr/bin/php, so you either need to copy it there or just change that path to point to where PHP is actually installed.

Also in Unix you need to type ./foo if the current directory is not in the current set of execution paths. But just like like any OS, if the command is in the path then you can just type foo and it will run.
usr/bin/php may not be the path but why then did it work when I prefixed the command with the './' ???
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Run PHP as normal shell script

Post by Benjamin »

Because you put a shebang in it.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Run PHP as normal shell script

Post by alex.barylski »

Ok...better a shebang than hebang I guess... :P

I have encountered that term before...but I have no idea what it means...I"ll have to Google it :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Run PHP as normal shell script

Post by pickle »

You have to ./ because you need to tell the shell where to look. Linux doesn't give a hoot where you are in the file system - it always looks at your (and I don't think I have the right term here) execution paths to find any executables. It's like in Windows when you go Start > Run & type in "notepad" - it works because "notepad" is in C:\WINDOWS\System32. You type in ... "fable" & it won't work because "fable.exe" isn't in the command prompt's execution path - C:\WINDOWS\System32.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply