Call and run php script from shell script
Solution 1
The problem here is that you are quoting the entiore command you are trying to run as a single variable. As a result, you're not running php
with foo.php
as an argument but instead are attempting to execute a file called php foo.php
. Here's a simpler example to show you what I mean:
$ var1="echo "
$ var2="foo"
$ set -x ## debugging info
$ "$var1$var2"
+ 'echo foo' ### The shell tries to execute both vars as a single command
bash: echo foo: command not found
$ "$var1" "$var2"
+ 'echo ' foo ### The shell tries to execute 'echo ' (echo with a space)
bash: echo foo: command not found
So, the right way is to remove the space and quote each variable separately:
$ var1="echo"
$ var2="foo"
$ "$var1" "$var2"
If you do that though, you'll hit the next error. The .
is the source
command. That tries to read a shell script and execute it in the current session. You are giving it a php script instead. That won't work, you need to execute it, not source it.
Finally, always avoid using CAPITAL variable names. The shell's reserved variables are capitalized so it's a good idea to always use lower case variable names for your scripts.
Putting all this together (with a few other minor improvements), what you want is something like:
#!/bin/sh
list="/path/to/my/site/dir"
config="/usr/bin/php"
for i in "$list"
do
"$config" "$i"/test.php
done
Solution 2
The problem in your code is the line:
. "${CONFIG}${i}/test.php"
Remove the .
Here is another example:
$ ls -l
-rwxrwxr-x 1 bg bg 67 Oct 20 09:42 index.php
-rwxrwxr-x 1 bg bg 68 Oct 20 09:43 test.sh
index.php
<?php
shell_exec('echo Hello > /tmp/hello.txt');
?>
test.sh
#!/bin/bash
/usr/bin/php index.php
Solution 3
Usually the command php is used for interpretation of PHP scripts in the shell.
$ php /path/script-name.php
I made simple test.sh and it works:
$ cat ./text.sh
#!/bin/bash
sudo php /var/www/wiki/maintenance/update.php
$ chmod +x ./test.sh
$ ./test.sh
It works.
After that I made complicated script as your example:
$ cat ./text.sh
#!/bin/sh
LIST=/var/www/wiki/maintenance
CONFIG=/usr/bin/php
for i in $LIST
do
${CONFIG} ${i}/update.php
done
$ sudo ./test.sh
It works!
$ cat ./text.sh
#!/bin/sh
LIST="/var/www/wiki/maintenance"
CONFIG="/usr/bin/php "
for i in $LIST
do
${CONFIG}${i}/update.php
done
$ sudo ./test.sh
Works also!
Related videos on Youtube
Comments
-
S.I. over 1 year
I'm trying something simple yet can't figured it out. Trying to run one shell script which call php script from the site directory. My shell script is simple
test.sh
:#!/bin/sh LIST="/path/to/my/site/dir" CONFIG="/usr/bin/php " for i in $LIST do . "${CONFIG}${i}/test.php" done
My
test.php
doesn't have errors and when I run it directly in browser it's working. It again simple script like<?php // source code ?>
When I run
./test.sh
result is./test.sh: 8: .: Can't open /usr/bin/php /path/to/my/site/dir/test.php
Line 8 is
. "${CONFIG}${i}/test.php"
I've tried also to add at the beginning of PHP file this line but the result is same
#!/usr/bin/php
UPDATE: Path to php
$ which -a php /usr/bin/php
I've also made
chmod +x test.php
-
Benny over 7 yearsTry to append the
-q
, like this:/usr/bin/php -q /path/to/my/site/dir/test.php
-
S.I. over 7 yearsThanks but same
./test.sh: 8: .: Can't open /usr/bin/php -q /path/to/my/site/dir/test.php
-
Benny over 7 yearsOk, please try with the
-f
(Parse and execute file) like this:php -f test.php
. make sure the file is executablechmod +x test.php
-
S.I. over 7 yearsWhen I run
$ php /path/to/test.php
directly in terminal it's working. But from shell script doesn't work -
pa4080 over 7 yearsPlease remove the quote marks and the dot in line 8. Check the update of my answer.
-
S.I. over 7 yearsWithout them I've got 1000+ lines in terminal with mysql/php configuration files..
-
-
S.I. over 7 yearssame
./test.sh: 8: .: Can't open php /path/to/my/site/dir/test.php
-
S.I. over 7 yearsWhen I run
$ php /path/script-name.php
directly in terminal it's working. But from shell script doesn't work -
pa4080 over 7 yearsHave you try run it as bash script
#!/bin/bash
? -
S.I. over 7 yearsYou mean
#!/bin/bash php /path/to/test.php
? -
pa4080 over 7 yearsI mean to put it in the first line of the script instead of
#!/bin/sh
. This line indicates the shell who will interpret the script. -
S.I. over 7 yearsHm, different error now. Like the file isn't in the directory but it is
./test.sh: line 8: .: /usr/bin/php /path/to/my/site/dir/test.php: No such file or directory
-
S.I. over 7 yearsI've wrote full path there like
/var/www/html/site/test.php
-
S.I. over 7 yearsHm may be because of different users?
-rwxrwxr-x 1 root root 382 Oct 20 09:46 test.sh
and-rwxr-xr-x 1 root www-data 25 Aug 19 09:15 test.php
? -
pa4080 over 7 yearsHowever,
#!/bin/sh
isn't a problem. I think @Benny is right, remove.
. -
S.I. over 7 yearswith
#!/bin/sh
->./test.sh: line 8: /usr/bin/php /path/to/my/site/dir/test.php: not found
-
Benny over 7 yearsMake sure you set
/path/to/my/site/dir/
to your real correct path -
S.I. over 7 yearsI'm sure it's correct
$ pwd -> /var/www/html/site
-
S.I. over 7 yearsStill I've got
Not found
and without quote marks I've got 1000+ lines in terminal with mysql/php configuration files.. Everything is same as your and Benny's examples and didn't work. -
S.I. over 7 yearsWhen I run
$ php /path/to/script/test.php
directly in terminal it's work. But if I run same thing from shell script which contain same lines I've gotnot found
. Even your first example#!/bin/bash sudo php /var/www/wiki/maintenance/update.php
-
terdon over 7 yearsWhy are you using
sudo
? That just introduces a pointless security problem. Please only usesudo
when you need it. -
S.I. over 7 yearsJust for testing @SpasSpasov examples. Still doesn't want to execute php script from shell script. It's executed only if I run it directly in terminal
-
terdon over 7 years@S.I. yes, it won't. See my answer. You need to i) remove the extra space from
CONFIG
; ii) quote the variables separately and iii) execute it, not source it. -
S.I. over 7 yearsThank you very much for explaining and helped to get this script work!