[ Date Index ] [ Thread Index ] [ <= Previous by date / thread ] [ Next by date / thread => ]
On 19 Apr, 2013, at 9:36 pm, Grant Phillips-Sewell wrote:
I'm trying to write a little script that can read a file and invoke twidge to tweet its contents to Twitter. The # symbol, being a special character (I think) is the root the my problems... and being Twitter, the # symbol is going to be used a fair bit. If I invoke twidge from the command line as this: $ twidge update "#generic #twitter #update" then all is well. The # symbol(s) are wrapped up in "" nicely.I haven't got to the reading-from-a-file bit yet, but here's what I have:#!/bin/sh # Send Twitter updates through Twidge command="twidge update" status="$@" $command $status As it is, if I have no # symbols, it will work fine... however pop a # in there and it fails. It also fails with # symbols if I wrap the last line as: $command "$status" As it is, if I escape the # symbols, then it works fine... so I crack out sed...: status=`echo "$@" | sed -e "s/#/\#/g"` ... fails status=`echo "$@" | sed -e "s/\#/\\#/g"` ... fails (escaping the # symbol in sed) status=`echo "$@" | sed -e "s/\#/\\\#/g"` ... fails (escaping the # symbol and the \ symbol in the replacement string) Now, regexes of any nature have never been my strong point. I was successful withstatus=`echo "$@" | sed -e "s/~/#/g"` ... (replacing the ~ with the # symbol)but I don't really want my text file(s) to all have ~ instead of # symbols if I can get away with it. Anybody got any ideas?
We've seen several good ones that should be getting you closer to a solution: check your quoting and escaping, try piping to stdin, use $* not $@, use variables not literals, replace # with ~ for intermediate steps...
Couple more, neither of them as useful as the foregoing, I fear:1. (Optimization) Wherever you see 'sed s/x/y/g' where x and y are both single characters, think 'tr' instead.
2. Several of the foregoing tips are variations on the theme of indirection. Remember, in *n*x everything is a file. Rather than shell variables in RAM, try putting your data into temp files on disk at each step -- the shell is probably doing that behind the scenes if the data are non-trivial anyway. This gives you out-of-band diagnostic capabilities, which are very useful. Once you've got things working, optimize the files away again if you must.
> tr '#' '~' < raw_messages.txt > scrubbed_messages.txt > head -1 scrubbed-messages.txt > scrubbed_message_1.txt > twidge update < scrubbed_message_1.txt Or something like that. -- Phil Hudson http://hudson-it.no-ip.biz @UWascalWabbit PGP/GnuPG ID: 0x887DCA63 -- The Mailing List for the Devon & Cornwall LUG http://mailman.dclug.org.uk/listinfo/list FAQ: http://www.dcglug.org.uk/listfaq