Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I use a Php script which is actually for sending sms from the terminal using an online sms service 160by2

What it does is opens the terminal,asks for the number then the sms and it is sent...

Now i want the text in that sms to be copied to a new file everytime.

for example like text1.txt then text2.txt and it goes on incrementing every time.

If its not possible from php script then may be i can rite a shell script that first executes the php script and then makes the new file with some command and copies the text in the sms into that file.

I want to know that command( i guess pipelining is also used) and also,how to make the file name different everytime?

I don't know shell scripting.

but in a normal programing language,like c/c++/java,the way would be a loop in which a variable is incremented everytime and then passed as a parameter to the name..is anything like this possible in shell scripting?

share|improve this question
    
... and this has what to do with Ubuntu? You need to post this on scripting ;) –  Rinzwind Jul 30 '12 at 18:52
    
@Rinzwind...this script runs in ubuntu...and the part i want is how to create a file to paste the content of terminal in it..which is normally done in shellscripting which is related to ubuntu...thats why :) ;) –  Nirmik Jul 30 '12 at 18:56
    
shell script just means that it is written in bourne or bash shell. you can add a shebang to your php script too and chmod it and you can execute it like any other executable file. you need to be more specific if you want a certain bash feature explained. –  Janus Troelsen Jul 30 '12 at 19:09
    
@Ysangkok...the php file is already executable...thats how i execute it in the terminal..m confused about whether the "make a txtfile and copy the conents into it" should be implemented in the php script or through a shell script..and in either case..how.. –  Nirmik Jul 30 '12 at 19:12
    
in php it's pretty easy, you can use file_put_contents(). it's easy in bash too, i'll show you shortly –  Janus Troelsen Jul 30 '12 at 19:12
show 3 more comments

2 Answers

up vote 1 down vote accepted
+50

Your script should be modified to do two additional things:

  1. Generate a unique filename for the text message (based on date, time, etc.)
  2. Write the number $phno and message $msg to this file

We can do the former using the PHP date function, and the latter using the file_put_contents function.

All you need to do is insert these two lines of code after the phone number and message have been entered:

$textfilename="text ".date("Y-m-d His").".txt";
file_put_contents ($textfilename, $phno."\n".$msg."\n");
  • Sample output, in file text 2012-08-21 195239.txt:

    1800LOLCATS
    ICANHAZCHEEZBURGER?
    
share|improve this answer
    
i'll add those lines in the same php script m using? and where will these files be created? how do i define a path to them? –  Nirmik Aug 22 '12 at 11:13
    
Yes, same PHP script. They are created in the same directory as the script. To define a path, just add the full path in front of "$textfilename"s value. –  izx Aug 22 '12 at 12:59
    
adding the path in this fashion- $textfilename=/home/username/folder/"text"... or $textfilename=/home/username/folder"text"... did not work...the script doesnt run at all! it exits..and als this file was not created in the same one as the script...it was created under /home/nirmik whereas my script is under /home/nirmik/Apps –  Nirmik Aug 22 '12 at 18:02
    
@Nirmik, it needs to be quoted, e.g. $textfilename="/home/username/folder/text ".date..." –  izx Aug 22 '12 at 18:44
    
okay done...just one more thing...can i have only the hour and minutes in the time part? –  Nirmik Aug 22 '12 at 20:46
add comment

here's a php script that writes a file named using the unix timestamp:

#!/usr/bin/env php
<?php
file_put_contents("file" . ((int) microtime(true)) . ".txt", file_get_contents("/proc/cpuinfo"));
?>

if you want fill the file from i.e. stdin, change /proc/cpuinfo to php://stdin

here's a shell script that does the same as the original php script

#!/bin/sh
cat /proc/cpuinfo | tee "file`date +%s`.txt" > /dev/null

you can chmod +x both of these and execute them

share|improve this answer
    
in the php script,where will this file be created? and how do i specify a location to it if i want to? –  Nirmik Jul 30 '12 at 19:20
    
the file will be created in the working directory of the script. if you want another directory, just prepend it as usual. i.e. you can replace "file" ... with "/tmp/file" ... and it is always going to be saved in /tmp –  Janus Troelsen Jul 30 '12 at 19:32
    
well it has created a weird txt file- paste.ubuntu.com/1120131 –  Nirmik Jul 30 '12 at 19:49
    
i mean i added that line to the earlier php script... –  Nirmik Jul 30 '12 at 19:50
    
the processor information you see is content from /proc/cpuinfo. if you want other content in your log file, just change the path. if you want to determine what to write from the STDOUT from another script, you can for example replace "cat /proc/cpuinfo" with "./myscript.sh" and the output of that script will go to your log file –  Janus Troelsen Jul 30 '12 at 19:51
show 6 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.