doujia5863 2014-10-16 08:50
浏览 62

将变量从PHP传递给Ubuntu shell脚本

I have a shell script like this (in /usr/local/bin/esm-script/import-master.php):

#! /bin/bash 
echo "this is the file name $1."
script -c 'PGPASSWORD="pwd123" /usr/bin/psql --host localhost --port 5432 --username "postgres" --no-password --quiet "dbESM" < "$1"' /dev/null

Now I'm calling it through a PHP script like this:

$NewFile = "/var/www/...master-data.sql"; //full path
$strImport = '/usr/local/bin/esm-script/import-master.sh ' . $NewFile;  
$strMsg = shell_exec($strImport);
echo "$strMsg<br />";
echo 'done!';

However, when I run the PHP code, this is the message I get on the browser:

this is the file name /var/www/ESM-Backend/uploads/master-data.sql. Script started, file is /dev/null Script done, file is /dev/null sh: 1: cannot open : No such file Script started, file is /dev/null
done! 

I'm not a shell scripting person so I don't know if I'm missing something.

I've checked that the folder with the sql file has the correct permissions (775) and has data (insert statements).

So why does this not work? Any ideas and guidelines are really appreciated.

EDIT I hard-coded the file in the shell script file like this:

script -c 'PGPASSWORD="#UR!23" /usr/bin/psql --host localhost --port 5432 --username "postgres" --no-password --quiet "dbESM" < "/var/www/ESM-Backend/uploads/master-data.sql"' /dev/null

And it works. But I need it to run with the file passed through PHP.

  • 写回答

2条回答 默认 最新

  • dongxiong2000 2014-10-16 09:08
    关注

    I think the problem might be with the way you are passing the argument to your shell script i.e. $NewFile. From a relevant SO Post, you might want to try enclosing the argument(s) in double quotes like this:

    $strImport = '/usr/local/bin/esm-script/import-master.sh "'.$NewFile.'"';
    

    I'm assuming the permissions are set correctly and you are able to use shell_exec() normally to execute shell scripts via PHP. Hope this helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?