0

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.

itsols
  • 5,406
  • 7
  • 51
  • 95
  • what are you trying to do with these scripts ? As i understand, i think you want to import SQL from a file defined with path defined in php script, can you tell me why you are passing a php file to your postgresql command ? – Anas Oct 16 '14 at 08:56
  • I'm not passing a PHP file to postgres. Rather, I'm passing the name of the SQL data file to the script. The idea is to use it and insert the data into the postgresql DB. – itsols Oct 16 '14 at 09:02
  • so, why $NewFile = "/var/www/...master-data.php" ? – Anas Oct 16 '14 at 09:16
  • Thanks Anas - that was a typo. – itsols Oct 16 '14 at 09:17

2 Answers2

1

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.

Community
  • 1
  • 1
Vivek Pradhan
  • 4,777
  • 3
  • 26
  • 46
0

First, a big thank you for Vivek for giving me the idea of using quotes. I then thought that the the error was the way the command-line argument was enclosed within single quotes in the shell script file.

So I tried the following:

script -c 'PGPASSWORD="#UR!23" /usr/bin/psql --host localhost --port 5432 --username "postgres" --no-password --quiet "dbESM" < "'$1'"' /dev/null

And yes, it solved the problem.

The idea is that the $1 argument was given outside of the quoted string for the script command.

I also had the doubt on how to concatenate strings in bash and here's a great link I found for it: How to concatenate string variables in Bash?

Community
  • 1
  • 1
itsols
  • 5,406
  • 7
  • 51
  • 95