PHP Shell Programming (Grep)

I have the following script (see bottom of script) which runs fine, except for this part:

grep \| temp_test.csv > test.csv

It never runs this section for some reason.

I've tried adding

$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");

to the php file but it doesn't work either.

Any tips/suggestionbs on how to get this part going would be much appreciated.

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

<?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;

*******************************************
PeterErhardAsked:
Who is Participating?
 
Kim RyanConnect With a Mentor IT ConsultantCommented:
Try egrep (enhanced grep), it may be better at handling the escaping the | symbol

egrep "\|" temp_test.csv > test.csv
egrep '|' temp_test.csv > test.csv

or maybe just this
grep '|' temp_test.csv > test.csv


0
 
PeterErhardAuthor Commented:
Thanks, will give that a try.

The weird thing is that when I run grep \| temp_test.csv > test.csv straight from the command line, it works perfectly :S
0
 
Kim RyanIT ConsultantCommented:
Are you running same ksh from command line, or is it a different one?
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
PeterErhardAuthor Commented:
I'm using ksh when I run it manually, but not specificing anything through my PHP script. Do I need to? If so, how would I do that, to run that grep command?
0
 
Kim RyanIT ConsultantCommented:
It is specified by first line in your script, which is set to ksh also
#!/bin/ksh
Not sure why grep won't work, but it can be safer to quote special characters like the pipe symbol
0
 
PeterErhardAuthor Commented:
Sorry, but how are you suggesting I change this:

$output = shell_exec("grep \| temp_test.csv > test.csv");
0
 
Kim RyanIT ConsultantCommented:
I was suggesting that in your script...
--------------
sqlplus -s pcms/pcms@pcms @dim_query.sql $1 $2

grep \| temp_test.csv > test.csv

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

...you change the grep line to one of the following
grep '|' temp_test.csv > test.csv
grep '\|' temp_test.csv > test.csv
0
 
PeterErhardAuthor Commented:
Ahh I see, thank you, will give it a try.

I don't have access right at the moment, but will report backin 10 hours time.

Thanks for your help so far :)
0
 
PeterErhardAuthor Commented:
I've tried:

egrep "\|" temp_test.csv > test.csv
egrep '|' temp_test.csv > test.csv
grep '|' temp_test.csv > test.csv
grep '\|' temp_test.csv > test.csv

and none of them work, but they do run fine at the command line :(
0
 
PeterErhardAuthor Commented:
I used the below, and I had a permission problem, it's all fixed now :)

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

I'll give you the points anyway, for helping me out.
0
 
Kim RyanIT ConsultantCommented:
thanx, glad its working for u.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.