1

I've looked at how composer is set up to run a php script by calling composer without issuing php manually and I tried to do this myself but it isn't working. I know this is probably asked before but I can't formulate a question to find it.

I have my file

#!/usr/bin/php
<?php

echo 'teststring' . PHP_EOL;

when I issue test in the directory the file is located I get nothing, but if I do php test then I get "teststring" echoed out. The question is - how do I set it up so that I can execute the file directly?

I have given x permissions on the file.

Here is a sample output:

x@y:~/www/html/dev/Project$ ls
...
-rwxrwxr-x 1 www-data www-data    53 сеп 22 12:34 test
...
x@y:~/www/html/dev/Project$ test
x@y:~/www/html/dev/Project$ php test
teststring
x@y:~/www/html/dev/Project$ which php
/usr/bin/php
x@y:~/www/html/dev/Project$
php_nub_qq
  • 15,199
  • 21
  • 74
  • 144
  • 1
    Your shebang line doesn't look ok to me. It should be something like `#!/usr/bin/env/php` or `#!/usr/bin/php` depending on where your executable lies. – Havelock Sep 22 '15 at 09:23
  • @Havelock tried both your suggestions, still the same resullt. As I said I just copied that from composer. – php_nub_qq Sep 22 '15 at 09:25
  • Then you need to find where your executable php is. You can try with something like `find / -name php` or `locate php` or even maybe `whereis php`... – Havelock Sep 22 '15 at 09:28
  • You should try to locate php executable by using which instead of whereis like so`$ which php` – Filip Sep 22 '15 at 09:34
  • My installation is pretty standard and php is located in `/usr/bin/php` but even if I change line 1 to `#!/usr/bin/php` I still get the same results, see the edit I will make shortly. – php_nub_qq Sep 22 '15 at 09:36
  • The problem you're facing is only about the way you're trying to execute the shell script, not about the path of php, you can execute it like so `./teststring` or using the full path of the script like so `~/www/html/dev/Project/teststring` – Filip Sep 22 '15 at 09:48

1 Answers1

0

You should try to locate php executable by using which instead of whereis like so

$ which php

... this command only searches for executables, while whereis tries to guess useful links, you can read more [here] (https://superuser.com/questions/40301/which-whereis-differences)

Then just run it like so

$ ./teststring

You can read more about executing shell scripts here

thegeekstuff

tldp.org

Community
  • 1
  • 1
Filip
  • 1,214
  • 10
  • 19
  • Well that is not the case with composer, if you install it locally you can call `composer`, That's what I'm interested in. Also if I call it with `./` notation I get `-bash: ./test: /usr/bin/php^M: bad interpreter: No such file or directory `. If I change the first line to `#!/usr/bin/env php` I get `: No such file or directory` – php_nub_qq Sep 22 '15 at 09:51
  • My bad, let me check out this `composer` you speak of, I obviously know nothing about :) – Filip Sep 22 '15 at 09:55
  • It is not just composer, pretty much any library provides such functionality, you can also see `phpunit` for example, it is the same. Also laravel's `artisan` has the same `#!/usr/bin/env php`, you can see it [here](https://github.com/laravel/laravel/blob/master/artisan) – php_nub_qq Sep 22 '15 at 09:56
  • Hmm, the shell should interpret that php executable just fine though and that `/usr/bin/php^M` looks weird, you should discard the trailing ^M, [what-does-m-character-mean-in-vim](http://stackoverflow.com/questions/5843495/what-does-m-character-mean-in-vim) or [remove-m-characters-at-end-of-lines-in-vi](http://www.tech-recipes.com/rx/150/remove-m-characters-at-end-of-lines-in-vi/) – Filip Sep 22 '15 at 10:09