Logo 
Search:

Unix / Linux / Ubuntu Forum

Ask Question   UnAnswered
Home » Forum » Unix / Linux / Ubuntu       RSS Feeds

Shell script question

  Date: Dec 02    Category: Unix / Linux / Ubuntu    Views: 993
  

I want to write a shell script that responds to each
key I hit on the keyboard (without requiring a carriage-
return after each such key stroke).

I know that shell scripts normally process a line of code
at a time. But what I want this time is essentially
an infinite loop that responds to each key stroke.
You can do that, of course, in C or Forth, or other
compiled languages. But I want to do it right at the
script level.

Unfortunately, I have not found any shell command that
provides for such action. Anyone have any ideas about how
to do this without branching out to some other language?

Share: 

 

3 Answers Found

 
Answer #1    Answered On: Dec 02    

My question is why? Compiled apts use less resource than a script that
has to be interpreted. Are you just trying to learn?

 
Answer #2    Answered On: Dec 02    

Your question, of course, is quite a good one. But there really
is a reason why I THINK I should do it this way.

I have written what amounts to a process-control program, and it
works quite well. Except for one problem: Rather inexperienced people will
have access to the keyboard and the display. Single
key pushes will determine the direction and amplitude of the process. But some
of the operators will make mistakes on the keyboard, and the processes need to
be protected from those mistakes. Control-C, for example, could bring the
program to a halt, with potentially devastating effects on the process under
control. And worse, some of these people have nothing good to say about
computers, and would be quite happy to see the entire "computer-control"
approach come to an inglorious end. Those types can probably be counted on to
"experiment" with undefined key strokes.

Clearly, some protection from "dis-allowed" keystrokes needs to be
built in. I wrote the original working program in gforth. But
the only potentially-available keyboard protection I know about in
Forth seems too inadequate. But in shell scripts you have the TRAP command,
which looks ideal for disallowing dangerous keystrokes. So I thought I might
well revert to the script level for this application (which does not require
blazing speed).

If you know of another, wiser, route to take, I would be very grateful to hear
about it. It even seems to me that this specific problem should be widespread
in computer applications; there ought to be some frequently-used solutions. But
I have not found any, despite a lot of hunting. But sadly, I am not any kind of
programming expert :)

 
Answer #3    Answered On: Dec 02    

I at one time could program in C++ but my mind won't work that way any
more. Remember that Linux is a multi-user multi-Processing system.
Setting up a user account will give the user only so many permissions as
set by the administrator/super user.

1. Keep all distractions off the desktop for your user accounts. (don't
offer any options not needed for your application)

2. Build a help file and menu that clearly defines the control functions
to reduce experimenting.

3. Only offer operator sign out and switch, not shut down or other
options on their desktop.

4. Build a keystroke call to the system that only responds to keystrokes
defined by the program, I don't remember the function call or name.

5. You can move the control key location in the keyboard layout in
system tools to make tampering more difficult.

6. Misbehaving applications can't take control of Linux as they can in
doze. If you must accept only keystrokes in your control that are
defined, and not any other keystroke. You may be better off with a
dedicated controller that accepts only communication from your control
computer that it understands (you have defined), via USB or serial cable.

At one time you could get a fourth compiler for the Micro chip pic
controllers, (The controllers I used in my applications). I however
programmed them in assembly so could not tell you how well those
compilers worked. I have notices some cross compilers in the
repositories that you might check out.

I hope this gave you some Ideas as to how to cure your dilemma.

 
Didn't find what you were looking for? Find more on Shell script question Or get search suggestion and latest updates.




Tagged: