0

How can I pass in a .txt file from bash script to a PHP process. I am trying as below but I have had no luck and help so far.

echo "$some100linestring" | tee log.txt
php /path/to/php/job.php --log_string=log.txt

Can someone please help. Thanks in advance.

oyeesh
  • 551
  • 9
  • 21
  • 2
    Why not use `cat textfile.txt` instead of `echo`? Then, for instance, have a look at this question: [Reading line by line from STDIN](https://stackoverflow.com/questions/11968244/reading-line-by-line-from-stdin) – Javier Elices Feb 12 '18 at 21:55
  • 1
    I think you might want to explain your purpose a little better. Your title talks about passing a text file, your example shows passing a filename. What exactly are you trying to do, and what is going wrong, precisely? – Stratadox Feb 12 '18 at 21:55
  • @Stratadox Apologies for not being clear. I am trying to pass the .txt file(I need a file as I had issues with passing a very large string) to a php job. In the job I have a function to `file_get_contents` from the txt file and parse each line to extract the information I need. I am unable to find out the syntax to pass a file rather filename. – oyeesh Feb 12 '18 at 22:11
  • 2
    How does the PHP script read the input? Are you possibly looking for something like `php script.php < file.txt`? This would allow the script to read the file from STDIN. – lxg Feb 12 '18 at 22:15
  • @lxg I was trying to use `file_get_contents` but as you mentioned, I am now able to use STDIN. Thanks a lot! – oyeesh Feb 12 '18 at 22:24
  • @Shrav: Glad to help. I’ve posted it as an answer so that others don’t need to come here for a seemingly unanswered question. – lxg Feb 12 '18 at 22:30

1 Answers1

0

If you want to pass the file content via STDIN, you can use the following syntax on the shell:

php /path/to/php/job.php < log.txt

Or, if log.txt is the result of some other operation, you can pipe it directly to the script:

echo "$some100linestring" | php /path/to/php/job.php
lxg
  • 12,375
  • 12
  • 51
  • 73