A question for the AWK wizards
Giorgos Keramidas
keramida at ceid.upatras.gr
Tue Jul 25 13:37:10 UTC 2006
On 2006-07-25 21:43, Murray Taylor <MTaylor at bytecraft.com.au> wrote:
> Hi all,
>
> I have a shell script which is called with an arbitrary message
> argument. Punctuation excludes * ? & | > < chars.
>
> It processes it via an AWK command line 'script' and dumps the result
> in a file for the SMS sender...
>
> Nice and simple.
>
> Except that the AWK script seems to duplicate the last character or
> two in the message. Everything else in the 200 odd lines of shell
> scripts surrounding this function run just fine, and this bit runs
> too, but this tiny thing is _VERY_ annoying.
>
> The shell code is listed below.
>
> Please teach me what bit I missed .... (C and TCL are my forte, not
> AWK)
> ------------------8<------------------------------
> # sourced into other scripts that need to SMS
> # !! 4 space indents, NOT tabs !!
> #
> # generate the sms message
> # the awk code forces the message to be < 160 chars
> sendsms() {
> msg=$1
>
> case ${sms_enable} in
> [Yy][Ee][Ss])
> for phone in ${phonelist}
> do
> tmpfile=`mktemp -t sms`
> echo ${phone} >> ${tmpfile}
> ${AWK} '{ printf "%-0.159s", $0 }' >> ${tmpfile} << EOF2
> `echo $msg`
> EOF2
> mv ${tmpfile} ${gsmspool_dir}
> done
> ;;
> *)
> ;;
> esac
> }
The above has a weird construct which can be simplified a bit:
| ${AWK} '{ printf "%-0.159s", $0 }' >> ${tmpfile} << EOF2
| `echo $msg`
| EOF2
You can write this as:
| echo "${msg}" | ${AWK} '{printf "%-0.159s", $0}' >> "${tmpfile}"
Are you deliberately avoiding to append a newline character to the
output of ${AWK} above? See the output of the two commands below,
as it's filtered through hd(1) utility.
| $ echo foo | awk '{ printf "%-0.159s", $0 }' | hd
| 00000000 66 6f 6f |foo|
| 00000003
| $ echo foo | awk '{ printf "%-0.159s\n", $0 }' | hd
| 00000000 66 6f 6f 0a |foo.|
| 00000004
| $
There is no problem with this part of the scripts you posted though.
They should work as expected. I'd probably look elsewhere for a bug
that causes the character duplication.
More information about the freebsd-questions
mailing list