Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 841
  • Last Modified:

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;

*******************************************
0
PeterErhard
Asked:
PeterErhard
  • 5
  • 3
1 Solution
 
aminerdCommented:
$output = shell_exec('./run_dim_query.sh $date1 $date2');


Variable replacement does not occur with single quotes. Try:

$output = shell_exec("./run_dim_query.sh $date1 $date2");
0
 
PeterErhardAuthor Commented:
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?
0
 
aminerdCommented:
Unfortunately, no, I don't see it offhand. Does the temp_test.csv file get created?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
PeterErhardAuthor Commented:
Yes it does get generated, but it doesn't seem to come back in and run:

grep \| temp_test.csv > test.csv
0
 
blue_hunterCommented:
specific the location of your "grep"

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

hope this help
0
 
PeterErhardAuthor Commented:
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?
0
 
PeterErhardAuthor Commented:
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");

?>
0
 
aminerdCommented:
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.
0
 
PeterErhardAuthor Commented:
$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.

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now