A bash scripting question

Devin Teske devin.teske at fisglobal.com
Thu Jun 21 16:24:24 UTC 2012


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.


More information about the freebsd-questions mailing list