下面以 root 身份在命令行上工作
if [[ -e /var/log/apache2/error.log ]]; then echo YES; fi
YES
Run Code Online (Sandbox Code Playgroud)
然而,这在脚本中并没有
if [[ -e /var/log/apache2/error.log ]]; then
echo YES
fi
Run Code Online (Sandbox Code Playgroud)
知道为什么会这样吗?我没有得到预期的输出或错误。
脚本的第一行是 #!/bin/bash
由于脚本是由 PHP 脚本 (www-admin) 调用的,我认为可能是由于文件权限,但 error.log 文件具有读取权限
-rw-r--r-- 1 root adm 1763810 Sep 17 09:02 /var/log/apache2/error.log
Run Code Online (Sandbox Code Playgroud)
父文件夹权限
drwxr-xr-x 10 root root 4096 Mar 20 2019 var
drwxr-xr-x 5 root root 12288 Sep 17 06:25 log
drwxr-xr-x 2 root root 4096 Sep 17 06:25 apache2
Run Code Online (Sandbox Code Playgroud)
PHP脚本如何调用bash脚本
$cmd = "sh myscript.sh";
$output = array();
$output = shell_exec($cmd);
Run Code Online (Sandbox Code Playgroud)
该脚本在没有 IF 语句的情况下运行良好..
ter*_*don 11
您正在使用 .bashrc 调用 bash 脚本sh
。这通常是一个基本的 POSIX shell,如dash
. 这[[
不是 POSIX,它是一种 bashism(也存在于其他一些 shell 中),因此您sh
不支持它:
$ dash -c "if [[ 10 -gt 8 ]]; then echo yeah; fi"
dash: 1: [[: not found
Run Code Online (Sandbox Code Playgroud)
因此,要么更改您的脚本以使用标准[
([[
无论如何您都没有使用任何特殊功能):
if [ -e /var/log/apache2/error.log ]; then
echo YES
fi
Run Code Online (Sandbox Code Playgroud)
或者更改您的 PHP 脚本并使用 bash 显式调用该脚本:
$cmd = "bash myscript.sh";
Run Code Online (Sandbox Code Playgroud)
或者,由于您确实有一个#!/bin/bash
shebang,并且假设脚本设置了可执行位,只需直接调用它:
$cmd = "./myscript.sh";
Run Code Online (Sandbox Code Playgroud)