A bash scripting question

I have a bash scripting question that has really bothered me; First i want to list files in a directory with a command - no prob Second - i want to get the name of the file - no problem Third i want to read the content of the file - no problem Fourth i want to extract some information , column and rows from the file - no problem My Idea:
My idea is to receive sms and forward to an email - working file but only one at a time! #script 1 second is whenever a reply is sent from the email address "x" with subject "$CELLPHONE $MESSAGE" the script is called by clone to send the sms to user $cellphone - working file but only one at a time! #script 2
*Problem:* The directory contains multiple files with dynamic names (generated by system) some properties being date,time and cellphone number The problem is i don't know how to read multiple files name, read content to a file for processing at a go and output the results into another file using bash. No 2 - i want the *filename* of the other files....in the same directory and each file to be called one at a time. take example cd /tmp -rw-r--r-- 1 root root 0 Jun 16 12:42 a.2012.1242pm -rw-r--r-- 1 root root 0 Jun 16 12:42 b.2012.1243pm -rw-r--r-- 1 root root 0 Jun 16 12:42 c.2012.1244pm -rw-r--r-- 1 root root 0 Jun 16 12:42 d.2012.1245pm -rw-r--r-- 1 root root 0 Jun 16 12:42 e.2012.1246pm bear in mind the files are generated randomly and have no control over that :)- so to speak for question purposes I have the below script to read one line at a time and move what is read to another folder - not good practice. I would want to retain the file in same folder but my bash script should know that the file is read! How do i perfect this: This reads a file at a time and sends an email but if multiple are received at a go - all the rest iregardless are moved to the tmp2 folder. #!/bin/bash cd /tmp for f in "`ls`"; do cat $f | mail -s "$f" lixton (ati) gmail.com mv /tmp/* /tmp2 done *Now for reading an email reply file from above:* Well this will send BUT only one at a time and if multiple emails are received at a go - all the others are deleted! How do i perfect this.... #!/bin/bash for i in 30 (corresponds to a linux user email box line 30 where subject is) do awk -v a=$i 'NR==a {print $0}' /var/spool/mail/root > testfile2 cat /dev/null > /var/spool/mail/root echo `cat /root/testfile2` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' testfile2` Would possibly have VIRTUAL users where each has a mailbox qmail format. Help.... Wilson.

I have a bash scripting question that has really bothered me; First i want to list files in a directory with a command - no prob Second - i want to get the name of the file - no problem Third i want to read the content of the file - no problem Fourth i want to extract some information , column and rows from the file - no problem My Idea:
My idea is to receive sms and forward to an email - working fine but only one at a time! #script 1 second is whenever a reply is sent from the email address "x" with subject "$CELLPHONE $MESSAGE" the script is called by clone to send the sms to user $cellphone - working fine but only one at a time! #script 2
*Problem:* The directory contains multiple files with dynamic names (generated by system) some properties being date,time and cellphone number The problem is i don't know how to read multiple files name, read content to a file for processing at a go and output the results into another file using bash. No 2 - i want the *filename* of the other files....in the same directory and each file to be called one at a time. take example cd /tmp -rw-r--r-- 1 root root 0 Jun 16 12:42 a.2012.1242pm -rw-r--r-- 1 root root 0 Jun 16 12:42 b.2012.1243pm -rw-r--r-- 1 root root 0 Jun 16 12:42 c.2012.1244pm -rw-r--r-- 1 root root 0 Jun 16 12:42 d.2012.1245pm -rw-r--r-- 1 root root 0 Jun 16 12:42 e.2012.1246pm bear in mind the files are generated randomly and have no control over that :)- so to speak for question purposes I have the below script to read one line at a time and move what is read to another folder - not good practice. I would want to retain the file in same folder but my bash script should know that the file is read! How do i perfect this: This reads a file at a time and sends an email but if multiple are received at a go - all the rest regardless are moved to the tmp2 folder. #!/bin/bash cd /tmp for f in "`ls`"; do cat $f | mail -s "$f" lixton (ati) gmail.com mv /tmp/* /tmp2 done *Now for reading an email reply file from above:* Well this will send BUT only one at a time and if multiple emails are received at a go - all the others are deleted! How do i perfect this.... #!/bin/bash for i in 30 (corresponds to a linux user email box line 30 where subject is) do awk -v a=$i 'NR==a {print $0}' /var/spool/mail/root > testfile2 cat /dev/null > /var/spool/mail/root echo `cat /root/testfile2` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' testfile2` Would possibly have VIRTUAL users where each has a mailbox qmail format. Help.... Wilson.

On Sat, Jun 16, 2012 at 12:57 PM, Thuo Wilson <lixton@gmail.com> wrote:
I have a bash scripting question that has really bothered me;
First i want to list files in a directory with a command - no prob Second - i want to get the name of the file - no problem Third i want to read the content of the file - no problem Fourth i want to extract some information , column and rows from the file - no problem
My Idea:
My idea is to receive sms and forward to an email - working file but only one at a time! #script 1 second is whenever a reply is sent from the email address "x" with subject "$CELLPHONE $MESSAGE" the script is called by clone to send the sms to user $cellphone - working file but only one at a time! #script 2
*Problem:* The directory contains multiple files with dynamic names (generated by system) some properties being date,time and cellphone number The problem is i don't know how to read multiple files name, read content to a file for processing at a go and output the results into another file using bash. No 2 - i want the *filename* of the other files....in the same directory and each file to be called one at a time.
take example
cd /tmp
-rw-r--r-- 1 root root 0 Jun 16 12:42 a.2012.1242pm -rw-r--r-- 1 root root 0 Jun 16 12:42 b.2012.1243pm -rw-r--r-- 1 root root 0 Jun 16 12:42 c.2012.1244pm -rw-r--r-- 1 root root 0 Jun 16 12:42 d.2012.1245pm -rw-r--r-- 1 root root 0 Jun 16 12:42 e.2012.1246pm
bear in mind the files are generated randomly and have no control over that :)- so to speak for question purposes
I have the below script to read one line at a time and move what is read to another folder - not good practice. I would want to retain the file in same folder but my bash script should know that the file is read!
How do i perfect this: This reads a file at a time and sends an email but if multiple are received at a go - all the rest iregardless are moved to the tmp2 folder.
#!/bin/bash cd /tmp for f in "`ls`"; do cat $f | mail -s "$f" lixton (ati) gmail.com mv /tmp/* /tmp2
^^^^^^^^^^^^^^^^^^^^^^^^ Replace this line. mv /tmp/$f /tmp2/ You could perfect that by appending a timestamp to /tmp2/$f so that there is no name clash the next day :-)
done
*Now for reading an email reply file from above:* Well this will send BUT only one at a time and if multiple emails are received at a go - all the others are deleted! How do i perfect this....
#!/bin/bash for i in 30 (corresponds to a linux user email box line 30 where subject is) do awk -v a=$i 'NR==a {print $0}' /var/spool/mail/root > testfile2 cat /dev/null > /var/spool/mail/root echo `cat /root/testfile2` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' testfile2`
Use the same idea as above. Appending a timestamp will make files unique.
Would possibly have VIRTUAL users where each has a mailbox qmail format.
Qmail? :-) -- Best regards, Odhiambo WASHINGTON, Nairobi,KE +254733744121/+254722743223 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ I can't hear you -- I'm using the scrambler.

On 16 June 2012 13:28, Odhiambo Washington <odhiambo@gmail.com> wrote:
On Sat, Jun 16, 2012 at 12:57 PM, Thuo Wilson <lixton@gmail.com> wrote:
I have a bash scripting question that has really bothered me;
First i want to list files in a directory with a command - no prob Second - i want to get the name of the file - no problem Third i want to read the content of the file - no problem Fourth i want to extract some information , column and rows from the file - no problem
My Idea:
My idea is to receive sms and forward to an email - working file but only one at a time! #script 1 second is whenever a reply is sent from the email address "x" with subject "$CELLPHONE $MESSAGE" the script is called by clone to send the sms to user $cellphone - working file but only one at a time! #script 2
*Problem:* The directory contains multiple files with dynamic names (generated by system) some properties being date,time and cellphone number The problem is i don't know how to read multiple files name, read content to a file for processing at a go and output the results into another file using bash. No 2 - i want the *filename* of the other files....in the same directory and each file to be called one at a time.
take example
cd /tmp
-rw-r--r-- 1 root root 0 Jun 16 12:42 a.2012.1242pm -rw-r--r-- 1 root root 0 Jun 16 12:42 b.2012.1243pm -rw-r--r-- 1 root root 0 Jun 16 12:42 c.2012.1244pm -rw-r--r-- 1 root root 0 Jun 16 12:42 d.2012.1245pm -rw-r--r-- 1 root root 0 Jun 16 12:42 e.2012.1246pm
bear in mind the files are generated randomly and have no control over that :)- so to speak for question purposes
I have the below script to read one line at a time and move what is read to another folder - not good practice. I would want to retain the file in same folder but my bash script should know that the file is read!
How do i perfect this: This reads a file at a time and sends an email but if multiple are received at a go - all the rest iregardless are moved to the tmp2 folder.
#!/bin/bash cd /tmp for f in "`ls`"; do cat $f | mail -s "$f" lixton (ati) gmail.com mv /tmp/* /tmp2
^^^^^^^^^^^^^^^^^^^^^^^^ Replace this line.
mv /tmp/$f /tmp2/
You could perfect that by appending a timestamp to /tmp2/$f so that there is no name clash the next day :-)
Hi Wash, That could only deal with only one issue. Any files that comes later that when the dir files are listed. #!/bin/bash cd /tmp for f in "`ls`"; do cat $f | mail -s "$f" lixton (ati) gmail.com mv /tmp/$f* /tmp2 done Looking at example above... if multiple files are listed, only one file (1st one is read) and the rest "only content is read using "cat" Wilson
done
*Now for reading an email reply file from above:* Well this will send BUT only one at a time and if multiple emails are received at a go - all the others are deleted! How do i perfect this....
#!/bin/bash for i in 30 (corresponds to a linux user email box line 30 where subject is) do awk -v a=$i 'NR==a {print $0}' /var/spool/mail/root > testfile2 cat /dev/null > /var/spool/mail/root echo `cat /root/testfile2` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' testfile2`
Use the same idea as above. Appending a timestamp will make files unique.
Well if i want the SMS sent, i must read the conntent. timestamp may not help if i can be able to read all the files and perse the output to diffrent files or even one for sms delivery :)
Would possibly have VIRTUAL users where each has a mailbox qmail format.
Qmail? :-)
-- Best regards, Odhiambo WASHINGTON, Nairobi,KE +254733744121/+254722743223 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ I can't hear you -- I'm using the scrambler.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

On Sat, Jun 16, 2012 at 1:38 PM, Thuo Wilson <lixton@gmail.com> wrote:
On 16 June 2012 13:28, Odhiambo Washington <odhiambo@gmail.com> wrote:
On Sat, Jun 16, 2012 at 12:57 PM, Thuo Wilson <lixton@gmail.com> wrote:
I have a bash scripting question that has really bothered me;
First i want to list files in a directory with a command - no prob Second - i want to get the name of the file - no problem Third i want to read the content of the file - no problem Fourth i want to extract some information , column and rows from the file - no problem
My Idea:
My idea is to receive sms and forward to an email - working file but only one at a time! #script 1 second is whenever a reply is sent from the email address "x" with subject "$CELLPHONE $MESSAGE" the script is called by clone to send the sms to user $cellphone - working file but only one at a time! #script 2
*Problem:* The directory contains multiple files with dynamic names (generated by system) some properties being date,time and cellphone number The problem is i don't know how to read multiple files name, read content to a file for processing at a go and output the results into another file using bash. No 2 - i want the *filename* of the other files....in the same directory and each file to be called one at a time.
take example
cd /tmp
-rw-r--r-- 1 root root 0 Jun 16 12:42 a.2012.1242pm -rw-r--r-- 1 root root 0 Jun 16 12:42 b.2012.1243pm -rw-r--r-- 1 root root 0 Jun 16 12:42 c.2012.1244pm -rw-r--r-- 1 root root 0 Jun 16 12:42 d.2012.1245pm -rw-r--r-- 1 root root 0 Jun 16 12:42 e.2012.1246pm
bear in mind the files are generated randomly and have no control over that :)- so to speak for question purposes
I have the below script to read one line at a time and move what is read to another folder - not good practice. I would want to retain the file in same folder but my bash script should know that the file is read!
How do i perfect this: This reads a file at a time and sends an email but if multiple are received at a go - all the rest iregardless are moved to the tmp2 folder.
#!/bin/bash cd /tmp for f in "`ls`"; do cat $f | mail -s "$f" lixton (ati) gmail.com mv /tmp/* /tmp2
^^^^^^^^^^^^^^^^^^^^^^^^ Replace this line.
mv /tmp/$f /tmp2/
You could perfect that by appending a timestamp to /tmp2/$f so that there is no name clash the next day :-)
Hi Wash,
That could only deal with only one issue. Any files that comes later that when the dir files are listed.
#!/bin/bash cd /tmp for f in "`ls`"; do cat $f | mail -s "$f" lixton (ati) gmail.com mv /tmp/$f* /tmp2 done
Why do use the asterisk in there? $f* ?
Looking at example above... if multiple files are listed, only one file (1st one is read) and the rest "only content is read using "cat"
In all those files, how many matches do you get when parsing for the content you want? For instance: find . -type f -exec grep -li 'SOME_STRING' {} \; If only 1 file matches, then you can use substitution to get the file, perhaps: for f in ` find . -type f -exec grep -li 'SOME_STRING' {} \;`; do cat $f ....
Wilson
done
*Now for reading an email reply file from above:* Well this will send BUT only one at a time and if multiple emails are received at a go - all the others are deleted! How do i perfect this....
#!/bin/bash for i in 30 (corresponds to a linux user email box line 30 where subject is) do awk -v a=$i 'NR==a {print $0}' /var/spool/mail/root > testfile2 cat /dev/null > /var/spool/mail/root echo `cat /root/testfile2` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' testfile2`
Use the same idea as above. Appending a timestamp will make files unique.
Well if i want the SMS sent, i must read the conntent. timestamp may not help if i can be able to read all the files and perse the output to diffrent files or even one for sms delivery :)
I am not sure I understand you, and as such I may come out as more misleading :) What MTA do you use BTW? Postfix? I think I have somewhere a script that can help you in this endeavor, but I only used it with Exim. -- Best regards, Odhiambo WASHINGTON, Nairobi,KE +254733744121/+254722743223 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ I can't hear you -- I'm using the scrambler.

On 16 June 2012 23:38, Odhiambo Washington <odhiambo@gmail.com> wrote:
On Sat, Jun 16, 2012 at 1:38 PM, Thuo Wilson <lixton@gmail.com> wrote:
On 16 June 2012 13:28, Odhiambo Washington <odhiambo@gmail.com> wrote:
On Sat, Jun 16, 2012 at 12:57 PM, Thuo Wilson <lixton@gmail.com> wrote:
I have a bash scripting question that has really bothered me;
First i want to list files in a directory with a command - no prob Second - i want to get the name of the file - no problem Third i want to read the content of the file - no problem Fourth i want to extract some information , column and rows from the file - no problem
My Idea:
My idea is to receive sms and forward to an email - working file but only one at a time! #script 1 second is whenever a reply is sent from the email address "x" with subject "$CELLPHONE $MESSAGE" the script is called by clone to send the sms to user $cellphone - working file but only one at a time! #script 2
*Problem:* The directory contains multiple files with dynamic names (generated by system) some properties being date,time and cellphone number The problem is i don't know how to read multiple files name, read content to a file for processing at a go and output the results into another file using bash. No 2 - i want the *filename* of the other files....in the same directory and each file to be called one at a time.
take example
cd /tmp
-rw-r--r-- 1 root root 0 Jun 16 12:42 a.2012.1242pm -rw-r--r-- 1 root root 0 Jun 16 12:42 b.2012.1243pm -rw-r--r-- 1 root root 0 Jun 16 12:42 c.2012.1244pm -rw-r--r-- 1 root root 0 Jun 16 12:42 d.2012.1245pm -rw-r--r-- 1 root root 0 Jun 16 12:42 e.2012.1246pm
bear in mind the files are generated randomly and have no control over that :)- so to speak for question purposes
I have the below script to read one line at a time and move what is read to another folder - not good practice. I would want to retain the file in same folder but my bash script should know that the file is read!
How do i perfect this: This reads a file at a time and sends an email but if multiple are received at a go - all the rest iregardless are moved to the tmp2 folder.
#!/bin/bash cd /tmp for f in "`ls`"; do cat $f | mail -s "$f" lixton (ati) gmail.com mv /tmp/* /tmp2
^^^^^^^^^^^^^^^^^^^^^^^^ Replace this line.
mv /tmp/$f /tmp2/
You could perfect that by appending a timestamp to /tmp2/$f so that there is no name clash the next day :-)
Hi Wash,
That could only deal with only one issue. Any files that comes later that when the dir files are listed.
#!/bin/bash cd /tmp for f in "`ls`"; do cat $f | mail -s "$f" lixton (ati) gmail.com mv /tmp/$f* /tmp2 done
Why do use the asterisk in there? $f* ?
Looking at example above... if multiple files are listed, only one file (1st one is read) and the rest "only content is read using "cat"
In all those files, how many matches do you get when parsing for the content you want?
For instance:
find . -type f -exec grep -li 'SOME_STRING' {} \;
If only 1 file matches, then you can use substitution to get the file, perhaps:
for f in ` find . -type f -exec grep -li 'SOME_STRING' {} \;`; do cat $f ....
This will output content of the file :(
Wilson
done
*Now for reading an email reply file from above:* Well this will send BUT only one at a time and if multiple emails are received at a go - all the others are deleted! How do i perfect this....
#!/bin/bash for i in 30 (corresponds to a linux user email box line 30 where subject is) do awk -v a=$i 'NR==a {print $0}' /var/spool/mail/root > testfile2 cat /dev/null > /var/spool/mail/root echo `cat /root/testfile2` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' testfile2`
Use the same idea as above. Appending a timestamp will make files unique.
Well if i want the SMS sent, i must read the conntent. timestamp may not help if i can be able to read all the files and perse the output to diffrent files or even one for sms delivery :)
I am not sure I understand you, and as such I may come out as more misleading :)
What MTA do you use BTW? Postfix?
- i can use postfix/sendmail.
I think I have somewhere a script that can help you in this endeavor, but I only used it with Exim.
may i look into the script, may be it has what i want...
-- Best regards, Odhiambo WASHINGTON, Nairobi,KE +254733744121/+254722743223 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ I can't hear you -- I'm using the scrambler.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

this is just a pointer: whichever way you do it, keeping files in the same directory means the time consumed by the filter will increase as files increase thus making the process slower and slower by every new file -- On Mon, Jun 18, 2012 at 9:41 AM, Thuo Wilson <lixton@gmail.com> wrote:
On 16 June 2012 23:38, Odhiambo Washington <odhiambo@gmail.com> wrote:
On Sat, Jun 16, 2012 at 1:38 PM, Thuo Wilson <lixton@gmail.com> wrote:
On 16 June 2012 13:28, Odhiambo Washington <odhiambo@gmail.com> wrote:
On Sat, Jun 16, 2012 at 12:57 PM, Thuo Wilson <lixton@gmail.com> wrote:
I have a bash scripting question that has really bothered me;
First i want to list files in a directory with a command - no prob Second - i want to get the name of the file - no problem Third i want to read the content of the file - no problem Fourth i want to extract some information , column and rows from the file - no problem
My Idea:
My idea is to receive sms and forward to an email - working file but only one at a time! #script 1 second is whenever a reply is sent from the email address "x" with subject "$CELLPHONE $MESSAGE" the script is called by clone to send the sms to user $cellphone - working file but only one at a time! #script 2
*Problem:* The directory contains multiple files with dynamic names (generated by system) some properties being date,time and cellphone number The problem is i don't know how to read multiple files name, read content to a file for processing at a go and output the results into another file using bash. No 2 - i want the *filename* of the other files....in the same directory and each file to be called one at a time.
take example
cd /tmp
-rw-r--r-- 1 root root 0 Jun 16 12:42 a.2012.1242pm -rw-r--r-- 1 root root 0 Jun 16 12:42 b.2012.1243pm -rw-r--r-- 1 root root 0 Jun 16 12:42 c.2012.1244pm -rw-r--r-- 1 root root 0 Jun 16 12:42 d.2012.1245pm -rw-r--r-- 1 root root 0 Jun 16 12:42 e.2012.1246pm
bear in mind the files are generated randomly and have no control over that :)- so to speak for question purposes
I have the below script to read one line at a time and move what is read to another folder - not good practice. I would want to retain the file in same folder but my bash script should know that the file is read!
How do i perfect this: This reads a file at a time and sends an email but if multiple are received at a go - all the rest iregardless are moved to the tmp2 folder.
#!/bin/bash cd /tmp for f in "`ls`"; do cat $f | mail -s "$f" lixton (ati) gmail.com mv /tmp/* /tmp2
^^^^^^^^^^^^^^^^^^^^^^^^ Replace this line.
mv /tmp/$f /tmp2/
You could perfect that by appending a timestamp to /tmp2/$f so that there is no name clash the next day :-)
Hi Wash,
That could only deal with only one issue. Any files that comes later that when the dir files are listed.
#!/bin/bash cd /tmp for f in "`ls`"; do cat $f | mail -s "$f" lixton (ati) gmail.com mv /tmp/$f* /tmp2 done
Why do use the asterisk in there? $f* ?
Looking at example above... if multiple files are listed, only one file (1st one is read) and the rest "only content is read using "cat"
In all those files, how many matches do you get when parsing for the content you want?
For instance:
find . -type f -exec grep -li 'SOME_STRING' {} \;
If only 1 file matches, then you can use substitution to get the file, perhaps:
for f in ` find . -type f -exec grep -li 'SOME_STRING' {} \;`; do cat $f ....
This will output content of the file :(
Wilson
done
*Now for reading an email reply file from above:* Well this will send BUT only one at a time and if multiple emails are received at a go - all the others are deleted! How do i perfect this....
#!/bin/bash for i in 30 (corresponds to a linux user email box line 30 where subject is) do awk -v a=$i 'NR==a {print $0}' /var/spool/mail/root > testfile2 cat /dev/null > /var/spool/mail/root echo `cat /root/testfile2` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' testfile2`
Use the same idea as above. Appending a timestamp will make files unique.
Well if i want the SMS sent, i must read the conntent. timestamp may not help if i can be able to read all the files and perse the output to diffrent files or even one for sms delivery :)
I am not sure I understand you, and as such I may come out as more misleading :)
What MTA do you use BTW? Postfix?
- i can use postfix/sendmail.
I think I have somewhere a script that can help you in this endeavor, but I only used it with Exim.
may i look into the script, may be it has what i want...
-- Best regards, Odhiambo WASHINGTON, Nairobi,KE +254733744121/+254722743223 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ I can't hear you -- I'm using the scrambler.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

- as @wash said above, only move the specific file - not everything which is denoted by "*" so this should look like mv /tmp/$f /tmp2 - instead of reading mail spool, you can pipe incoming mails to a script either by using procmailrc or by using /etc/aliases file if using postfix. this could look like this: smsemail: "|/usr/libexec/postfix/trap.py" smsemail being the account receiving mails. if your script is elsewhere you will need to tell SELinux of that otherwise it will fail ( thats if you have selinux) -- -erastus +254733725373 Nairobi Kenya On Mon, Jun 18, 2012 at 10:24 AM, gisho <gichuhie@gmail.com> wrote:
this is just a pointer: whichever way you do it, keeping files in the same directory means the time consumed by the filter will increase as files increase thus making the process slower and slower by every new file
--
On Mon, Jun 18, 2012 at 9:41 AM, Thuo Wilson <lixton@gmail.com> wrote:
On 16 June 2012 23:38, Odhiambo Washington <odhiambo@gmail.com> wrote:
On Sat, Jun 16, 2012 at 1:38 PM, Thuo Wilson <lixton@gmail.com> wrote:
On 16 June 2012 13:28, Odhiambo Washington <odhiambo@gmail.com> wrote:
On Sat, Jun 16, 2012 at 12:57 PM, Thuo Wilson <lixton@gmail.com>wrote:
I have a bash scripting question that has really bothered me;
First i want to list files in a directory with a command - no prob Second - i want to get the name of the file - no problem Third i want to read the content of the file - no problem Fourth i want to extract some information , column and rows from the file - no problem
My Idea: >My idea is to receive sms and forward to an email - working file but only one at a time! #script 1 >second is whenever a reply is sent from the email address "x" with subject "$CELLPHONE $MESSAGE" the script is called by clone to send the sms to user $cellphone - working file but only one at a time! #script 2
*Problem:* The directory contains multiple files with dynamic names (generated by system) some properties being date,time and cellphone number The problem is i don't know how to read multiple files name, read content to a file for processing at a go and output the results into another file using bash. No 2 - i want the *filename* of the other files....in the same directory and each file to be called one at a time.
take example
cd /tmp
-rw-r--r-- 1 root root 0 Jun 16 12:42 a.2012.1242pm -rw-r--r-- 1 root root 0 Jun 16 12:42 b.2012.1243pm -rw-r--r-- 1 root root 0 Jun 16 12:42 c.2012.1244pm -rw-r--r-- 1 root root 0 Jun 16 12:42 d.2012.1245pm -rw-r--r-- 1 root root 0 Jun 16 12:42 e.2012.1246pm
bear in mind the files are generated randomly and have no control over that :)- so to speak for question purposes
I have the below script to read one line at a time and move what is read to another folder - not good practice. I would want to retain the file in same folder but my bash script should know that the file is read!
How do i perfect this: This reads a file at a time and sends an email but if multiple are received at a go - all the rest iregardless are moved to the tmp2 folder.
#!/bin/bash cd /tmp for f in "`ls`"; do cat $f | mail -s "$f" lixton (ati) gmail.com mv /tmp/* /tmp2
^^^^^^^^^^^^^^^^^^^^^^^^ Replace this line.
mv /tmp/$f /tmp2/
You could perfect that by appending a timestamp to /tmp2/$f so that there is no name clash the next day :-)
Hi Wash,
That could only deal with only one issue. Any files that comes later that when the dir files are listed.
#!/bin/bash cd /tmp for f in "`ls`"; do cat $f | mail -s "$f" lixton (ati) gmail.com mv /tmp/$f* /tmp2 done
Why do use the asterisk in there? $f* ?
Looking at example above... if multiple files are listed, only one file (1st one is read) and the rest "only content is read using "cat"
In all those files, how many matches do you get when parsing for the content you want?
For instance:
find . -type f -exec grep -li 'SOME_STRING' {} \;
If only 1 file matches, then you can use substitution to get the file, perhaps:
for f in ` find . -type f -exec grep -li 'SOME_STRING' {} \;`; do cat $f ....
This will output content of the file :(
Wilson
done
*Now for reading an email reply file from above:* Well this will send BUT only one at a time and if multiple emails are received at a go - all the others are deleted! How do i perfect this....
#!/bin/bash for i in 30 (corresponds to a linux user email box line 30 where subject is) do awk -v a=$i 'NR==a {print $0}' /var/spool/mail/root > testfile2 cat /dev/null > /var/spool/mail/root echo `cat /root/testfile2` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' testfile2`
Use the same idea as above. Appending a timestamp will make files unique.
Well if i want the SMS sent, i must read the conntent. timestamp may not help if i can be able to read all the files and perse the output to diffrent files or even one for sms delivery :)
I am not sure I understand you, and as such I may come out as more misleading :)
What MTA do you use BTW? Postfix?
- i can use postfix/sendmail.
I think I have somewhere a script that can help you in this endeavor, but I only used it with Exim.
may i look into the script, may be it has what i want...
-- Best regards, Odhiambo WASHINGTON, Nairobi,KE +254733744121/+254722743223 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ I can't hear you -- I'm using the scrambler.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

On 18 June 2012 10:52, gisho <gichuhie@gmail.com> wrote:
- as @wash said above, only move the specific file - not everything which is denoted by "*" so this should look like mv /tmp/$f /tmp2
- instead of reading mail spool, you can pipe incoming mails to a script either by using procmailrc or by using /etc/aliases file if using postfix. this could look like this:
smsemail: "|/usr/libexec/postfix/trap.py"
Thanks Gisho, Thats is *part* of what i needed. I will use the alias. But note supposing am doing bulk sms - this may [not affirmative] fail due to numbers of emails < inflow... Second: i decided to change script to read as follows; simpler but doing better [?] Imagine i don't have the script called from alias. How do i read one line at a time in the file "email_to_sms" and exec the command to send the sms. That is my main issue Am sure its something simple i havent scratched my head enough! #!/bin/bash `grep Subject /var/spool/mail/sms > email_to_sms` cat /dev/null > /var/spool/mail/sms killall -9 gammu-smsd #since for some reason gammu-smsd is using use_lock and wont disable echo `cat email_to_sms` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms` sleep 15 #wait for the above to finish /etc/init.d/gammu-smsd start #start to continue checking any incoming sms cat email_to_sms >> email_to_sms2 #incase make a backup file cat /dev/null > email_to_sms #empty for no2 message in the line :) Wilson.
smsemail being the account receiving mails. if your script is elsewhere you will need to tell SELinux of that otherwise it will fail ( thats if you have selinux)
-- -erastus +254733725373 Nairobi Kenya
On Mon, Jun 18, 2012 at 10:24 AM, gisho <gichuhie@gmail.com> wrote:
this is just a pointer: whichever way you do it, keeping files in the same directory means the time consumed by the filter will increase as files increase thus making the process slower and slower by every new file
--
On Mon, Jun 18, 2012 at 9:41 AM, Thuo Wilson <lixton@gmail.com> wrote:
On 16 June 2012 23:38, Odhiambo Washington <odhiambo@gmail.com> wrote:
On Sat, Jun 16, 2012 at 1:38 PM, Thuo Wilson <lixton@gmail.com> wrote:
On 16 June 2012 13:28, Odhiambo Washington <odhiambo@gmail.com> wrote:
On Sat, Jun 16, 2012 at 12:57 PM, Thuo Wilson <lixton@gmail.com>wrote:
> I have a bash scripting question that has really bothered me; > > > First i want to list files in a directory with a command - no prob > Second - i want to get the name of the file - no problem > Third i want to read the content of the file - no problem > Fourth i want to extract some information , column and rows from the > file - no problem > > My Idea: > >My idea is to receive sms and forward to an email - working file > but only one at a time! #script 1 > >second is whenever a reply is sent from the email address "x" > with subject "$CELLPHONE $MESSAGE" the script is called by clone to send > the sms to user $cellphone - working file but only one at a time! #script > 2 > > > *Problem:* > The directory contains multiple files with dynamic names (generated > by system) some properties being date,time and cellphone number > The problem is i don't know how to read multiple files name, read > content to a file for processing at a go and output the results into > another file using bash. > No 2 - i want the *filename* of the other files....in the same > directory and each file to be called one at a time. > > take example > > cd /tmp > > -rw-r--r-- 1 root root 0 Jun 16 12:42 a.2012.1242pm > -rw-r--r-- 1 root root 0 Jun 16 12:42 b.2012.1243pm > -rw-r--r-- 1 root root 0 Jun 16 12:42 c.2012.1244pm > -rw-r--r-- 1 root root 0 Jun 16 12:42 d.2012.1245pm > -rw-r--r-- 1 root root 0 Jun 16 12:42 e.2012.1246pm > > bear in mind the files are generated randomly and have no control > over that :)- so to speak for question purposes > > I have the below script to read one line at a time and move what is > read to another folder - not good practice. I would want to retain the file > in same folder but my bash script should know that the file is read! > > How do i perfect this: This reads a file at a time and sends an > email but if multiple are received at a go - all the rest iregardless are > moved to the tmp2 folder. > > #!/bin/bash > cd /tmp > for f in "`ls`"; do > cat $f | mail -s "$f" lixton (ati) gmail.com > mv /tmp/* /tmp2 > ^^^^^^^^^^^^^^^^^^^^^^^^ Replace this line.
mv /tmp/$f /tmp2/
You could perfect that by appending a timestamp to /tmp2/$f so that there is no name clash the next day :-)
Hi Wash,
That could only deal with only one issue. Any files that comes later that when the dir files are listed.
#!/bin/bash cd /tmp for f in "`ls`"; do cat $f | mail -s "$f" lixton (ati) gmail.com mv /tmp/$f* /tmp2 done
Why do use the asterisk in there? $f* ?
Looking at example above... if multiple files are listed, only one file (1st one is read) and the rest "only content is read using "cat"
In all those files, how many matches do you get when parsing for the content you want?
For instance:
find . -type f -exec grep -li 'SOME_STRING' {} \;
If only 1 file matches, then you can use substitution to get the file, perhaps:
for f in ` find . -type f -exec grep -li 'SOME_STRING' {} \;`; do cat $f ....
This will output content of the file :(
Wilson
> done > > *Now for reading an email reply file from above:* > Well this will send BUT only one at a time and if multiple emails > are received at a go - all the others are deleted! > How do i perfect this.... > > #!/bin/bash > for i in 30 (corresponds to a linux user email box line 30 where > subject is) > do > awk -v a=$i 'NR==a {print $0}' /var/spool/mail/root > testfile2 > cat /dev/null > /var/spool/mail/root > echo `cat /root/testfile2` | /usr/bin/gammu --sendsms TEXT `awk > '{print $2}' testfile2` > > Use the same idea as above. Appending a timestamp will make files unique.
Well if i want the SMS sent, i must read the conntent. timestamp may not help if i can be able to read all the files and perse the output to diffrent files or even one for sms delivery :)
I am not sure I understand you, and as such I may come out as more misleading :)
What MTA do you use BTW? Postfix?
- i can use postfix/sendmail.
I think I have somewhere a script that can help you in this endeavor, but I only used it with Exim.
may i look into the script, may be it has what i want...
-- Best regards, Odhiambo WASHINGTON, Nairobi,KE +254733744121/+254722743223 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ I can't hear you -- I'm using the scrambler.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

On Mon, Jun 18, 2012 at 4:01 PM, Thuo Wilson <lixton@gmail.com> wrote:
On 18 June 2012 10:52, gisho <gichuhie@gmail.com> wrote:
- as @wash said above, only move the specific file - not everything which is denoted by "*" so this should look like mv /tmp/$f /tmp2
- instead of reading mail spool, you can pipe incoming mails to a script either by using procmailrc or by using /etc/aliases file if using postfix. this could look like this:
smsemail: "|/usr/libexec/postfix/trap.py"
Thanks Gisho, Thats is *part* of what i needed. I will use the alias. But note supposing am doing bulk sms - this may [not affirmative] fail due to numbers of emails < inflow... Second: i decided to change script to read as follows; simpler but doing better [?]
Imagine i don't have the script called from alias. How do i read one line at a time in the file "email_to_sms" and exec the command to send the sms. That is my main issue
Am sure its something simple i havent scratched my head enough!
#!/bin/bash `grep Subject /var/spool/mail/sms > email_to_sms` cat /dev/null > /var/spool/mail/sms killall -9 gammu-smsd #since for some reason gammu-smsd is using use_lock and wont disable echo `cat email_to_sms` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms` sleep 15 #wait for the above to finish /etc/init.d/gammu-smsd start #start to continue checking any incoming sms cat email_to_sms >> email_to_sms2 #incase make a backup file cat /dev/null > email_to_sms #empty for no2 message in the line :)
So the file email_to_sms is a text file containing some data you need to work on, each line at a time: for data in `cat email_to_sms`; do echo $data | /usr/bin/gammu $ARGS .....; done That will surely iterate through all the data in the file, one line at a time... You can do loop processing until there is nothing left, or always check if exists a file before you act on it.. However at this point, I suggest you work on a file which has been moved to another directory, so move the file first and reference it from where it is. Since the filenames seem to clash at some point (or why do you wven want to move the files?) then my initial suggestion stands - append the current timestamp value to the filename to make it unique. Please try and tell breakdown what this command line does: echo `cat email_to_sms` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms` Then again, if you need a program to rexec, there is no need to give it a SIGKILL (-9). Just do a SIGHUP (-1) So you can rexec gammu after it completes processing the first file while making it loop to look for the existence of another file in the parent directory ... and the loop continues... Wilson.
smsemail being the account receiving mails. if your script is elsewhere you will need to tell SELinux of that otherwise it will fail ( thats if you have selinux)
-- -erastus +254733725373 Nairobi Kenya
On Mon, Jun 18, 2012 at 10:24 AM, gisho <gichuhie@gmail.com> wrote:
this is just a pointer: whichever way you do it, keeping files in the same directory means the time consumed by the filter will increase as files increase thus making the process slower and slower by every new file
--
On Mon, Jun 18, 2012 at 9:41 AM, Thuo Wilson <lixton@gmail.com> wrote:
On 16 June 2012 23:38, Odhiambo Washington <odhiambo@gmail.com> wrote:
On Sat, Jun 16, 2012 at 1:38 PM, Thuo Wilson <lixton@gmail.com> wrote:
On 16 June 2012 13:28, Odhiambo Washington <odhiambo@gmail.com>wrote:
> > > On Sat, Jun 16, 2012 at 12:57 PM, Thuo Wilson <lixton@gmail.com>wrote: > >> I have a bash scripting question that has really bothered me; >> >> >> First i want to list files in a directory with a command - no prob >> Second - i want to get the name of the file - no problem >> Third i want to read the content of the file - no problem >> Fourth i want to extract some information , column and rows from >> the file - no problem >> >> My Idea: >> >My idea is to receive sms and forward to an email - working file >> but only one at a time! #script 1 >> >second is whenever a reply is sent from the email address "x" >> with subject "$CELLPHONE $MESSAGE" the script is called by clone to send >> the sms to user $cellphone - working file but only one at a time! #script >> 2 >> >> >> *Problem:* >> The directory contains multiple files with dynamic names (generated >> by system) some properties being date,time and cellphone number >> The problem is i don't know how to read multiple files name, read >> content to a file for processing at a go and output the results into >> another file using bash. >> No 2 - i want the *filename* of the other files....in the same >> directory and each file to be called one at a time. >> >> take example >> >> cd /tmp >> >> -rw-r--r-- 1 root root 0 Jun 16 12:42 a.2012.1242pm >> -rw-r--r-- 1 root root 0 Jun 16 12:42 b.2012.1243pm >> -rw-r--r-- 1 root root 0 Jun 16 12:42 c.2012.1244pm >> -rw-r--r-- 1 root root 0 Jun 16 12:42 d.2012.1245pm >> -rw-r--r-- 1 root root 0 Jun 16 12:42 e.2012.1246pm >> >> bear in mind the files are generated randomly and have no control >> over that :)- so to speak for question purposes >> >> I have the below script to read one line at a time and move what is >> read to another folder - not good practice. I would want to retain the file >> in same folder but my bash script should know that the file is read! >> >> How do i perfect this: This reads a file at a time and sends an >> email but if multiple are received at a go - all the rest iregardless are >> moved to the tmp2 folder. >> >> #!/bin/bash >> cd /tmp >> for f in "`ls`"; do >> cat $f | mail -s "$f" lixton (ati) gmail.com >> mv /tmp/* /tmp2 >> > ^^^^^^^^^^^^^^^^^^^^^^^^ Replace this line. >
> mv /tmp/$f /tmp2/ > > You could perfect that by appending a timestamp to /tmp2/$f so that > there is no name clash the next day :-) >
Hi Wash,
That could only deal with only one issue. Any files that comes later that when the dir files are listed.
#!/bin/bash cd /tmp for f in "`ls`"; do cat $f | mail -s "$f" lixton (ati) gmail.com mv /tmp/$f* /tmp2 done
Why do use the asterisk in there? $f* ?
Looking at example above... if multiple files are listed, only one file (1st one is read) and the rest "only content is read using "cat"
In all those files, how many matches do you get when parsing for the content you want?
For instance:
find . -type f -exec grep -li 'SOME_STRING' {} \;
If only 1 file matches, then you can use substitution to get the file, perhaps:
for f in ` find . -type f -exec grep -li 'SOME_STRING' {} \;`; do cat $f ....
This will output content of the file :(
Wilson
> > > > >> done >> >> *Now for reading an email reply file from above:* >> Well this will send BUT only one at a time and if multiple emails >> are received at a go - all the others are deleted! >> How do i perfect this.... >> >> #!/bin/bash >> for i in 30 (corresponds to a linux user email box line 30 where >> subject is) >> do >> awk -v a=$i 'NR==a {print $0}' /var/spool/mail/root > testfile2 >> cat /dev/null > /var/spool/mail/root >> echo `cat /root/testfile2` | /usr/bin/gammu --sendsms TEXT `awk >> '{print $2}' testfile2` >> >> > Use the same idea as above. Appending a timestamp will make files > unique. >
Well if i want the SMS sent, i must read the conntent. timestamp may not help if i can be able to read all the files and perse the output to diffrent files or even one for sms delivery :)
I am not sure I understand you, and as such I may come out as more misleading :)
What MTA do you use BTW? Postfix?
- i can use postfix/sendmail.
I think I have somewhere a script that can help you in this endeavor, but I only used it with Exim.
may i look into the script, may be it has what i want...
-- Best regards, Odhiambo WASHINGTON, Nairobi,KE +254733744121/+254722743223 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ I can't hear you -- I'm using the scrambler.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
-- Best regards, Odhiambo WASHINGTON, Nairobi,KE +254733744121/+254722743223 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ I can't hear you -- I'm using the scrambler.

just read the STDIN in python you could do this import sys if __name__=="__main__": message = sys.stdin.read() in perl: #!/usr/bin/perl -w my $message = <STDIN>; in bash: etc etc once you get the message you can go ahead to do whatever parsing you are doing. hint, hint: if the traffic is that much you can always use a queue like rabbitmq or even a db for that matter, in fact its highly recommended for those kind of jobs -- -erastus +254733725373 Nairobi Kenya On Mon, Jun 18, 2012 at 4:34 PM, Odhiambo Washington <odhiambo@gmail.com>wrote:
On Mon, Jun 18, 2012 at 4:01 PM, Thuo Wilson <lixton@gmail.com> wrote:
On 18 June 2012 10:52, gisho <gichuhie@gmail.com> wrote:
- as @wash said above, only move the specific file - not everything which is denoted by "*" so this should look like mv /tmp/$f /tmp2
- instead of reading mail spool, you can pipe incoming mails to a script either by using procmailrc or by using /etc/aliases file if using postfix. this could look like this:
smsemail: "|/usr/libexec/postfix/trap.py"
Thanks Gisho, Thats is *part* of what i needed. I will use the alias. But note supposing am doing bulk sms - this may [not affirmative] fail due to numbers of emails < inflow... Second: i decided to change script to read as follows; simpler but doing better [?]
Imagine i don't have the script called from alias. How do i read one line at a time in the file "email_to_sms" and exec the command to send the sms. That is my main issue
Am sure its something simple i havent scratched my head enough!
#!/bin/bash `grep Subject /var/spool/mail/sms > email_to_sms` cat /dev/null > /var/spool/mail/sms killall -9 gammu-smsd #since for some reason gammu-smsd is using use_lock and wont disable echo `cat email_to_sms` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms` sleep 15 #wait for the above to finish /etc/init.d/gammu-smsd start #start to continue checking any incoming sms cat email_to_sms >> email_to_sms2 #incase make a backup file cat /dev/null > email_to_sms #empty for no2 message in the line :)
So the file email_to_sms is a text file containing some data you need to work on, each line at a time:
for data in `cat email_to_sms`; do echo $data | /usr/bin/gammu $ARGS .....; done
That will surely iterate through all the data in the file, one line at a time... You can do loop processing until there is nothing left, or always check if exists a file before you act on it..
However at this point, I suggest you work on a file which has been moved to another directory, so move the file first and reference it from where it is. Since the filenames seem to clash at some point (or why do you wven want to move the files?) then my initial suggestion stands - append the current timestamp value to the filename to make it unique.
Please try and tell breakdown what this command line does:
echo `cat email_to_sms` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms`
Then again, if you need a program to rexec, there is no need to give it a SIGKILL (-9). Just do a SIGHUP (-1) So you can rexec gammu after it completes processing the first file while making it loop to look for the existence of another file in the parent directory ... and the loop continues...
Wilson.
smsemail being the account receiving mails. if your script is elsewhere you will need to tell SELinux of that otherwise it will fail ( thats if you have selinux)
-- -erastus +254733725373 Nairobi Kenya
On Mon, Jun 18, 2012 at 10:24 AM, gisho <gichuhie@gmail.com> wrote:
this is just a pointer: whichever way you do it, keeping files in the same directory means the time consumed by the filter will increase as files increase thus making the process slower and slower by every new file
--
On Mon, Jun 18, 2012 at 9:41 AM, Thuo Wilson <lixton@gmail.com> wrote:
On 16 June 2012 23:38, Odhiambo Washington <odhiambo@gmail.com> wrote:
On Sat, Jun 16, 2012 at 1:38 PM, Thuo Wilson <lixton@gmail.com>wrote:
> > > > On 16 June 2012 13:28, Odhiambo Washington <odhiambo@gmail.com>wrote: > >> >> >> On Sat, Jun 16, 2012 at 12:57 PM, Thuo Wilson <lixton@gmail.com>wrote: >> >>> I have a bash scripting question that has really bothered me; >>> >>> >>> First i want to list files in a directory with a command - no prob >>> Second - i want to get the name of the file - no problem >>> Third i want to read the content of the file - no problem >>> Fourth i want to extract some information , column and rows from >>> the file - no problem >>> >>> My Idea: >>> >My idea is to receive sms and forward to an email - working file >>> but only one at a time! #script 1 >>> >second is whenever a reply is sent from the email address "x" >>> with subject "$CELLPHONE $MESSAGE" the script is called by clone to send >>> the sms to user $cellphone - working file but only one at a >>> time! #script 2 >>> >>> >>> *Problem:* >>> The directory contains multiple files with dynamic names >>> (generated by system) some properties being date,time and cellphone number >>> The problem is i don't know how to read multiple files name, >>> read content to a file for processing at a go and output the results into >>> another file using bash. >>> No 2 - i want the *filename* of the other files....in the same >>> directory and each file to be called one at a time. >>> >>> take example >>> >>> cd /tmp >>> >>> -rw-r--r-- 1 root root 0 Jun 16 12:42 a.2012.1242pm >>> -rw-r--r-- 1 root root 0 Jun 16 12:42 b.2012.1243pm >>> -rw-r--r-- 1 root root 0 Jun 16 12:42 c.2012.1244pm >>> -rw-r--r-- 1 root root 0 Jun 16 12:42 d.2012.1245pm >>> -rw-r--r-- 1 root root 0 Jun 16 12:42 e.2012.1246pm >>> >>> bear in mind the files are generated randomly and have no >>> control over that :)- so to speak for question purposes >>> >>> I have the below script to read one line at a time and move what >>> is read to another folder - not good practice. I would want to retain the >>> file in same folder but my bash script should know that the file is read! >>> >>> How do i perfect this: This reads a file at a time and sends an >>> email but if multiple are received at a go - all the rest iregardless are >>> moved to the tmp2 folder. >>> >>> #!/bin/bash >>> cd /tmp >>> for f in "`ls`"; do >>> cat $f | mail -s "$f" lixton (ati) gmail.com >>> mv /tmp/* /tmp2 >>> >> ^^^^^^^^^^^^^^^^^^^^^^^^ Replace this line. >> > > > >> mv /tmp/$f /tmp2/ >> >> You could perfect that by appending a timestamp to /tmp2/$f so that >> there is no name clash the next day :-) >> > > Hi Wash, > > That could only deal with only one issue. Any files that comes > later that when the dir files are listed. > > #!/bin/bash > cd /tmp > for f in "`ls`"; do > cat $f | mail -s "$f" lixton (ati) gmail.com > mv /tmp/$f* /tmp2 > done >
Why do use the asterisk in there? $f* ?
> > Looking at example above... if multiple files are listed, only one > file (1st one is read) and the rest "only content is read using "cat" >
In all those files, how many matches do you get when parsing for the content you want?
For instance:
find . -type f -exec grep -li 'SOME_STRING' {} \;
If only 1 file matches, then you can use substitution to get the file, perhaps:
for f in ` find . -type f -exec grep -li 'SOME_STRING' {} \;`; do cat $f ....
This will output content of the file :(
> Wilson > > > > >> >> >> >> >>> done >>> >>> *Now for reading an email reply file from above:* >>> Well this will send BUT only one at a time and if multiple >>> emails are received at a go - all the others are deleted! >>> How do i perfect this.... >>> >>> #!/bin/bash >>> for i in 30 (corresponds to a linux user email box line 30 where >>> subject is) >>> do >>> awk -v a=$i 'NR==a {print $0}' /var/spool/mail/root > testfile2 >>> cat /dev/null > /var/spool/mail/root >>> echo `cat /root/testfile2` | /usr/bin/gammu --sendsms TEXT `awk >>> '{print $2}' testfile2` >>> >>> >> Use the same idea as above. Appending a timestamp will make files >> unique. >> > > Well if i want the SMS sent, i must read the conntent. timestamp may > not help if i can be able to read all the files and perse the output to > diffrent files or even one for sms delivery :) > > I am not sure I understand you, and as such I may come out as more misleading :)
What MTA do you use BTW? Postfix?
- i can use postfix/sendmail.
I think I have somewhere a script that can help you in this endeavor, but I only used it with Exim.
may i look into the script, may be it has what i want...
-- Best regards, Odhiambo WASHINGTON, Nairobi,KE +254733744121/+254722743223 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ I can't hear you -- I'm using the scrambler.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
-- Best regards, Odhiambo WASHINGTON, Nairobi,KE +254733744121/+254722743223 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ I can't hear you -- I'm using the scrambler.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

instead of stopping and starting gammu-smsd for every message, i'ld suggest sending of smses in batch like once every minute to give it a smooth run. i don't know how it works but it doesn't seem right to me. On Mon, Jun 18, 2012 at 4:51 PM, gisho <gichuhie@gmail.com> wrote:
just read the STDIN in python you could do this
import sys if __name__=="__main__": message = sys.stdin.read()
in perl: #!/usr/bin/perl -w my $message = <STDIN>;
in bash: etc etc
once you get the message you can go ahead to do whatever parsing you are doing.
hint, hint: if the traffic is that much you can always use a queue like rabbitmq or even a db for that matter, in fact its highly recommended for those kind of jobs
-- -erastus +254733725373 Nairobi Kenya
On Mon, Jun 18, 2012 at 4:34 PM, Odhiambo Washington <odhiambo@gmail.com>wrote:
On Mon, Jun 18, 2012 at 4:01 PM, Thuo Wilson <lixton@gmail.com> wrote:
On 18 June 2012 10:52, gisho <gichuhie@gmail.com> wrote:
- as @wash said above, only move the specific file - not everything which is denoted by "*" so this should look like mv /tmp/$f /tmp2
- instead of reading mail spool, you can pipe incoming mails to a script either by using procmailrc or by using /etc/aliases file if using postfix. this could look like this:
smsemail: "|/usr/libexec/postfix/trap.py"
Thanks Gisho, Thats is *part* of what i needed. I will use the alias. But note supposing am doing bulk sms - this may [not affirmative] fail due to numbers of emails < inflow... Second: i decided to change script to read as follows; simpler but doing better [?]
Imagine i don't have the script called from alias. How do i read one line at a time in the file "email_to_sms" and exec the command to send the sms. That is my main issue
Am sure its something simple i havent scratched my head enough!
#!/bin/bash `grep Subject /var/spool/mail/sms > email_to_sms` cat /dev/null > /var/spool/mail/sms killall -9 gammu-smsd #since for some reason gammu-smsd is using use_lock and wont disable echo `cat email_to_sms` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms` sleep 15 #wait for the above to finish /etc/init.d/gammu-smsd start #start to continue checking any incoming sms cat email_to_sms >> email_to_sms2 #incase make a backup file cat /dev/null > email_to_sms #empty for no2 message in the line :)
So the file email_to_sms is a text file containing some data you need to work on, each line at a time:
for data in `cat email_to_sms`; do echo $data | /usr/bin/gammu $ARGS .....; done
That will surely iterate through all the data in the file, one line at a time... You can do loop processing until there is nothing left, or always check if exists a file before you act on it..
However at this point, I suggest you work on a file which has been moved to another directory, so move the file first and reference it from where it is. Since the filenames seem to clash at some point (or why do you wven want to move the files?) then my initial suggestion stands - append the current timestamp value to the filename to make it unique.
Please try and tell breakdown what this command line does:
echo `cat email_to_sms` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms`
Then again, if you need a program to rexec, there is no need to give it a SIGKILL (-9). Just do a SIGHUP (-1) So you can rexec gammu after it completes processing the first file while making it loop to look for the existence of another file in the parent directory ... and the loop continues...
Wilson.
smsemail being the account receiving mails. if your script is elsewhere you will need to tell SELinux of that otherwise it will fail ( thats if you have selinux)
-- -erastus +254733725373 Nairobi Kenya
On Mon, Jun 18, 2012 at 10:24 AM, gisho <gichuhie@gmail.com> wrote:
this is just a pointer: whichever way you do it, keeping files in the same directory means the time consumed by the filter will increase as files increase thus making the process slower and slower by every new file
--
On Mon, Jun 18, 2012 at 9:41 AM, Thuo Wilson <lixton@gmail.com> wrote:
On 16 June 2012 23:38, Odhiambo Washington <odhiambo@gmail.com>wrote:
> > > On Sat, Jun 16, 2012 at 1:38 PM, Thuo Wilson <lixton@gmail.com>wrote: > >> >> >> >> On 16 June 2012 13:28, Odhiambo Washington <odhiambo@gmail.com>wrote: >> >>> >>> >>> On Sat, Jun 16, 2012 at 12:57 PM, Thuo Wilson <lixton@gmail.com>wrote: >>> >>>> I have a bash scripting question that has really bothered me; >>>> >>>> >>>> First i want to list files in a directory with a command - no prob >>>> Second - i want to get the name of the file - no problem >>>> Third i want to read the content of the file - no problem >>>> Fourth i want to extract some information , column and rows from >>>> the file - no problem >>>> >>>> My Idea: >>>> >My idea is to receive sms and forward to an email - working file >>>> but only one at a time! #script 1 >>>> >second is whenever a reply is sent from the email address "x" >>>> with subject "$CELLPHONE $MESSAGE" the script is called by clone to send >>>> the sms to user $cellphone - working file but only one at a >>>> time! #script 2 >>>> >>>> >>>> *Problem:* >>>> The directory contains multiple files with dynamic names >>>> (generated by system) some properties being date,time and cellphone number >>>> The problem is i don't know how to read multiple files name, >>>> read content to a file for processing at a go and output the results into >>>> another file using bash. >>>> No 2 - i want the *filename* of the other files....in the same >>>> directory and each file to be called one at a time. >>>> >>>> take example >>>> >>>> cd /tmp >>>> >>>> -rw-r--r-- 1 root root 0 Jun 16 12:42 a.2012.1242pm >>>> -rw-r--r-- 1 root root 0 Jun 16 12:42 b.2012.1243pm >>>> -rw-r--r-- 1 root root 0 Jun 16 12:42 c.2012.1244pm >>>> -rw-r--r-- 1 root root 0 Jun 16 12:42 d.2012.1245pm >>>> -rw-r--r-- 1 root root 0 Jun 16 12:42 e.2012.1246pm >>>> >>>> bear in mind the files are generated randomly and have no >>>> control over that :)- so to speak for question purposes >>>> >>>> I have the below script to read one line at a time and move what >>>> is read to another folder - not good practice. I would want to retain the >>>> file in same folder but my bash script should know that the file is read! >>>> >>>> How do i perfect this: This reads a file at a time and sends an >>>> email but if multiple are received at a go - all the rest iregardless are >>>> moved to the tmp2 folder. >>>> >>>> #!/bin/bash >>>> cd /tmp >>>> for f in "`ls`"; do >>>> cat $f | mail -s "$f" lixton (ati) gmail.com >>>> mv /tmp/* /tmp2 >>>> >>> ^^^^^^^^^^^^^^^^^^^^^^^^ Replace this line. >>> >> >> >> >>> mv /tmp/$f /tmp2/ >>> >>> You could perfect that by appending a timestamp to /tmp2/$f so >>> that there is no name clash the next day :-) >>> >> >> Hi Wash, >> >> That could only deal with only one issue. Any files that comes >> later that when the dir files are listed. >> >> #!/bin/bash >> cd /tmp >> for f in "`ls`"; do >> cat $f | mail -s "$f" lixton (ati) gmail.com >> mv /tmp/$f* /tmp2 >> done >> > > Why do use the asterisk in there? $f* ? > > >> >> Looking at example above... if multiple files are listed, only one >> file (1st one is read) and the rest "only content is read using "cat" >> > >
> In all those files, how many matches do you get when parsing for the > content you want? > > For instance: > > find . -type f -exec grep -li 'SOME_STRING' {} \; > > If only 1 file matches, then you can use substitution to get the > file, perhaps: > > for f in ` find . -type f -exec grep -li 'SOME_STRING' {} \;`; do > cat $f .... >
This will output content of the file :(
> > > >> Wilson >> >> >> >> >>> >>> >>> >>> >>>> done >>>> >>>> *Now for reading an email reply file from above:* >>>> Well this will send BUT only one at a time and if multiple >>>> emails are received at a go - all the others are deleted! >>>> How do i perfect this.... >>>> >>>> #!/bin/bash >>>> for i in 30 (corresponds to a linux user email box line 30 where >>>> subject is) >>>> do >>>> awk -v a=$i 'NR==a {print $0}' /var/spool/mail/root > testfile2 >>>> cat /dev/null > /var/spool/mail/root >>>> echo `cat /root/testfile2` | /usr/bin/gammu --sendsms TEXT `awk >>>> '{print $2}' testfile2` >>>> >>>> >>> Use the same idea as above. Appending a timestamp will make files >>> unique. >>> >> >> Well if i want the SMS sent, i must read the conntent. timestamp >> may not help if i can be able to read all the files and perse the output to >> diffrent files or even one for sms delivery :) >> >> > I am not sure I understand you, and as such I may come out as more > misleading :) > > What MTA do you use BTW? Postfix?
- i can use postfix/sendmail.
> > I think I have somewhere a script that can help you in this > endeavor, but I only used it with Exim. > >
may i look into the script, may be it has what i want...
> > > -- > Best regards, > Odhiambo WASHINGTON, > Nairobi,KE > +254733744121/+254722743223 > _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ > I can't hear you -- I'm using the scrambler. > > > _______________________________________________ > Skunkworks mailing list > Skunkworks@lists.my.co.ke > ------------ > List info, subscribe/unsubscribe > http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks > ------------ > > Skunkworks Rules > http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 > ------------ > Other services @ http://my.co.ke >
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
-- Best regards, Odhiambo WASHINGTON, Nairobi,KE +254733744121/+254722743223 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ I can't hear you -- I'm using the scrambler.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

On 18 June 2012 16:51, gisho <gichuhie@gmail.com> wrote: Pyth
just read the STDIN
in python you could do this
import sys if __name__=="__main__": message = sys.stdin.read()
No much clue in python
in perl: #!/usr/bin/perl -w my $message = <STDIN>;
could try this - but does it mean BASH cant do this. An example a file named ABC.txt contains the following #cat ABC.txt Subject: +254XXXXXX See you soon Subject: +255TTTTTTT I wont come today Subject: +256PPPPPP oh no this isnt working I want to call each line at a time allowing another external program to fully exec whiras the above lines are read one at a time. Am thinking of for while loop but wondering how to hold the rows of the file in memory Wilson
in bash: etc etc
once you get the message you can go ahead to do whatever parsing you are doing.
hint, hint: if the traffic is that much you can always use a queue like rabbitmq or even a db for that matter, in fact its highly recommended for those kind of jobs
-- -erastus +254733725373 Nairobi Kenya
On Mon, Jun 18, 2012 at 4:34 PM, Odhiambo Washington <odhiambo@gmail.com>wrote:
On Mon, Jun 18, 2012 at 4:01 PM, Thuo Wilson <lixton@gmail.com> wrote:
On 18 June 2012 10:52, gisho <gichuhie@gmail.com> wrote:
- as @wash said above, only move the specific file - not everything which is denoted by "*" so this should look like mv /tmp/$f /tmp2
- instead of reading mail spool, you can pipe incoming mails to a script either by using procmailrc or by using /etc/aliases file if using postfix. this could look like this:
smsemail: "|/usr/libexec/postfix/trap.py"
Thanks Gisho, Thats is *part* of what i needed. I will use the alias. But note supposing am doing bulk sms - this may [not affirmative] fail due to numbers of emails < inflow... Second: i decided to change script to read as follows; simpler but doing better [?]
Imagine i don't have the script called from alias. How do i read one line at a time in the file "email_to_sms" and exec the command to send the sms. That is my main issue
Am sure its something simple i havent scratched my head enough!
#!/bin/bash `grep Subject /var/spool/mail/sms > email_to_sms` cat /dev/null > /var/spool/mail/sms killall -9 gammu-smsd #since for some reason gammu-smsd is using use_lock and wont disable echo `cat email_to_sms` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms` sleep 15 #wait for the above to finish /etc/init.d/gammu-smsd start #start to continue checking any incoming sms cat email_to_sms >> email_to_sms2 #incase make a backup file cat /dev/null > email_to_sms #empty for no2 message in the line :)
So the file email_to_sms is a text file containing some data you need to work on, each line at a time:
for data in `cat email_to_sms`; do echo $data | /usr/bin/gammu $ARGS .....; done
That will surely iterate through all the data in the file, one line at a time... You can do loop processing until there is nothing left, or always check if exists a file before you act on it..
However at this point, I suggest you work on a file which has been moved to another directory, so move the file first and reference it from where it is. Since the filenames seem to clash at some point (or why do you wven want to move the files?) then my initial suggestion stands - append the current timestamp value to the filename to make it unique.
Please try and tell breakdown what this command line does:
echo `cat email_to_sms` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms`
Then again, if you need a program to rexec, there is no need to give it a SIGKILL (-9). Just do a SIGHUP (-1) So you can rexec gammu after it completes processing the first file while making it loop to look for the existence of another file in the parent directory ... and the loop continues...
Wilson.
smsemail being the account receiving mails. if your script is elsewhere you will need to tell SELinux of that otherwise it will fail ( thats if you have selinux)
-- -erastus +254733725373 Nairobi Kenya
On Mon, Jun 18, 2012 at 10:24 AM, gisho <gichuhie@gmail.com> wrote:
this is just a pointer: whichever way you do it, keeping files in the same directory means the time consumed by the filter will increase as files increase thus making the process slower and slower by every new file
--
On Mon, Jun 18, 2012 at 9:41 AM, Thuo Wilson <lixton@gmail.com> wrote:
On 16 June 2012 23:38, Odhiambo Washington <odhiambo@gmail.com>wrote:
> > > On Sat, Jun 16, 2012 at 1:38 PM, Thuo Wilson <lixton@gmail.com>wrote: > >> >> >> >> On 16 June 2012 13:28, Odhiambo Washington <odhiambo@gmail.com>wrote: >> >>> >>> >>> On Sat, Jun 16, 2012 at 12:57 PM, Thuo Wilson <lixton@gmail.com>wrote: >>> >>>> I have a bash scripting question that has really bothered me; >>>> >>>> >>>> First i want to list files in a directory with a command - no prob >>>> Second - i want to get the name of the file - no problem >>>> Third i want to read the content of the file - no problem >>>> Fourth i want to extract some information , column and rows from >>>> the file - no problem >>>> >>>> My Idea: >>>> >My idea is to receive sms and forward to an email - working file >>>> but only one at a time! #script 1 >>>> >second is whenever a reply is sent from the email address "x" >>>> with subject "$CELLPHONE $MESSAGE" the script is called by clone to send >>>> the sms to user $cellphone - working file but only one at a >>>> time! #script 2 >>>> >>>> >>>> *Problem:* >>>> The directory contains multiple files with dynamic names >>>> (generated by system) some properties being date,time and cellphone number >>>> The problem is i don't know how to read multiple files name, >>>> read content to a file for processing at a go and output the results into >>>> another file using bash. >>>> No 2 - i want the *filename* of the other files....in the same >>>> directory and each file to be called one at a time. >>>> >>>> take example >>>> >>>> cd /tmp >>>> >>>> -rw-r--r-- 1 root root 0 Jun 16 12:42 a.2012.1242pm >>>> -rw-r--r-- 1 root root 0 Jun 16 12:42 b.2012.1243pm >>>> -rw-r--r-- 1 root root 0 Jun 16 12:42 c.2012.1244pm >>>> -rw-r--r-- 1 root root 0 Jun 16 12:42 d.2012.1245pm >>>> -rw-r--r-- 1 root root 0 Jun 16 12:42 e.2012.1246pm >>>> >>>> bear in mind the files are generated randomly and have no >>>> control over that :)- so to speak for question purposes >>>> >>>> I have the below script to read one line at a time and move what >>>> is read to another folder - not good practice. I would want to retain the >>>> file in same folder but my bash script should know that the file is read! >>>> >>>> How do i perfect this: This reads a file at a time and sends an >>>> email but if multiple are received at a go - all the rest iregardless are >>>> moved to the tmp2 folder. >>>> >>>> #!/bin/bash >>>> cd /tmp >>>> for f in "`ls`"; do >>>> cat $f | mail -s "$f" lixton (ati) gmail.com >>>> mv /tmp/* /tmp2 >>>> >>> ^^^^^^^^^^^^^^^^^^^^^^^^ Replace this line. >>> >> >> >> >>> mv /tmp/$f /tmp2/ >>> >>> You could perfect that by appending a timestamp to /tmp2/$f so >>> that there is no name clash the next day :-) >>> >> >> Hi Wash, >> >> That could only deal with only one issue. Any files that comes >> later that when the dir files are listed. >> >> #!/bin/bash >> cd /tmp >> for f in "`ls`"; do >> cat $f | mail -s "$f" lixton (ati) gmail.com >> mv /tmp/$f* /tmp2 >> done >> > > Why do use the asterisk in there? $f* ? > > >> >> Looking at example above... if multiple files are listed, only one >> file (1st one is read) and the rest "only content is read using "cat" >> > >
> In all those files, how many matches do you get when parsing for the > content you want? > > For instance: > > find . -type f -exec grep -li 'SOME_STRING' {} \; > > If only 1 file matches, then you can use substitution to get the > file, perhaps: > > for f in ` find . -type f -exec grep -li 'SOME_STRING' {} \;`; do > cat $f .... >
This will output content of the file :(
> > > >> Wilson >> >> >> >> >>> >>> >>> >>> >>>> done >>>> >>>> *Now for reading an email reply file from above:* >>>> Well this will send BUT only one at a time and if multiple >>>> emails are received at a go - all the others are deleted! >>>> How do i perfect this.... >>>> >>>> #!/bin/bash >>>> for i in 30 (corresponds to a linux user email box line 30 where >>>> subject is) >>>> do >>>> awk -v a=$i 'NR==a {print $0}' /var/spool/mail/root > testfile2 >>>> cat /dev/null > /var/spool/mail/root >>>> echo `cat /root/testfile2` | /usr/bin/gammu --sendsms TEXT `awk >>>> '{print $2}' testfile2` >>>> >>>> >>> Use the same idea as above. Appending a timestamp will make files >>> unique. >>> >> >> Well if i want the SMS sent, i must read the conntent. timestamp >> may not help if i can be able to read all the files and perse the output to >> diffrent files or even one for sms delivery :) >> >> > I am not sure I understand you, and as such I may come out as more > misleading :) > > What MTA do you use BTW? Postfix?
- i can use postfix/sendmail.
> > I think I have somewhere a script that can help you in this > endeavor, but I only used it with Exim. > >
may i look into the script, may be it has what i want...
> > > -- > Best regards, > Odhiambo WASHINGTON, > Nairobi,KE > +254733744121/+254722743223 > _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ > I can't hear you -- I'm using the scrambler. > > > _______________________________________________ > Skunkworks mailing list > Skunkworks@lists.my.co.ke > ------------ > List info, subscribe/unsubscribe > http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks > ------------ > > Skunkworks Rules > http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 > ------------ > Other services @ http://my.co.ke >
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
-- Best regards, Odhiambo WASHINGTON, Nairobi,KE +254733744121/+254722743223 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ I can't hear you -- I'm using the scrambler.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

On 18 June 2012 16:34, Odhiambo Washington <odhiambo@gmail.com> wrote:
On Mon, Jun 18, 2012 at 4:01 PM, Thuo Wilson <lixton@gmail.com> wrote:
On 18 June 2012 10:52, gisho <gichuhie@gmail.com> wrote:
- as @wash said above, only move the specific file - not everything which is denoted by "*" so this should look like mv /tmp/$f /tmp2
- instead of reading mail spool, you can pipe incoming mails to a script either by using procmailrc or by using /etc/aliases file if using postfix. this could look like this:
smsemail: "|/usr/libexec/postfix/trap.py"
Thanks Gisho, Thats is *part* of what i needed. I will use the alias. But note supposing am doing bulk sms - this may [not affirmative] fail due to numbers of emails < inflow... Second: i decided to change script to read as follows; simpler but doing better [?]
Imagine i don't have the script called from alias. How do i read one line at a time in the file "email_to_sms" and exec the command to send the sms. That is my main issue
Am sure its something simple i havent scratched my head enough!
#!/bin/bash `grep Subject /var/spool/mail/sms > email_to_sms` cat /dev/null > /var/spool/mail/sms killall -9 gammu-smsd #since for some reason gammu-smsd is using use_lock and wont disable echo `cat email_to_sms` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms` sleep 15 #wait for the above to finish /etc/init.d/gammu-smsd start #start to continue checking any incoming sms cat email_to_sms >> email_to_sms2 #incase make a backup file cat /dev/null > email_to_sms #empty for no2 message in the line :)
for sure slowly we are getting there...
So the file email_to_sms is a text file containing some data you need to work on, each line at a time:
for data in `cat email_to_sms`; do echo $data | /usr/bin/gammu $ARGS .....; done
isnt this doing same thing as my script? :)
That will surely iterate through all the data in the file, one line at a time... You can do loop processing until there is nothing left, or always check if exists a file before you act on it..
However at this point, I suggest you work on a file which has been moved to another directory, so move the file first and reference it from where it is. Since the filenames seem to clash at some point (or why do you wven want to move the files?) then my initial suggestion stands - append the current timestamp value to the filename to make it unique.
Please try and tell breakdown what this command line does:
echo `cat email_to_sms` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms`
assuming the file content looks like: A +254XXXXX F A +255YYYYYY G A +256ZZZZZ K echo `cat email_to_sms` The command just get the file content - first part; /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms` - send an sms to the number on the second column (2) in the file email_to_sms
Then again, if you need a program to rexec, there is no need to give it a SIGKILL (-9). Just do a SIGHUP (-1) So you can rexec gammu after it completes processing the first file while making it loop to look for the existence of another file in the parent directory ... and the loop continues...
Not bad thought!
Wilson.
smsemail being the account receiving mails. if your script is elsewhere you will need to tell SELinux of that otherwise it will fail ( thats if you have selinux)
-- -erastus +254733725373 Nairobi Kenya
On Mon, Jun 18, 2012 at 10:24 AM, gisho <gichuhie@gmail.com> wrote:
this is just a pointer: whichever way you do it, keeping files in the same directory means the time consumed by the filter will increase as files increase thus making the process slower and slower by every new file
--
On Mon, Jun 18, 2012 at 9:41 AM, Thuo Wilson <lixton@gmail.com> wrote:
On 16 June 2012 23:38, Odhiambo Washington <odhiambo@gmail.com> wrote:
On Sat, Jun 16, 2012 at 1:38 PM, Thuo Wilson <lixton@gmail.com>wrote:
> > > > On 16 June 2012 13:28, Odhiambo Washington <odhiambo@gmail.com>wrote: > >> >> >> On Sat, Jun 16, 2012 at 12:57 PM, Thuo Wilson <lixton@gmail.com>wrote: >> >>> I have a bash scripting question that has really bothered me; >>> >>> >>> First i want to list files in a directory with a command - no prob >>> Second - i want to get the name of the file - no problem >>> Third i want to read the content of the file - no problem >>> Fourth i want to extract some information , column and rows from >>> the file - no problem >>> >>> My Idea: >>> >My idea is to receive sms and forward to an email - working file >>> but only one at a time! #script 1 >>> >second is whenever a reply is sent from the email address "x" >>> with subject "$CELLPHONE $MESSAGE" the script is called by clone to send >>> the sms to user $cellphone - working file but only one at a >>> time! #script 2 >>> >>> >>> *Problem:* >>> The directory contains multiple files with dynamic names >>> (generated by system) some properties being date,time and cellphone number >>> The problem is i don't know how to read multiple files name, >>> read content to a file for processing at a go and output the results into >>> another file using bash. >>> No 2 - i want the *filename* of the other files....in the same >>> directory and each file to be called one at a time. >>> >>> take example >>> >>> cd /tmp >>> >>> -rw-r--r-- 1 root root 0 Jun 16 12:42 a.2012.1242pm >>> -rw-r--r-- 1 root root 0 Jun 16 12:42 b.2012.1243pm >>> -rw-r--r-- 1 root root 0 Jun 16 12:42 c.2012.1244pm >>> -rw-r--r-- 1 root root 0 Jun 16 12:42 d.2012.1245pm >>> -rw-r--r-- 1 root root 0 Jun 16 12:42 e.2012.1246pm >>> >>> bear in mind the files are generated randomly and have no >>> control over that :)- so to speak for question purposes >>> >>> I have the below script to read one line at a time and move what >>> is read to another folder - not good practice. I would want to retain the >>> file in same folder but my bash script should know that the file is read! >>> >>> How do i perfect this: This reads a file at a time and sends an >>> email but if multiple are received at a go - all the rest iregardless are >>> moved to the tmp2 folder. >>> >>> #!/bin/bash >>> cd /tmp >>> for f in "`ls`"; do >>> cat $f | mail -s "$f" lixton (ati) gmail.com >>> mv /tmp/* /tmp2 >>> >> ^^^^^^^^^^^^^^^^^^^^^^^^ Replace this line. >> > > > >> mv /tmp/$f /tmp2/ >> >> You could perfect that by appending a timestamp to /tmp2/$f so that >> there is no name clash the next day :-) >> > > Hi Wash, > > That could only deal with only one issue. Any files that comes > later that when the dir files are listed. > > #!/bin/bash > cd /tmp > for f in "`ls`"; do > cat $f | mail -s "$f" lixton (ati) gmail.com > mv /tmp/$f* /tmp2 > done >
Why do use the asterisk in there? $f* ?
> > Looking at example above... if multiple files are listed, only one > file (1st one is read) and the rest "only content is read using "cat" >
In all those files, how many matches do you get when parsing for the content you want?
For instance:
find . -type f -exec grep -li 'SOME_STRING' {} \;
If only 1 file matches, then you can use substitution to get the file, perhaps:
for f in ` find . -type f -exec grep -li 'SOME_STRING' {} \;`; do cat $f ....
This will output content of the file :(
> Wilson > > > > >> >> >> >> >>> done >>> >>> *Now for reading an email reply file from above:* >>> Well this will send BUT only one at a time and if multiple >>> emails are received at a go - all the others are deleted! >>> How do i perfect this.... >>> >>> #!/bin/bash >>> for i in 30 (corresponds to a linux user email box line 30 where >>> subject is) >>> do >>> awk -v a=$i 'NR==a {print $0}' /var/spool/mail/root > testfile2 >>> cat /dev/null > /var/spool/mail/root >>> echo `cat /root/testfile2` | /usr/bin/gammu --sendsms TEXT `awk >>> '{print $2}' testfile2` >>> >>> >> Use the same idea as above. Appending a timestamp will make files >> unique. >> > > Well if i want the SMS sent, i must read the conntent. timestamp may > not help if i can be able to read all the files and perse the output to > diffrent files or even one for sms delivery :) > > I am not sure I understand you, and as such I may come out as more misleading :)
What MTA do you use BTW? Postfix?
- i can use postfix/sendmail.
I think I have somewhere a script that can help you in this endeavor, but I only used it with Exim.
may i look into the script, may be it has what i want...
-- Best regards, Odhiambo WASHINGTON, Nairobi,KE +254733744121/+254722743223 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ I can't hear you -- I'm using the scrambler.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
-- Best regards, Odhiambo WASHINGTON, Nairobi,KE +254733744121/+254722743223 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ I can't hear you -- I'm using the scrambler.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

On Mon, Jun 18, 2012 at 5:08 PM, Thuo Wilson <lixton@gmail.com> wrote:
On 18 June 2012 16:34, Odhiambo Washington <odhiambo@gmail.com> wrote:
On Mon, Jun 18, 2012 at 4:01 PM, Thuo Wilson <lixton@gmail.com> wrote:
On 18 June 2012 10:52, gisho <gichuhie@gmail.com> wrote:
- as @wash said above, only move the specific file - not everything which is denoted by "*" so this should look like mv /tmp/$f /tmp2
- instead of reading mail spool, you can pipe incoming mails to a script either by using procmailrc or by using /etc/aliases file if using postfix. this could look like this:
smsemail: "|/usr/libexec/postfix/trap.py"
Thanks Gisho, Thats is *part* of what i needed. I will use the alias. But note supposing am doing bulk sms - this may [not affirmative] fail due to numbers of emails < inflow... Second: i decided to change script to read as follows; simpler but doing better [?]
Imagine i don't have the script called from alias. How do i read one line at a time in the file "email_to_sms" and exec the command to send the sms. That is my main issue
Am sure its something simple i havent scratched my head enough!
#!/bin/bash `grep Subject /var/spool/mail/sms > email_to_sms` cat /dev/null > /var/spool/mail/sms killall -9 gammu-smsd #since for some reason gammu-smsd is using use_lock and wont disable echo `cat email_to_sms` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms` sleep 15 #wait for the above to finish /etc/init.d/gammu-smsd start #start to continue checking any incoming sms cat email_to_sms >> email_to_sms2 #incase make a backup file cat /dev/null > email_to_sms #empty for no2 message in the line :)
for sure slowly we are getting there...
So the file email_to_sms is a text file containing some data you need to work on, each line at a time:
for data in `cat email_to_sms`; do echo $data | /usr/bin/gammu $ARGS .....; done
isnt this doing same thing as my script? :)
That will surely iterate through all the data in the file, one line at a time... You can do loop processing until there is nothing left, or always check if exists a file before you act on it..
However at this point, I suggest you work on a file which has been moved to another directory, so move the file first and reference it from where it is. Since the filenames seem to clash at some point (or why do you wven want to move the files?) then my initial suggestion stands - append the current timestamp value to the filename to make it unique.
Please try and tell breakdown what this command line does:
echo `cat email_to_sms` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms`
assuming the file content looks like:
A +254XXXXX F A +255YYYYYY G A +256ZZZZZ K
echo `cat email_to_sms` The command just get the file content - first part; /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms` - send an sms to the number on the second column (2) in the file email_to_sms
Then again, if you need a program to rexec, there is no need to give it a SIGKILL (-9). Just do a SIGHUP (-1) So you can rexec gammu after it completes processing the first file while making it loop to look for the existence of another file in the parent directory ... and the loop continues...
Not bad thought!
[wash@jaribu ~/THUO]$ pwd /home/wash/THUO [wash@jaribu ~/THUO]$ ls -1 email_to_sms email_to_sms1 email_to_sms2 [wash@jaribu ~/THUO]$ file=`ls -1` [wash@jaribu ~/THUO]$ telno=`cat $file | awk '{print $2}'` [wash@jaribu ~/THUO]$ for t in $telno; do echo -e "$t\n"; done +254XXXXX +255YYYYYY +256ZZZZZ +333XXXXX +444YYYYYY +555ZZZZZ +6664XXXXX +7774YYYYYY +8884ZZZZZ I hope this gives you some clue. It's a way to deal with each file at a time.. -- Best regards, Odhiambo WASHINGTON, Nairobi,KE +254733744121/+254722743223 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ I can't hear you -- I'm using the scrambler.

On 18 June 2012 19:51, Odhiambo Washington <odhiambo@gmail.com> wrote:
On Mon, Jun 18, 2012 at 5:08 PM, Thuo Wilson <lixton@gmail.com> wrote:
On 18 June 2012 16:34, Odhiambo Washington <odhiambo@gmail.com> wrote:
On Mon, Jun 18, 2012 at 4:01 PM, Thuo Wilson <lixton@gmail.com> wrote:
On 18 June 2012 10:52, gisho <gichuhie@gmail.com> wrote:
- as @wash said above, only move the specific file - not everything which is denoted by "*" so this should look like mv /tmp/$f /tmp2
- instead of reading mail spool, you can pipe incoming mails to a script either by using procmailrc or by using /etc/aliases file if using postfix. this could look like this:
smsemail: "|/usr/libexec/postfix/trap.py"
Thanks Gisho, Thats is *part* of what i needed. I will use the alias. But note supposing am doing bulk sms - this may [not affirmative] fail due to numbers of emails < inflow... Second: i decided to change script to read as follows; simpler but doing better [?]
Imagine i don't have the script called from alias. How do i read one line at a time in the file "email_to_sms" and exec the command to send the sms. That is my main issue
Am sure its something simple i havent scratched my head enough!
#!/bin/bash `grep Subject /var/spool/mail/sms > email_to_sms` cat /dev/null > /var/spool/mail/sms killall -9 gammu-smsd #since for some reason gammu-smsd is using use_lock and wont disable echo `cat email_to_sms` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms` sleep 15 #wait for the above to finish /etc/init.d/gammu-smsd start #start to continue checking any incoming sms cat email_to_sms >> email_to_sms2 #incase make a backup file cat /dev/null > email_to_sms #empty for no2 message in the line :)
for sure slowly we are getting there...
So the file email_to_sms is a text file containing some data you need to work on, each line at a time:
for data in `cat email_to_sms`; do echo $data | /usr/bin/gammu $ARGS .....; done
isnt this doing same thing as my script? :)
That will surely iterate through all the data in the file, one line at a time... You can do loop processing until there is nothing left, or always check if exists a file before you act on it..
However at this point, I suggest you work on a file which has been moved to another directory, so move the file first and reference it from where it is. Since the filenames seem to clash at some point (or why do you wven want to move the files?) then my initial suggestion stands - append the current timestamp value to the filename to make it unique.
Please try and tell breakdown what this command line does:
echo `cat email_to_sms` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms`
assuming the file content looks like:
A +254XXXXX F A +255YYYYYY G A +256ZZZZZ K
echo `cat email_to_sms` The command just get the file content - first part; /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms` - send an sms to the number on the second column (2) in the file email_to_sms
Then again, if you need a program to rexec, there is no need to give it a SIGKILL (-9). Just do a SIGHUP (-1) So you can rexec gammu after it completes processing the first file while making it loop to look for the existence of another file in the parent directory ... and the loop continues...
Not bad thought!
[wash@jaribu ~/THUO]$ pwd /home/wash/THUO
[wash@jaribu ~/THUO]$ ls -1 email_to_sms email_to_sms1 email_to_sms2 [wash@jaribu ~/THUO]$ file=`ls -1` [wash@jaribu ~/THUO]$ telno=`cat $file | awk '{print $2}'` [wash@jaribu ~/THUO]$ for t in $telno; do echo -e "$t\n"; done +254XXXXX
+255YYYYYY
+256ZZZZZ
+333XXXXX
+444YYYYYY
+555ZZZZZ
+6664XXXXX
+7774YYYYYY
+8884ZZZZZ
I hope this gives you some clue. It's a way to deal with each file at a time..
I want other text too A +254XXXXX F RR A +255YYYYYY G HH A +256ZZZZZ K LL My dilemma is this in simple terms. Read line 1 then execute a command *if there is another line? "if"* Read line 2 then execute a command Read line 3 then execute a command Note:i can also read all the columns and read all rows at a go with awk. But narrowing down to row/line at a time - sijui hiyo! Reading the file i know how to- is not a problem - my script is doing, *but reading a line at a time then EXEC, line2 then EXEC, line3 then EXEC - that is my issue :)* * *
-- Best regards, Odhiambo WASHINGTON, Nairobi,KE +254733744121/+254722743223 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ I can't hear you -- I'm using the scrambler.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

Well, still scratching my head and am heading somewhere; This is doing exactly what i need BUT reading one line at a time untill the 10th line, if i want more i add manually... This might help some1 someday! But if there is a way to perfect it please do so..... #!/bin/bash smsfile=email_to_sms `grep Subject /var/spool/mail/sms >> email_to_sms` if [[ -s $smsfile ]] ; then cat /dev/null > /var/spool/mail/sms sed -i 's/Subject: //g' $smsfile echo `sed -n '1p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==1 {print $1}' $smsfile` echo `sed -n '2p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==2 {print $1}' $smsfile` echo `sed -n '3p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==3 {print $1}' $smsfile` echo `sed -n '4p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==4 {print $1}' $smsfile` echo `sed -n '5p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==5 {print $1}' $smsfile` echo `sed -n '6p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==6 {print $1}' $smsfile` echo `sed -n '7p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==7 {print $1}' $smsfile` echo `sed -n '8p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==8 {print $1}' $smsfile` echo `sed -n '9p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==9 {print $1}' $smsfile` echo `sed -n '10p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==10 {print $1}' $smsfile` else echo "***********Sorry the SMS FILE "$smsfile" is empty.************" fi /etc/init.d/gammu-smsd start cat email_to_sms >> email_to_sms2 cat /dev/null > email_to_sms On 19 June 2012 12:48, Thuo Wilson <lixton@gmail.com> wrote:
On 18 June 2012 19:51, Odhiambo Washington <odhiambo@gmail.com> wrote:
On Mon, Jun 18, 2012 at 5:08 PM, Thuo Wilson <lixton@gmail.com> wrote:
On 18 June 2012 16:34, Odhiambo Washington <odhiambo@gmail.com> wrote:
On Mon, Jun 18, 2012 at 4:01 PM, Thuo Wilson <lixton@gmail.com> wrote:
On 18 June 2012 10:52, gisho <gichuhie@gmail.com> wrote:
- as @wash said above, only move the specific file - not everything which is denoted by "*" so this should look like mv /tmp/$f /tmp2
- instead of reading mail spool, you can pipe incoming mails to a script either by using procmailrc or by using /etc/aliases file if using postfix. this could look like this:
smsemail: "|/usr/libexec/postfix/trap.py"
Thanks Gisho, Thats is *part* of what i needed. I will use the alias. But note supposing am doing bulk sms - this may [not affirmative] fail due to numbers of emails < inflow... Second: i decided to change script to read as follows; simpler but doing better [?]
Imagine i don't have the script called from alias. How do i read one line at a time in the file "email_to_sms" and exec the command to send the sms. That is my main issue
Am sure its something simple i havent scratched my head enough!
#!/bin/bash `grep Subject /var/spool/mail/sms > email_to_sms` cat /dev/null > /var/spool/mail/sms killall -9 gammu-smsd #since for some reason gammu-smsd is using use_lock and wont disable echo `cat email_to_sms` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms` sleep 15 #wait for the above to finish /etc/init.d/gammu-smsd start #start to continue checking any incoming sms cat email_to_sms >> email_to_sms2 #incase make a backup file cat /dev/null > email_to_sms #empty for no2 message in the line :)
for sure slowly we are getting there...
So the file email_to_sms is a text file containing some data you need to work on, each line at a time:
for data in `cat email_to_sms`; do echo $data | /usr/bin/gammu $ARGS .....; done
isnt this doing same thing as my script? :)
That will surely iterate through all the data in the file, one line at a time... You can do loop processing until there is nothing left, or always check if exists a file before you act on it..
However at this point, I suggest you work on a file which has been moved to another directory, so move the file first and reference it from where it is. Since the filenames seem to clash at some point (or why do you wven want to move the files?) then my initial suggestion stands - append the current timestamp value to the filename to make it unique.
Please try and tell breakdown what this command line does:
echo `cat email_to_sms` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms`
assuming the file content looks like:
A +254XXXXX F A +255YYYYYY G A +256ZZZZZ K
echo `cat email_to_sms` The command just get the file content - first part; /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms` - send an sms to the number on the second column (2) in the file email_to_sms
Then again, if you need a program to rexec, there is no need to give it a SIGKILL (-9). Just do a SIGHUP (-1) So you can rexec gammu after it completes processing the first file while making it loop to look for the existence of another file in the parent directory ... and the loop continues...
Not bad thought!
[wash@jaribu ~/THUO]$ pwd /home/wash/THUO
[wash@jaribu ~/THUO]$ ls -1 email_to_sms email_to_sms1 email_to_sms2 [wash@jaribu ~/THUO]$ file=`ls -1` [wash@jaribu ~/THUO]$ telno=`cat $file | awk '{print $2}'` [wash@jaribu ~/THUO]$ for t in $telno; do echo -e "$t\n"; done +254XXXXX
+255YYYYYY
+256ZZZZZ
+333XXXXX
+444YYYYYY
+555ZZZZZ
+6664XXXXX
+7774YYYYYY
+8884ZZZZZ
I hope this gives you some clue. It's a way to deal with each file at a time..
I want other text too
A +254XXXXX F RR A +255YYYYYY G HH A +256ZZZZZ K LL
My dilemma is this in simple terms.
Read line 1 then execute a command *if there is another line? "if"* Read line 2 then execute a command Read line 3 then execute a command Note:i can also read all the columns and read all rows at a go with awk. But narrowing down to row/line at a time - sijui hiyo!
Reading the file i know how to- is not a problem - my script is doing, *but reading a line at a time then EXEC, line2 then EXEC, line3 then EXEC - that is my issue :)* * *
-- Best regards, Odhiambo WASHINGTON, Nairobi,KE +254733744121/+254722743223 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ I can't hear you -- I'm using the scrambler.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

Did you want this? ╭─laban at ubuntu in /tmp @ '2012-06-21 21:41:15' ╰─○ head sms_file A +254XXXXX F RR A +255YYYYYY G HH A +256ZZZZZ K LL ╭─laban at ubuntu in /tmp @ '2012-06-21 21:41:24' ╰─○ IFS=" dquote> " ╭─laban at ubuntu in /tmp @ '2012-06-21 21:41:33' ╰─○ for line in $(< sms_file) for> do for> echo processing $line; for> field1=$(echo $line | awk '{print $1}'); phone_no=$(echo $line | awk '{print $2}'); field3=$(echo $line | awk '{print $3}'); for> echo "Processed -> $field1::$phone_no::$field3"; for> echo; for> done processing A +254XXXXX F RR Processed -> A::+254XXXXX::F processing A +255YYYYYY G HH Processed -> A::+255YYYYYY::G processing A +256ZZZZZ K LL Processed -> A::+256ZZZZZ::K The trick here is using IFS (Internal Field Separator) to tell the shell to split on newlines not whitespace. However, that is serialized.. Let's slow things a bit by injecting some ZZZs.. ╭─laban at ubuntu in /tmp @ '2012-06-21 21:51:24' ╰─○ sendsms () { line=$1; echo processing $line; field1=$(echo $line | awk '{print $1}'); phone_no=$(echo $line | awk '{print $2}'); field3=$(echo $line | awk '{print $3}'); sleep 5; echo "Processed -> $field1::$phone_no::$field3"; echo; } ╭─laban at ubuntu in /tmp @ '2012-06-21 21:51:52' ╰─○ {time (for line in $(< sms_file); do sendsms $line; done) } processing A +254XXXXX F RR Processed -> A::+254XXXXX::F processing A +255YYYYYY G HH Processed -> A::+255YYYYYY::G processing A +256ZZZZZ K LL Processed -> A::+256ZZZZZ::K ( for line in $(< sms_file); do; sendsms $line; done; ) 0,01s user 0,02s system 0% cpu 15,057 total 15 seconds for 3 messages! Since you are into bulk sms you might want something a little bit faster. Enter xargs 1. Move sendsms into a standalone script that works on one line per invocation 2. Parallel invoke the sendsms with xargs -P ╭─laban at ubuntu in /tmp @ '2012-06-21 22:00:16' ╰─○ cat sendsms.sh #!/bin/bash - set -o nounset # Treat unset variables as an error sendsms () { line=$1; echo processing $line; field1=$(echo $line | awk '{print $1}'); phone_no=$(echo $line | awk '{print $2}'); field3=$(echo $line | awk '{print $3}'); sleep 5; echo "Processed -> $field1::$phone_no::$field3"; echo; } sendsms "$@" ╭─laban at ubuntu in /tmp @ '2012-06-21 22:00:28' ╰─○ {time (cat sms_file | xargs --max-procs=10 --max-args=1 --replace=Z ./sendsms.sh Z) } processing A +256ZZZZZ K LL processing A +254XXXXX F RR processing A +255YYYYYY G HH Processed -> A::+256ZZZZZ::K Processed -> A::+255YYYYYY::G Processed -> A::+254XXXXX::F ( cat sms_file | xargs --max-procs=10 --max-args=1 --replace=Z ./sendsms.sh Z) 0,00s user 0,02s system 0% cpu 5,027 total And we have (!web) shell scale :) On Thu, Jun 21, 2012 at 3:08 PM, Thuo Wilson <lixton@gmail.com> wrote:
Well, still scratching my head and am heading somewhere; This is doing exactly what i need BUT reading one line at a time untill the 10th line, if i want more i add manually... This might help some1 someday! But if there is a way to perfect it please do so.....
#!/bin/bash
smsfile=email_to_sms `grep Subject /var/spool/mail/sms >> email_to_sms` if [[ -s $smsfile ]] ; then cat /dev/null > /var/spool/mail/sms sed -i 's/Subject: //g' $smsfile echo `sed -n '1p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==1 {print $1}' $smsfile` echo `sed -n '2p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==2 {print $1}' $smsfile` echo `sed -n '3p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==3 {print $1}' $smsfile` echo `sed -n '4p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==4 {print $1}' $smsfile` echo `sed -n '5p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==5 {print $1}' $smsfile` echo `sed -n '6p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==6 {print $1}' $smsfile` echo `sed -n '7p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==7 {print $1}' $smsfile` echo `sed -n '8p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==8 {print $1}' $smsfile` echo `sed -n '9p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==9 {print $1}' $smsfile` echo `sed -n '10p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==10 {print $1}' $smsfile` else echo "***********Sorry the SMS FILE "$smsfile" is empty.************" fi /etc/init.d/gammu-smsd start cat email_to_sms >> email_to_sms2 cat /dev/null > email_to_sms
On 19 June 2012 12:48, Thuo Wilson <lixton@gmail.com> wrote:
On 18 June 2012 19:51, Odhiambo Washington <odhiambo@gmail.com> wrote:
On Mon, Jun 18, 2012 at 5:08 PM, Thuo Wilson <lixton@gmail.com> wrote:
On 18 June 2012 16:34, Odhiambo Washington <odhiambo@gmail.com> wrote:
On Mon, Jun 18, 2012 at 4:01 PM, Thuo Wilson <lixton@gmail.com> wrote:
On 18 June 2012 10:52, gisho <gichuhie@gmail.com> wrote:
> - as @wash said above, only move the specific file - not everything > which is denoted by "*" so this should look like mv /tmp/$f /tmp2 > > - instead of reading mail spool, you can pipe incoming mails to a > script either by using procmailrc or by using /etc/aliases file if using > postfix. this could look like this: > > smsemail: "|/usr/libexec/postfix/trap.py" >
Thanks Gisho, Thats is *part* of what i needed. I will use the alias. But note supposing am doing bulk sms - this may [not affirmative] fail due to numbers of emails < inflow... Second: i decided to change script to read as follows; simpler but doing better [?]
Imagine i don't have the script called from alias. How do i read one line at a time in the file "email_to_sms" and exec the command to send the sms. That is my main issue
Am sure its something simple i havent scratched my head enough!
#!/bin/bash `grep Subject /var/spool/mail/sms > email_to_sms` cat /dev/null > /var/spool/mail/sms killall -9 gammu-smsd #since for some reason gammu-smsd is using use_lock and wont disable echo `cat email_to_sms` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms` sleep 15 #wait for the above to finish /etc/init.d/gammu-smsd start #start to continue checking any incoming sms cat email_to_sms >> email_to_sms2 #incase make a backup file cat /dev/null > email_to_sms #empty for no2 message in the line :)
for sure slowly we are getting there...
So the file email_to_sms is a text file containing some data you need to work on, each line at a time:
for data in `cat email_to_sms`; do echo $data | /usr/bin/gammu $ARGS .....; done
isnt this doing same thing as my script? :)
That will surely iterate through all the data in the file, one line at a time... You can do loop processing until there is nothing left, or always check if exists a file before you act on it..
However at this point, I suggest you work on a file which has been moved to another directory, so move the file first and reference it from where it is. Since the filenames seem to clash at some point (or why do you wven want to move the files?) then my initial suggestion stands - append the current timestamp value to the filename to make it unique.
Please try and tell breakdown what this command line does:
echo `cat email_to_sms` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms`
assuming the file content looks like:
A +254XXXXX F A +255YYYYYY G A +256ZZZZZ K
echo `cat email_to_sms` The command just get the file content - first part; /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms` - send an sms to the number on the second column (2) in the file email_to_sms
Then again, if you need a program to rexec, there is no need to give it a SIGKILL (-9). Just do a SIGHUP (-1) So you can rexec gammu after it completes processing the first file while making it loop to look for the existence of another file in the parent directory ... and the loop continues...
Not bad thought!
[wash@jaribu ~/THUO]$ pwd /home/wash/THUO
[wash@jaribu ~/THUO]$ ls -1 email_to_sms email_to_sms1 email_to_sms2 [wash@jaribu ~/THUO]$ file=`ls -1` [wash@jaribu ~/THUO]$ telno=`cat $file | awk '{print $2}'` [wash@jaribu ~/THUO]$ for t in $telno; do echo -e "$t\n"; done +254XXXXX
+255YYYYYY
+256ZZZZZ
+333XXXXX
+444YYYYYY
+555ZZZZZ
+6664XXXXX
+7774YYYYYY
+8884ZZZZZ
I hope this gives you some clue. It's a way to deal with each file at a time..
I want other text too
A +254XXXXX F RR A +255YYYYYY G HH A +256ZZZZZ K LL
My dilemma is this in simple terms.
Read line 1 then execute a command *if there is another line? "if"* Read line 2 then execute a command Read line 3 then execute a command Note:i can also read all the columns and read all rows at a go with awk. But narrowing down to row/line at a time - sijui hiyo!
Reading the file i know how to- is not a problem - my script is doing, *but reading a line at a time then EXEC, line2 then EXEC, line3 then EXEC - that is my issue :)* * *
-- Best regards, Odhiambo WASHINGTON, Nairobi,KE +254733744121/+254722743223 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ I can't hear you -- I'm using the scrambler.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

On 21 June 2012 23:04, Laban Mwangi <lmwangi@gmail.com> wrote:
Did you want this? ╭─laban at ubuntu in /tmp @ '2012-06-21 21:41:15' ╰─○ head sms_file A +254XXXXX F RR A +255YYYYYY G HH A +256ZZZZZ K LL ╭─laban at ubuntu in /tmp @ '2012-06-21 21:41:24' ╰─○ IFS=" dquote> " ╭─laban at ubuntu in /tmp @ '2012-06-21 21:41:33' ╰─○ for line in $(< sms_file) for> do for> echo processing $line; for> field1=$(echo $line | awk '{print $1}'); phone_no=$(echo $line | awk '{print $2}'); field3=$(echo $line | awk '{print $3}'); for> echo "Processed -> $field1::$phone_no::$field3"; for> echo; for> done processing A +254XXXXX F RR Processed -> A::+254XXXXX::F
processing A +255YYYYYY G HH Processed -> A::+255YYYYYY::G
processing A +256ZZZZZ K LL Processed -> A::+256ZZZZZ::K
The trick here is using IFS (Internal Field Separator) to tell the shell to split on newlines not whitespace. However, that is serialized.. Let's slow things a bit by injecting some ZZZs..
╭─laban at ubuntu in /tmp @ '2012-06-21 21:51:24' ╰─○ sendsms () { line=$1; echo processing $line; field1=$(echo $line | awk '{print $1}'); phone_no=$(echo $line | awk '{print $2}'); field3=$(echo $line | awk '{print $3}'); sleep 5; echo "Processed -> $field1::$phone_no::$field3"; echo; } ╭─laban at ubuntu in /tmp @ '2012-06-21 21:51:52' ╰─○ {time (for line in $(< sms_file); do sendsms $line; done) }
Thankf for this Laban. Note an sms does not contain 3 fields - so this isnt an answer yet :)
processing A +254XXXXX F RR Processed -> A::+254XXXXX::F
processing A +255YYYYYY G HH Processed -> A::+255YYYYYY::G
processing A +256ZZZZZ K LL Processed -> A::+256ZZZZZ::K
( for line in $(< sms_file); do; sendsms $line; done; ) 0,01s user 0,02s system 0% cpu 15,057 total
dont need this.
15 seconds for 3 messages! Since you are into bulk sms you might want something a little bit faster. Enter xargs 1. Move sendsms into a standalone script that works on one line per invocation 2. Parallel invoke the sendsms with xargs -P
╭─laban at ubuntu in /tmp @ '2012-06-21 22:00:16' ╰─○ cat sendsms.sh
#!/bin/bash -
set -o nounset # Treat unset variables as an error
sendsms () { line=$1; echo processing $line; field1=$(echo $line | awk '{print $1}'); phone_no=$(echo $line | awk '{print $2}'); field3=$(echo $line | awk '{print $3}');
This calls for only 3 fileds. an sms could have something line " +254xyzabc this is getting into my nerves:), help me" UNless am missing something - mine will do better.
sleep 5; echo "Processed -> $field1::$phone_no::$field3"; echo; }
sendsms "$@" ╭─laban at ubuntu in /tmp @ '2012-06-21 22:00:28' ╰─○ {time (cat sms_file | xargs --max-procs=10 --max-args=1 --replace=Z ./sendsms.sh Z) } processing A +256ZZZZZ K LL processing A +254XXXXX F RR processing A +255YYYYYY G HH Processed -> A::+256ZZZZZ::K
Processed -> A::+255YYYYYY::G Processed -> A::+254XXXXX::F
( cat sms_file | xargs --max-procs=10 --max-args=1 --replace=Z ./sendsms.sh Z) 0,00s user 0,02s system 0% cpu 5,027 total
And we have (!web) shell scale :)
Where is the webshell?
On Thu, Jun 21, 2012 at 3:08 PM, Thuo Wilson <lixton@gmail.com> wrote:
Well, still scratching my head and am heading somewhere; This is doing exactly what i need BUT reading one line at a time untill the 10th line, if i want more i add manually... This might help some1 someday! But if there is a way to perfect it please do so.....
#!/bin/bash
smsfile=email_to_sms `grep Subject /var/spool/mail/sms >> email_to_sms` if [[ -s $smsfile ]] ; then cat /dev/null > /var/spool/mail/sms sed -i 's/Subject: //g' $smsfile echo `sed -n '1p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==1 {print $1}' $smsfile` echo `sed -n '2p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==2 {print $1}' $smsfile` echo `sed -n '3p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==3 {print $1}' $smsfile` echo `sed -n '4p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==4 {print $1}' $smsfile` echo `sed -n '5p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==5 {print $1}' $smsfile` echo `sed -n '6p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==6 {print $1}' $smsfile` echo `sed -n '7p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==7 {print $1}' $smsfile` echo `sed -n '8p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==8 {print $1}' $smsfile` echo `sed -n '9p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==9 {print $1}' $smsfile` echo `sed -n '10p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==10 {print $1}' $smsfile` else echo "***********Sorry the SMS FILE "$smsfile" is empty.************" fi /etc/init.d/gammu-smsd start cat email_to_sms >> email_to_sms2 cat /dev/null > email_to_sms
On 19 June 2012 12:48, Thuo Wilson <lixton@gmail.com> wrote:
On 18 June 2012 19:51, Odhiambo Washington <odhiambo@gmail.com> wrote:
On Mon, Jun 18, 2012 at 5:08 PM, Thuo Wilson <lixton@gmail.com> wrote:
On 18 June 2012 16:34, Odhiambo Washington <odhiambo@gmail.com> wrote:
On Mon, Jun 18, 2012 at 4:01 PM, Thuo Wilson <lixton@gmail.com>wrote:
> > > On 18 June 2012 10:52, gisho <gichuhie@gmail.com> wrote: > >> - as @wash said above, only move the specific file - not everything >> which is denoted by "*" so this should look like mv /tmp/$f /tmp2 >> >> - instead of reading mail spool, you can pipe incoming mails to a >> script either by using procmailrc or by using /etc/aliases file if using >> postfix. this could look like this: >> >> smsemail: "|/usr/libexec/postfix/trap.py" >> > > Thanks Gisho, > Thats is *part* of what i needed. I will use the alias. But note > supposing am doing bulk sms - this may [not affirmative] fail due to > numbers of emails < inflow... > Second: i decided to change script to read as follows; simpler but > doing better [?] > > Imagine i don't have the script called from alias. How do i read one > line at a time in the file "email_to_sms" and exec the command to send the > sms. That is my main issue > >
> Am sure its something simple i havent scratched my head enough! > > #!/bin/bash > `grep Subject /var/spool/mail/sms > email_to_sms` > cat /dev/null > /var/spool/mail/sms > killall -9 gammu-smsd #since for some reason > gammu-smsd is using use_lock and wont disable > echo `cat email_to_sms` | /usr/bin/gammu --sendsms TEXT `awk > '{print $2}' email_to_sms` > sleep 15 #wait for the above to finish > /etc/init.d/gammu-smsd start #start to continue checking any > incoming sms > cat email_to_sms >> email_to_sms2 #incase make a backup file > cat /dev/null > email_to_sms #empty for no2 message in the line :) > > for sure slowly we are getting there...
So the file email_to_sms is a text file containing some data you need to work on, each line at a time:
for data in `cat email_to_sms`; do echo $data | /usr/bin/gammu $ARGS .....; done
isnt this doing same thing as my script? :)
That will surely iterate through all the data in the file, one line at a time... You can do loop processing until there is nothing left, or always check if exists a file before you act on it..
However at this point, I suggest you work on a file which has been moved to another directory, so move the file first and reference it from where it is. Since the filenames seem to clash at some point (or why do you wven want to move the files?) then my initial suggestion stands - append the current timestamp value to the filename to make it unique.
Please try and tell breakdown what this command line does:
echo `cat email_to_sms` | /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms`
assuming the file content looks like:
A +254XXXXX F A +255YYYYYY G A +256ZZZZZ K
echo `cat email_to_sms` The command just get the file content - first part; /usr/bin/gammu --sendsms TEXT `awk '{print $2}' email_to_sms` - send an sms to the number on the second column (2) in the file email_to_sms
Then again, if you need a program to rexec, there is no need to give it a SIGKILL (-9). Just do a SIGHUP (-1) So you can rexec gammu after it completes processing the first file while making it loop to look for the existence of another file in the parent directory ... and the loop continues...
Not bad thought!
[wash@jaribu ~/THUO]$ pwd /home/wash/THUO
[wash@jaribu ~/THUO]$ ls -1 email_to_sms email_to_sms1 email_to_sms2 [wash@jaribu ~/THUO]$ file=`ls -1` [wash@jaribu ~/THUO]$ telno=`cat $file | awk '{print $2}'` [wash@jaribu ~/THUO]$ for t in $telno; do echo -e "$t\n"; done +254XXXXX
+255YYYYYY
+256ZZZZZ
+333XXXXX
+444YYYYYY
+555ZZZZZ
+6664XXXXX
+7774YYYYYY
+8884ZZZZZ
I hope this gives you some clue. It's a way to deal with each file at a time..
I want other text too
A +254XXXXX F RR A +255YYYYYY G HH A +256ZZZZZ K LL
My dilemma is this in simple terms.
Read line 1 then execute a command *if there is another line? "if"* Read line 2 then execute a command Read line 3 then execute a command Note:i can also read all the columns and read all rows at a go with awk. But narrowing down to row/line at a time - sijui hiyo!
Reading the file i know how to- is not a problem - my script is doing, *but reading a line at a time then EXEC, line2 then EXEC, line3 then EXEC - that is my issue :)* * *
-- Best regards, Odhiambo WASHINGTON, Nairobi,KE +254733744121/+254722743223 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ I can't hear you -- I'm using the scrambler.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

On Thu, Jun 21, 2012 at 4:08 PM, Thuo Wilson <lixton@gmail.com> wrote:
Well, still scratching my head and am heading somewhere; This is doing exactly what i need BUT reading one line at a time untill the 10th line, if i want more i add manually... This might help some1 someday! But if there is a way to perfect it please do so.....
#!/bin/bash
smsfile=email_to_sms `grep Subject /var/spool/mail/sms >> email_to_sms` if [[ -s $smsfile ]] ; then cat /dev/null > /var/spool/mail/sms sed -i 's/Subject: //g' $smsfile echo `sed -n '1p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==1 {print $1}' $smsfile` echo `sed -n '2p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==2 {print $1}' $smsfile` echo `sed -n '3p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==3 {print $1}' $smsfile` echo `sed -n '4p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==4 {print $1}' $smsfile` echo `sed -n '5p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==5 {print $1}' $smsfile` echo `sed -n '6p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==6 {print $1}' $smsfile` echo `sed -n '7p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==7 {print $1}' $smsfile` echo `sed -n '8p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==8 {print $1}' $smsfile` echo `sed -n '9p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==9 {print $1}' $smsfile` echo `sed -n '10p' $smsfile` | /usr/bin/gammu --sendsms TEXT `awk 'NR==10 {print $1}' $smsfile` else echo "***********Sorry the SMS FILE "$smsfile" is empty.************" fi /etc/init.d/gammu-smsd start cat email_to_sms >> email_to_sms2 cat /dev/null > email_to_sms
Thi is a rework of your script, though I did not test it yet.. #!/bin/sh smsfile=email_to_sms spoolfile=/var/spol/mail/sms grep Subject "$spoolfile" >> "$smsfile" if [ -s "$smsfile" ]; then : > "$spoolfile" sed -e 's/Subject: //g' "$smsfile" | awk ' { if (NR > 10) exit print | "/usr/bin/gammu --sendsms TEXT " $1 }' else echo "***********Sorry the SMS FILE "$smsfile" is empty.************" fi gammu-smsd start cat "$smsfile" >> email_to_sms2 : > "$smsfile" Note: If you remove/comment out the " if (NR > 10) exit", then all lines in the file will get processed. -- Best regards, Odhiambo WASHINGTON, Nairobi,KE +254733744121/+254722743223 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ I can't hear you -- I'm using the scrambler.
participants (4)
-
gisho
-
Laban Mwangi
-
Odhiambo Washington
-
Thuo Wilson