在 bash 脚本中运行 php:错误“无法打开输入文件”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39649556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:13:05  来源:igfitidea点击:

Run php in a bash script: error "Could not open input file"

phpbash

提问by sylvain1264

I made a simple script like:

我做了一个简单的脚本,如:

#!/bin/bash
php /var/www/mysite/script1.php
php /var/www/mysite/script2.php

When I run it as root like this:

当我像这样以 root 身份运行它时:

bash update.sh

I get following errors:

我收到以下错误:

Could not open input file: /var/www/mysite/script1.php
Could not open input file: /var/www/mysite/script2.php

What is wrong ? I tried with permissions 777 on my php files and all folders to access it. When I do directly php /var/www/mysite/script1.php in my command line it works fine.

怎么了 ?我尝试在我的 php 文件和所有文件夹上使用 777 权限来访问它。当我在命令行中直接执行 php /var/www/mysite/script1.php 时,它工作正常。

采纳答案by Jean-Fran?ois Fabre

When a batch file passes through some windows-compliant editors or other mishaps, it may happen that carriage return chars are appended to the end of the line (just before linefeed)

当批处理文件通过一些 Windows 兼容的编辑器或其他不幸时,可能会发生回车符附加到行尾(就在换行之前)

so the all the lines for instance this one:

所以所有的行,例如这一行:

php /var/www/mysite/script1.php

contains an invisible \rchar which is interpreted as part of the argument py php=> file /var/www/mysite/script1.php<invisible char>not found.

包含一个不可见的\r字符,它被解释为参数 py php=>/var/www/mysite/script1.php<invisible char>未找到文件的一部分。

Do the following:

请执行下列操作:

dos2unix update.sh > newbatchfile.sh

or

或者

tr -d "5" < update.sh > newbatchfile.sh

(compare file sizes, if the newbatchfile is smaller, problem was CR chars and is fixed)

(比较文件大小,如果 newbatchfile 较小,则问题是 CR 字符并已修复)