Page 1 of 1
Run PHP as normal shell script
Posted: Thu Feb 14, 2008 6:15 pm
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
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:
I get a 'command not found' error???
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?
Re: Run PHP as normal shell script
Posted: Fri Feb 15, 2008 12:17 am
by Benjamin
You get it figured out?
Re: Run PHP as normal shell script
Posted: Fri Feb 15, 2008 12:52 am
by alex.barylski
No...I gave up...do you happen to know why?

Re: Run PHP as normal shell script
Posted: Fri Feb 15, 2008 12:57 am
by Benjamin
Yeah I have a good guess

Re: Run PHP as normal shell script
Posted: Fri Feb 15, 2008 12:59 am
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.
Re: Run PHP as normal shell script
Posted: Fri Feb 15, 2008 11:26 am
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 './' ???
Re: Run PHP as normal shell script
Posted: Fri Feb 15, 2008 11:59 am
by Benjamin
Because you put a shebang in it.
Re: Run PHP as normal shell script
Posted: Fri Feb 15, 2008 7:30 pm
by alex.barylski
Ok...better a shebang than hebang I guess...
I have encountered that term before...but I have no idea what it means...I"ll have to Google it

Re: Run PHP as normal shell script
Posted: Mon Feb 25, 2008 10:31 am
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.