Link to home
Start Free TrialLog in
Avatar of PeterErhard
PeterErhard

asked on

Shell Script through PHP

I'm having problems getting a php script to run a shell script, details below, any advice would be greatly appreciated.

******************************************

This works and successfully outputs to the csv file:

./run_dim_query.sh "01/01/2006" "01/07/2006"

However when using the PHP script it doesn't:

******************************************

<?php

$Date1Day = $_POST["Date1Day"];
$Date1Month = $_POST["Date1Month"];
$Date1Year = $_POST["Date1Year"];
$Date2Day = $_POST["Date2Day"];
$Date2Month = $_POST["Date2Month"];
$Date2Year = $_POST["Date2Year"];

$date1 = $Date1Day . "/" . $Date1Month . "/" . $Date1Year;
$date2 = $Date2Day . "/" . $Date2Month . "/" . $Date2Year;

echo $date1;
echo "<br>";
echo $date2;

$output = shell_exec('./run_dim_query.sh $date1 $date2');

echo $output;

?>

**********************************************

The $output variable returns "Enter value for 1:", and doesn't carry on. Why is this?

**********************************************

#!/bin/ksh

export ORAENV_ASK=NO
export ORACLE_SID=client

. /usr/local/bin/oraenv

sqlplus -s pcms/pcms@pcms @dim_query.sql $1 $2

grep \| temp_test.csv > test.csv

*******************************************

WHENEVER SQLERROR EXIT 5
WHENEVER OSERROR EXIT 10
SET PAGESIZE 0
SET LINESIZE 500
SET FEEDBACK OFF
SET ECHO OFF

SPOOL temp_test.csv

SELECT DISTINCT rtrim(cd.cH_doc_id) || '|' ||
    rtrim(cm.attr_178) || '|' ||
    rtrim(cm.attr_97) || '|' ||
    rtrim(cd.title)  || '|' ||
    rtrim(cd.action_date) || '|' ||
    rtrim(cm.attr_179)
    FROM pcms_cm_catalogue cv,
               pcms_cm_phases cp,
               cm_attributes cm,
               pcms_chdoc_data cd
          WHERE cd.product_id in(Select distinct(product_id) from pcms_chdoc_data)
            AND cd.create_date BETWEEN TO_DATE('&1','MM/DD/YYYY') AND TO_DATE('&2','MM/DD/YYYY')
            AND cd.super_type LIKE '%'
            AND cd.ch_doc_type = 'WO'
            AND cd.status LIKE '%'
            AND cd.status != '$TO_BE_DEFINED'
            AND cd.ch_doc_id LIKE '%'
            AND cd.ch_uid = cm.ch_uid
            AND cd.cm_phase = cp.phase_id
            AND NVL (cd.seq, 1) = 1
            AND cp.phase_name LIKE '%'
            AND cd.ch_uid = cv.ch_uid
            AND cv.user_name LIKE '%';

SPOOL OFF

EXIT;

*******************************************
ASKER CERTIFIED SOLUTION
Avatar of aminerd
aminerd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PeterErhard
PeterErhard

ASKER

Thanks very much, that did the trick.

One more question if you don't mind:

**********

#!/bin/ksh

export ORAENV_ASK=NO
export ORACLE_SID=client

. /usr/local/bin/oraenv

sqlplus -s pcms/pcms@pcms @dim_query.sql $1 $2

grep \| temp_test.csv > test.csv

****************

It is never running "grep \| temp_test.csv > test.csv"

Can you see the reason why?
Unfortunately, no, I don't see it offhand. Does the temp_test.csv file get created?
Yes it does get generated, but it doesn't seem to come back in and run:

grep \| temp_test.csv > test.csv
Avatar of blue_hunter
specific the location of your "grep"

/usr/local/bin/grep    <--- as example

hope this help
Sorry, are you referring to having the statement something like this:

/usr/local/bin/grep \| temp_test.csv > test.csv

Or should it be the directory where the files are actually located?
I've tried this but it doesn't work :(

<?php

$Date1Day = $_POST["Date1Day"];
$Date1Month = $_POST["Date1Month"];
$Date1Year = $_POST["Date1Year"];
$Date2Day = $_POST["Date2Day"];
$Date2Month = $_POST["Date2Month"];
$Date2Year = $_POST["Date2Year"];

$date1 = $Date1Day . "/" . $Date1Month . "/" . $Date1Year;
$date2 = $Date2Day . "/" . $Date2Month . "/" . $Date2Year;

echo $date1;
echo "<br>";
echo $date2;

$output = shell_exec("./run_dim_query.sh $date1 $date2");
$output = shell_exec("grep \| /var/apache/htdocs/reports/temp_test.csv > /var/apache/htdocs/reports/test.csv");

?>
Does test.csv already exist? Does the user you're running the PHP script as have write permissions to the file? As is, the script *should* work. It's tough to say why it wouldn't.

What you could try:

$output = shell_exec("./run_dim_query.sh $date1 $date2 2> /tmp/log");

Any error messages should be redirected to /tmp/log. See if there's anything descriptive there after running the PHP script.
$output = shell_exec("./run_dim_query.sh $date1 $date2");

The above runs fine. It's just the grep that doesn't work. I've tried:

$output = shell_exec("grep \| /var/apache/htdocs/reports/temp_test.csv > /var/apache/htdocs/reports/test.csv");

and

$output = shell_exec("grep \| temp_test.csv > test.csv");

but neither work.