Unix Shell - character count in ksh

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > November 2005 > character count in ksh





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author character count in ksh
Dutchie

2005-11-09, 5:53 pm

I've made a ksh script to sent SMS messages from an AIX server. Now i
want to build in a
counter that counts down from 160 when i start typing the actual sms
text.

Does anybody has any suggestions?

Regards,

Arjan

Janis Papanagnou

2005-11-09, 5:53 pm

Dutchie wrote:
> I've made a ksh script to sent SMS messages from an AIX server. Now i
> want to build in a
> counter that counts down from 160 when i start typing the actual sms
> text.
>
> Does anybody has any suggestions?


To read at most 200 characters newer ksh support...

read -n 200

or read in everything and determine the length before sending it

read v
# then test ${#v} against limit


Janis
Rodrick Brown

2005-11-09, 5:53 pm


"Janis Papanagnou" <Janis_Papanagnou@hotmail.com> wrote in message
news:dkqes9$l8m$1@online.de...
> Dutchie wrote:
>
> To read at most 200 characters newer ksh support...
>
> read -n 200
>
> or read in everything and determine the length before sending it
>
> read v
> # then test ${#v} against limit
>
>
> Janis


Actually the easiest way of doing what you want is possibly using PERL or C
or anything that can select()/poll stdin to have it update in real time you
will need to use some kind of curses based library.

--
Rodrick R. Brown
Unix Systems Admin
http://www.rodrickbrown.com
rodrick.brown[@]gmail.com

When in 1986 Apple bought a Cray X-MP and announced that they would use it
to design the next Apple Macintosh, Seymour Cray replied, "This is very
interesting because I am using an Apple Macintosh to design the Cray-2
supercomputer."


bsh

2005-11-09, 5:53 pm


Janis Papanagnou wrote:
> Dutchie wrote:
> read -n 200
> or read in everything and determine the length before sending it
> read v
> # then test ${#v} against limit


Yes. This is a very good solution.

Rodrick Brown wrote:
> Actually the easiest way of doing what you want is
> possibly using PERL or C or anything that can
> select()/poll stdin to have it update in real time you
> will need to use some kind of curses based library.


You PERL guys!! Always using a microtome to open an envelope
when just ripping it with your teeth is sufficient! An underused
feature of ksh88 and newer is reading and writing from
/dev/tcp/<ip#>/<port#> or /dev/udp/<ip#>/<port#> to do
network transfers. Already built into ksh! netcat(1) or telnet(1)
are not necessary (I've seen scripts that implement mail(1)
without using external network tools).

>From the ksh93 FAQ at kornshell.com, talking about non-buffered

read input parsing, which can be extended to undertand how to
dynamically determine character length:

How can a write a ksh script that responds directly to each
character so that you user just has to enter y, not y<return>?

There are two ways to do this. The easiest is to use:

read -n1 x

Alternatively, you could do:

function keytrap
{
.sh.edchar=${sh.edchar}$'\n'
}
trap keytrap KEYBD

and then

read x

To wit, I would try:

function keytrap
{
(( ${#.sh.edchar}>=159 )) && .sh.edchar=${sh.edchar}$'\n'
}
trap keytrap KEYBD
read x

The above is not tested.

Alternately, a third technique is simple truncation, again
with ksh93(1) builtin operators:

SMS_MSG=${SMS_MSG:0:160} # or ${SMS_MSG:0:159} if NL is appended

I understand that not everybody has ksh version 1993 or newer.
It is available free under license from kornshell.com and some
hosts have it already installed under dtksh(1).

=Brian

Rodrick Brown

2005-11-09, 5:53 pm

On 2005-11-08 19:17:28 -0500, "bsh" <brian_hiles@rocketmail.com> said:

>
> Janis Papanagnou wrote:
>
> Yes. This is a very good solution.
>
> Rodrick Brown wrote:
>
> You PERL guys!! Always using a microtome to open an envelope
> when just ripping it with your teeth is sufficient! An underused
> feature of ksh88 and newer is reading and writing from
> /dev/tcp/<ip#>/<port#> or /dev/udp/<ip#>/<port#> to do
> network transfers. Already built into ksh! netcat(1) or telnet(1)
> are not necessary (I've seen scripts that implement mail(1)
> without using external network tools).
>
> read input parsing, which can be extended to undertand how to
> dynamically determine character length:
>
> How can a write a ksh script that responds directly to each
> character so that you user just has to enter y, not y<return>?
>
> There are two ways to do this. The easiest is to use:
>
> read -n1 x
>
> Alternatively, you could do:
>
> function keytrap
> {
> .sh.edchar=${sh.edchar}$'\n'
> }
> trap keytrap KEYBD
>
> and then
>
> read x
>
> To wit, I would try:
>
> function keytrap
> {
> (( ${#.sh.edchar}>=159 )) && .sh.edchar=${sh.edchar}$'\n'
> }
> trap keytrap KEYBD
> read x
>
> The above is not tested.
>
> Alternately, a third technique is simple truncation, again
> with ksh93(1) builtin operators:
>
> SMS_MSG=${SMS_MSG:0:160} # or ${SMS_MSG:0:159} if NL is appended
>
> I understand that not everybody has ksh version 1993 or newer.
> It is available free under license from kornshell.com and some
> hosts have it already installed under dtksh(1).
>
> =Brian


perl just makes it so easy sometime I easily forget this is comp.unix.shells
--
Rodrick R. Brown
Senior IT Consultant
http://www.rodrickbrown.com

bsh

2005-11-10, 8:53 pm

Rodrick Brown wrote:
> PERL just makes it so easy sometime I easily forget this is comp.unix.shells


I disagree with you that PERL makes it necessarily "easy" in the sense
of
the level of abstraction, but will readily agree with you that perl, in
the sense
that however sophisticated the syntax, it's at least almost always
possible!
This is perhaps the fundamental criterion of the quality of any
programming
language.

=Brian

Chris F.A. Johnson

2005-11-11, 2:54 am

On 2005-11-09, Rodrick Brown wrote:
>
> PERL just makes it so easy sometime I easily forget this is comp.unix.shells


But it's not; it's comp.unix.shell. There is a moribund group of
that name.

My few attempts at learning PERL quickly drove me back to a sane
language, the shell.

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com