Re: A bash scripting question
On Jun 21, 2012, at 6:40 AM, Odhiambo Washington wrote: How Can I simplify/perfect the following script, so that I read _ALL_ the lines in the file and act on the content as shown below, so that I do not have to specifiy an action per line? This below 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. #!/usr/local/bin/bash smsfile=email_to_sms `grep Subject /var/spool/mail/sms $smsfile` 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 gammu-smsd start cat email_to_sms email_to_sms2 cat /dev/null email_to_sms Try the following… #!/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 -- Devin _ The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: A bash scripting question
On 06/21/2012 08:40 AM, Odhiambo Washington wrote: How Can I simplify/perfect the following script, so that I read _ALL_ the lines in the file and act on the content as shown below, so that I do not have to specifiy an action per line? This below 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. #!/usr/local/bin/bash smsfile=email_to_sms `grep Subject /var/spool/mail/sms $smsfile` 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 gammu-smsd start cat email_to_sms email_to_sms2 cat /dev/null email_to_sms Off the top of my head: 8 #!/bin/sh -e sed -e '/^Subject: /!d; s/^Subject: //' /var/spool/mail/sms ${smsfile} :/var/spool/mail/sms xargs -L1 /usr/bin/gammu --sendsms TEXT ${smsfile} mv -f ${smsfile} ${smsfile}.bak 8 No loops necessary. By the way, what's gammu, and why is it in /usr/bin ? -- Fuzzy love, -CyberLeo Technical Administrator CyberLeo.Net Webhosting http://www.CyberLeo.Net cyber...@cyberleo.net Furry Peace! - http://.fur.com/peace/ ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: A bash scripting question
CyberLeo Kitsana wrote Odhiambo Washington: By the way, what's gammu, /usr/ports/comms/gammu presumably ( for mobile phone connection ) and why is it in /usr/bin ? Pass. Cheers, Julian -- Julian Stacey, BSD Unix Linux C Sys Eng Consultants Munich http://berklix.com Reply below not above, cumulative like a play script, indent with . Format: Plain text. Not HTML, multipart/alternative, base64, quoted-printable. Mail from @yahoo dumped @berklix. http://berklix.org/yahoo/ ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
RE: A bash scripting question
-Original Message- From: owner-freebsd-questi...@freebsd.org [mailto:owner-freebsd- questi...@freebsd.org] On Behalf Of Devin Teske Sent: Thursday, June 21, 2012 9:24 AM To: Odhiambo Washington Cc: questions Subject: Re: A bash scripting question On Jun 21, 2012, at 6:40 AM, Odhiambo Washington wrote: How Can I simplify/perfect the following script, so that I read _ALL_ the lines in the file and act on the content as shown below, so that I do not have to specifiy an action per line? This below 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. #!/usr/local/bin/bash smsfile=email_to_sms `grep Subject /var/spool/mail/sms $smsfile` 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 gammu-smsd start cat email_to_sms email_to_sms2 cat /dev/null email_to_sms Try the following. #!/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 I can beat my original response (above), while retaining original functionality... #!/bin/sh spoolfile=/var/spool/mail/sms awk -v pat=^Subject: ' $0 ~ pat { sub(pat,) print | /usr/bin/gammu --sendsms TEXT $1 }' $spoolfile Or, as a shell One-Liner (compatible with any shell and any awk)... awk -v pat=^Subject: '$0~pat{sub(pat,);print|/usr/bin/gammu --sendsms TEXT $1}' $spoolfile -- Devin _ The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
RE: A bash scripting question
-Original Message- From: owner-freebsd-questi...@freebsd.org [mailto:owner-freebsd- questi...@freebsd.org] On Behalf Of dte...@freebsd.org Sent: Thursday, June 21, 2012 12:57 PM To: 'Odhiambo Washington' Cc: 'questions' Subject: RE: A bash scripting question -Original Message- From: owner-freebsd-questi...@freebsd.org [mailto:owner-freebsd- questi...@freebsd.org] On Behalf Of Devin Teske Sent: Thursday, June 21, 2012 9:24 AM To: Odhiambo Washington Cc: questions Subject: Re: A bash scripting question On Jun 21, 2012, at 6:40 AM, Odhiambo Washington wrote: How Can I simplify/perfect the following script, so that I read _ALL_ the lines in the file and act on the content as shown below, so that I do not have to specifiy an action per line? This below 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. #!/usr/local/bin/bash smsfile=email_to_sms `grep Subject /var/spool/mail/sms $smsfile` 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 gammu-smsd start cat email_to_sms email_to_sms2 cat /dev/null email_to_sms Try the following. #!/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 I can beat my original response (above), while retaining original functionality... #!/bin/sh spoolfile=/var/spool/mail/sms awk -v pat=^Subject: ' $0 ~ pat { sub(pat,) print | /usr/bin/gammu --sendsms TEXT $1 }' $spoolfile Actually, some functionality was lost in the above translation, let me add the missing functionality back-in... #!/bin/sh spoolfile=/var/spool/mail/sms awk -v pat=^Subject: ' BEGIN { found = 0 } $0 ~ pat { found++ sub(pat, ) print | /usr/bin/gammu --sendsms TEXT $1 } END { if ( ! found ) printf %sSorry the SMS FILE \%s\ is empty.%s\n, ***, FILENAME, *** exit ! found }' $spoolfile : $spoolfile Or, as a shell One-Liner (compatible with any shell and any awk)... The above doesn't translate so-well into a one-liner (unless you can stomach really long lines 80 chars), but here it is... awk -v pat=^Subject: 'BEGIN{found=0}$0~pat{found++;sub(pat, );print|/usr/bin/gammu --sendsms TEXT $1}END{if(!found)printf %sSorry the SMS FILE \%s\ is empty.%s\n,***,FILENAME,***;exit !found}' /var/spool/mail/sms : /var/spool/mail/sms -- Devin P.S. I think the above is the best you can do. _ The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org