7

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

6
  • Try to append the -q, like this: /usr/bin/php -q /path/to/my/site/dir/test.php
    – Benny
    Oct 20, 2016 at 6:11
  • Thanks but same ./test.sh: 8: .: Can't open /usr/bin/php -q /path/to/my/site/dir/test.php
    – S.I.
    Oct 20, 2016 at 6:12
  • Ok, please try with the -f (Parse and execute file) like this: php -f test.php . make sure the file is executable chmod +x test.php
    – Benny
    Oct 20, 2016 at 6:21
  • 1
    When I run $ php /path/to/test.php directly in terminal it's working. But from shell script doesn't work
    – S.I.
    Oct 20, 2016 at 6:24
  • Please remove the quote marks and the dot in line 8. Check the update of my answer.
    – pa4080
    Oct 20, 2016 at 7:19

3 Answers 3

5

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
5
  • Hm 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 ?
    – S.I.
    Oct 20, 2016 at 6:56
  • I've tried it already and the result is /test.sh: line 8: /usr/bin/php /var/www/html/site/test.php: No such file or directory
    – S.I.
    Oct 20, 2016 at 7:03
  • with #!/bin/sh -> ./test.sh: line 8: /usr/bin/php /path/to/my/site/dir/test.php: not found
    – S.I.
    Oct 20, 2016 at 7:22
  • Make sure you set /path/to/my/site/dir/ to your real correct path
    – Benny
    Oct 20, 2016 at 7:25
  • I'm sure it's correct $ pwd -> /var/www/html/site
    – S.I.
    Oct 20, 2016 at 7:32
4

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
1
  • Thank you very much for explaining and helped to get this script work!
    – S.I.
    Oct 20, 2016 at 12:10
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!

14
  • same ./test.sh: 8: .: Can't open php /path/to/my/site/dir/test.php
    – S.I.
    Oct 20, 2016 at 6:11
  • When I run $ php /path/script-name.php directly in terminal it's working. But from shell script doesn't work
    – S.I.
    Oct 20, 2016 at 6:19
  • Have you try run it as bash script #!/bin/bash?
    – pa4080
    Oct 20, 2016 at 6:21
  • You mean #!/bin/bash php /path/to/test.php ?
    – S.I.
    Oct 20, 2016 at 6:24
  • I 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.
    – pa4080
    Oct 20, 2016 at 6:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .