Bash script can't find file

I have a bash script that wants to run a php script like this:

php /srv/www/domain.com/public_html/item.php

However bash says that it can't find the file.

From the command line this command works:

file /srv/www/domain.com/public_html/item.php

From my bash script that same command does not work, it says it can't find the file.

Must be something basic I'm missing here?

8 Replies

@rebrunius:

I have a bash script that wants to run a php script like this:

php /srv/www/domain.com/public_html/item.php

However bash says that it can't find the file.

From the command line this command works:

file /srv/www/domain.com/public_html/item.php

From my bash script that same command does not work, it says it can't find the file.

Must be something basic I'm missing here?

Can you clarify what it is that isn't found specifically? (Maybe paste the actual output?

If it's really bash saying that it can't find the file, I assume it would be php that can't be found (different PATH?)?

It's not php that it can't find. Even using the file command it comes back with the following error (exact quote):

' (No such file or directory)w/php-insite.com/public_html/iptablesxyz/iptableDatabaseBuild.php

Notably, the error notice is not quoting the full path. The full path which I've echoed from the script is:

/srv/www/php-insite.com/public_html/iptablesxyz/iptableDatabaseBuild.php

Try putting the filename in single quotes:

php -f '/srv/www/php-insite.com/public_html/iptablesxyz/iptableDatabaseBuild.php'

@rebrunius:

It's not php that it can't find. Even using the file command it comes back with the following error (exact quote):

' (No such file or directory)w/php-insite.com/public_html/iptablesxyz/iptableDatabaseBuild.php
Note the error message has overwritten the first part of the filename. This likely means your bash script is in DOS format and has a CR-LF line ending. So the bash script is trying to open ….Build.php^M (control-M at the end) which, of course, doesn't exist.

"dos2unix" will convert it to Unix format.

I am using Notepad++ for the shell scripts and it does have a selection for Windows or Unix which I just changed to Unix. It seems to be helping. Notepad++ also recognizes the .sh extension and formats the source presentation nicely when it recognizes it.

Thanks

"Notepad++ also recognizes the .sh extension and formats the source presentation nicely when it recognizes it"

However, crontab does not recognize any file with a period in it so you can't use the ".sh" extension for those crontab scripts. That sucks?

This is actually not a crontab behavior; it's the behavior of run-parts which is what executes the scripts in /etc/cron.hourly/, /etc/cron.daily/, and so on:
> If neither the –lsbsysinit option nor the --regex option is given then the names must consist entirely of upper and lower case letters, digits, underscores, and hyphens.
The reason is that there may be files in those directories that should not be executed (e.g., those ending in .dpkg-old or .dpkg-dist, or editor backup files ending in ~). It's just one of those quirks that you have to file away in the back of your head.

Note that you can create a symbolic link without a dot to a file with a dot, e.g. ln -s /root/backup.sh /etc/cron.daily/backup and it will be run.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct