[Gllug] Stripping whitespace in bash script

ccooke ccooke-gllug at gkhs.net
Sun Aug 13 01:41:53 UTC 2006


On Sun, Aug 13, 2006 at 02:03:11AM +0100, Joel Bernstein wrote:
> On Sun, Aug 13, 2006 at 01:28:35AM +0100, Tethys wrote:
> > 
> > Nix writes:
> > 
> > >A *user* of GNU awk's networking facilities?
> > >
> > >(Is this the first?)
> > 
> > Perhaps. But I can probably beat that. One of my former colleagues
> > has taken to writing networking code in shell scripts. Gotta love
> > bash...
> 
> Uh...what? Bash has sockets now?
> 

For some while now - it took them from the Korn shell. If
support is compiled in, you just work on a file /dev/tcp/hostname/port
or /dev/udp/hostname/port. For most applications, you'd be doing
something like...

exec 3 <> /dev/tcp/hostname/port

... after which you can read from and write to file descriptor 3. An
alternative I've used is:

function connect_and_do_stuff
{
	: # some code...
}

connect_and_do_stuff <> /dev/tcp/hostname/port (Although this syntax
may fall afoul of bash's annoying and strange 'some stuff runs in a 
subshell' pitfall.)

(I've written two IRC clients in shell - pdksh and ksh93, along with an
RFC-compliant ESMTP server in bash. The latter was for an embedded linux
system which had bash installed but nothing like perl. My smtp server
was perfectly fast enough for the volume of mail being sent from a
palmtop and took up about one twentieth of the space any of the compiled
ones I could have installed would have used - and that's without
considering adding perl to the device.)

Bah. I never seem to talk about anything but shell on this list. In the
interest of balance: Given a choice of python or ruby, what would be the
best language to learn next?

-- 
Z=$[(X=`tput cols`)*(Y=`tput lines`)];O=(X+1 X X-1 1);a(){ for((p=0;p<Z;p++));\
do z=$[y=p/X+1,x=p%X+1,0];q=${B[p]};[ $i ]||B[p]=$[RANDOM%3];for e in ${O[@]} \
${O[@]//#/-};do(((j=p-e)>0))&&((0${B[j]}&&z++));done;c=\ ;((z==3))&&c=1;((z==2\
))&&c=$q;echo -ne "\E[$y;${x}H$c";A[p]=$c;done;i=1;B=("${A[@]}");a;};a # ccooke
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list