LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-05-2011, 03:56 AM   #1
suse_nerd
Member
 
Registered: May 2008
Distribution: SuSe
Posts: 50

Rep: Reputation: 15
Converting bash script into PHP


I wrote the following as our developers won't do it for us! The rest of my programming is in PHP, any ideas on how I can get this into php?

I'm in the middle of writing a reminder system which involves importing the customer data and sending out reminder emails.

All the script below does is download the current report $FILEIN, compare field number 6 ($DATA) with the date($DATE), if there is a match, it checks each line in the prescription_skus.csv to see if there is a match with field number 1 in the current report. It then outputs the entire line to $FILEOUT

Code:
TODAY=`date +"%Y-%m-%d"`
FILEIN="export-"$TODAY".csv"
FILEOUT="report"`date +"%Y-%m-%d"`
DATE=`date --date="1 days ago" +"%Y-%m-%d"`
INDATE=$1

wget  http://mysite.co.uk/$FILEIN


if test -z "$1"; then

	cat $FILEIN | while read line; do
		DATA=`echo $line | cut -d ',' -f 6 | awk '{ print $1; }'`
		if [ "$DATA" = "$DATE" ]; then
			if cat prescription_skus.csv | grep -q `echo $line | cut -d ',' -f 1` 
				then echo $line >> $FILEOUT; 
			fi
		fi
	done
  
else
	cat $FILEIN | while read line; do
		DATA=`echo $line | cut -d ',' -f 6 | awk '{ print $1; }'`
			if [ "$DATA" = "$1" ]; then
				if cat prescription_skus.csv | grep -q `echo $line | cut -d ',' -f 1` 
				then echo $line >> $FILEOUT; 
			fi
		fi
	done
  
fi
 
Old 07-05-2011, 10:56 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Code:
TODAY=`date +"%Y-%m-%d"`
Here you'll be using the date() function.

Code:
FILEIN="export-"$TODAY".csv"
FILEOUT="report"`date +"%Y-%m-%d"`
This is just string manipulation, once you've got the $TODAY variable

Code:
DATE=`date --date="1 days ago" +"%Y-%m-%d"`
Again, date(), but you'll have to use time() and take away the number of seconds in a day in order to get yesterday's date.

Code:
INDATE=$1
You'll use something like $_GET[] or $_REQUEST[] to read variables in.

Code:
wget  http://mysite.co.uk/$FILEIN
Your best bet here is using curl to download the file.

Code:
if test -z "$1"; then
	cat $FILEIN | while read line; do
Basic string comparison, then using fopen() to open a file, and fgets() to read from it.

Code:
		DATA=`echo $line | cut -d ',' -f 6 | awk '{ print $1; }'`
Perhaps a bit more tricky... Use explode() to split the string into an array, then just access the 6th element.

Code:
		if [ "$DATA" = "$DATE" ]; then
String comparison.

Code:
			if cat prescription_skus.csv | grep -q `echo $line | cut -d ',' -f 1`
You'll need to break this up a bit - first isolate the thing you want to match, using explode() as detailed above. Then you'll need to open the file, read it line-by-line and use preg_match() to see if there are any matching lines.

Code:
				then echo $line >> $FILEOUT;
This is just opening and writing to a file again.

Code:
			fi
		fi
	done
else
You shouldn't need help with this bit

Code:
	cat $FILEIN | while read line; do
		DATA=`echo $line | cut -d ',' -f 6 | awk '{ print $1; }'`
			if [ "$DATA" = "$1" ]; then
				if cat prescription_skus.csv | grep -q `echo $line | cut -d ',' -f 1` 
				then echo $line >> $FILEOUT; 
			fi
		fi
	done
  
fi
Same as similar sections of code above.

Hopefully this will give you a decent start to writing your PHP code... If you still have problems, do post back the code you have written and I would be more than happy to look at it

Hope this helps,

Last edited by Snark1994; 07-05-2011 at 04:54 PM. Reason: Finishing off my earlier post
 
Old 07-06-2011, 04:21 AM   #3
suse_nerd
Member
 
Registered: May 2008
Distribution: SuSe
Posts: 50

Original Poster
Rep: Reputation: 15
Code:
<?php
//include("functions.inc.php");
//  include("header.php");

//writeheader ();
//Date format: 06/07/2011

    $today = date("d/m/Y");
    $row = 0;
	$want_date = date("d/m/Y", strtotime("-1 days"));
    $count = 1;
   echo "Today is ".$today." We want ".$want_date."<BR>";
	
	$handle = fopen("daily","r");
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
		        $row++;
		$longdate = $data[5];
        $split = explode(" ", $longdate);
		$got_date = $split[0];
	    echo "<BR>Parsed row ".$row."Want :".$want_date." Got:".$got_date;

		if ($got_date == $want_date) {
		$handle2=  fopen("prescription_skus.csv","r");
		$data2= fgetcsv($handle2, 1000, ",");  
		//what we got in daily at this point (one value)
		$got_sku = $data [0];
		echo " Date Matched...Checking against SKUs for prescription products for ".$got_sku."...<BR>";

        //parse each line 
		

			while (($data = fgetcsv($handle2, 1000, ",")) !== FALSE) {
								for ($i = 0, $j = count($data2); $i < $j; $i++) {

			$want_sku = $data2[0];
echo "need:".$want_sku." have: ".$got_sku."<BR>";			
				
				
				if ($want_sku [i] ==  $got_sku [1]) {
					echo "...Match found, writing line..";
					$out_file = fopen('out.csv','w');
					fwrite ($out_file,print_r($data, true));
					echo "..line written <BR>";}
				else {echo "X";}				
				$count++;			

				}
			}
		
		}
	}
fclose($outfile); 
fclose($handle1);
fclose($handle2);


	?>
</body>
</html>
Now working, although I have yet to get a positive match to see if it writes to the file.

Last edited by suse_nerd; 07-08-2011 at 04:16 AM.
 
Old 07-07-2011, 09:39 AM   #4
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Can't you run it with test data? Like, edit daily and put in yesterday's date where it would normally appear and see if the script works. You would just have to ensure that "daily" downloads properly, and you can be fairly sure that the script works.

However, if you consider the problem solved, could you mark it as such using the thread tools please? Thank you
 
Old 07-08-2011, 04:15 AM   #5
suse_nerd
Member
 
Registered: May 2008
Distribution: SuSe
Posts: 50

Original Poster
Rep: Reputation: 15
Hi Snark

Our developers are letting me download the file now, so I can give it a test.

It's not actually resolved as it seems when a match is found, it doesn't iterate properly through every line of prescription_skus.csv and properly identify a match, it gets stuck on the first line.

This part is meant to iterate through the lines but doesn't
It also doesn't output the whole line to the file, only something like this

Quote:
Array
(
[0] => 30182701
)
Code:
for ($i = 0, $j = count($data2); $i < $j; $i++) {
...
...
$count++;}
I've just updated the code with a fix I thought should work, but it doesn't seem to.

Last edited by suse_nerd; 07-08-2011 at 04:19 AM.
 
Old 07-08-2011, 04:29 AM   #6
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
This line looks weird:

PHP Code:
fwrite ($out_file,print_r($datatrue)); 
If it should write the line as text, change it to something like:

PHP Code:
fwrite ($out_file,join(" ",$data)."\n"); 
Also, this line:

PHP Code:
$out_file fopen('out.csv','w'); 
This empties the file on each write, so you'd only get the last write in the file. The bash scripts appends, so change it to:

PHP Code:
$out_file fopen('out.csv','a'); 
 
Old 07-11-2011, 04:10 AM   #7
suse_nerd
Member
 
Registered: May 2008
Distribution: SuSe
Posts: 50

Original Poster
Rep: Reputation: 15
Interesting change, but it doesn't fix the problem that the lines aren't matching and that it doesn't parse the prescription_skus.csv file properly - it's stuck on the first line and doesn't go to the second line. The file is just like this
Quote:
48934934
43987439
28372837
12839293
 
Old 07-11-2011, 05:36 AM   #8
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hmm. The PHP code you posted wasn't very easy to read/understand. Based on the bash script, I think something like this is easier:

PHP Code:
$date date("d/m/Y"strtotime("-1 days"));
$skus file("prescription_skus.csv");
$data file("daily");
foreach (
$data as $line) {
    
$line trim($line);
    
$words explode(",",$line);
    
$word6 trim($words[5]);
    
$word1 trim($words[0]);
    if (
$word6 == $date) {
        foreach (
$skus as $sku) {
            if (
strpos($sku,$word1) !== false) {
                
file_put_contents("out.csv",$line."\n",FILE_APPEND);
            }
        }
    }

Not tested at all, but I think this is easier to follow. Is this the logic you want?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
converting a py script to bash saawan Linux - Software 2 05-06-2009 11:56 AM
Converting a Script from SHELL to PHP gcclinux Programming 3 02-21-2008 05:51 AM
converting bash script to csh DJOtaku Programming 8 02-13-2006 05:35 AM
bash script for converting ps to pdf juergenkemeter Linux - General 3 10-10-2005 04:35 PM
converting shell script to php script ? ibro Linux - General 6 05-24-2004 05:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration