I'm trying to write a shell script that runs periodically and if I'm not at the keyboard, it should do one thing, but if I am at the keyboard, it should do another thing. Therefore I've decided that it would be good if I could press down the CTRL key, (or some other key) and have the shell script detect the key state. I can't use "read" because the script will stop and wait for input, and I can't enter anything if I'm not at the keyboard.
You could try test -t to determine if stdin is a terminal (helps to identify if you running from cron (or piping input):
You could add a command line option to the script to prompt from data (or not), eg -b flag for batch mode.
If you really want to read the keystate under and you're using X11 you can use the xset -q command to query a number of things, including the keyboard LED mask. This is pretty hardware/system dependant and will probably not work over ssh connections, with character terminals or windows vt* terminal emulators, etc.
You could try test -t to determine if stdin is a terminal (helps to identify if you running from cron (or piping input):
You could add a command line option to the script to prompt from data (or not), eg -b flag for batch mode.
If you really want to read the keystate under and you're using X11 you can use the xset -q command to query a number of things, including the keyboard LED mask. This is pretty hardware/system dependant and will probably not work over ssh connections, with character terminals or windows vt* terminal emulators, etc.
None of those solutions will work. It's an initramfs script I'm working on, so there will no X11. I guess there is no simple solution to my problem. Thanks anyway.
So, I made this code, but it's not working and I have no idea why.
If I do something like 'cat /dev/input/event3' and press some keys, I get all sorts of garbage on the screen, so I know I can get output from the keyboard, but when I run the script '$VAR' stays empty. If I use a regular non-empty file or some other device, (like the hard disk) the script works.
I'm totally stumped. Any help would be much appreciated.
Clearly /dev/input/event3 is not a text file and therefore not suitable for reading with a Shell read. In fact there is little of interest to Shell programmers under /dev. (Yep I know about /dev/null etc.).
Be very very careful in /dev - it is remarkably easy to destroy your system.
What is the output from:
(Googling the device suggests that it might be a keyboard device on some versions of Linux - a fact of little use to Shell programmers).
What Operating System and version are you running?
What Shell are you running?
What are you trying to do? Can you explain the Project in words just in case there is a stock solution?
There will be kernel driver software between Shell and a keyboard.
If you have "bash" there is a Shell raw "read" with a timeout but even then it will not register keys like ctrl and shift as characters. Reading characters in a tight loop without timeout is CPU-intensive.
A common way to modify the behaviour of a cleverly designed running Shell script is to create a flag file or to use a Shell "trap" to react to a keyboard control code or signal.
What is an "initramfs script", what does it do, and why would you want to interrupt it?
Clearly /dev/input/event3 is not a text file and therefore not suitable for reading with a Shell read. In fact there is little of interest to Shell programmers under /dev. (Yep I know about /dev/null etc.).
Be very very careful in /dev - it is remarkably easy to destroy your system.
What is the output from:
(Googling the device suggests that it might be a keyboard device on some versions of Linux - a fact of little use to Shell programmers).
What Operating System and version are you running?
What Shell are you running?
What are you trying to do? Can you explain the Project in words just in case there is a stock solution?
There will be kernel driver software between Shell and a keyboard.
If you have "bash" there is a Shell raw "read" with a timeout but even then it will not register keys like ctrl and shift as characters. Reading characters in a tight loop without timeout is CPU-intensive.
A common way to modify the behaviour of a cleverly designed running Shell script is to create a flag file or to use a Shell "trap" to react to a keyboard control code or signal.
What is an "initramfs script", what does it do, and why would you want to interrupt it?
What runstate are you in while this script is running?
Now, here's my problem. I'm trying to unlock my encrypted system disk in Ubuntu 10.10. Sometimes I want to enter the passphrase via SSH and sometimes locally. Here's the piece of code that needs to be modified.
I need the script to run the first case if I want to enter the passphrase via the local keyboard, and run the second case if I want to enter the passphrase via SSH.
So, how do I notify the computer that I am at the keyboard? By pressing a key, perhaps.
This post is very difficult to understand for some reason. Maybe I'm being thick.
Quote:
I'm trying to write a shell script that runs periodically
This implies a regular cron job or some other automated job (hence the reply in post #2). This is clearly? not the case.
Best I can suggest is running the unix command "tty" when "at the keyboard". Hopefully it will reply "/dev/console". If it does, we have a simple way of distingishing where we are.
Btw. It would really help if you answered the questions.
With use of sed/awk, how can I print all possible combinations of a word with caps/non-caps.
Eg
Applying operation on "cap" should generate output as follows.
cap
CAP
Cap
cAp
caP
CAp
cAP
CaP (12 Replies)
I want to detect key pressed in my .cgi web page, but it does not work even I found the code in other web site.
My code is :
#!/bin/sh
#=================================================
# PATH defination
# ================================================... (2 Replies)
hi all
this s quite a foolish problem. I seem to hav pressed some keys s.t in unix, my letters are comin in caps and with caps lock on, i am getting lowercase letters. :o
Pls help.
Also is there any reference or manual where i can check in case such problems arrise?
thanx in advance
curiosity (4 Replies)
Hello,
Not sure if this is the right place to post it but..
I have a requirement to set Caps lock and/or Num lock on and off via a Cron job.
Now I have working scripts that do the job, but as soon as X starts up the jobs no longer run (well they appear to, but Caps lock and Num lock do not... (0 Replies)
My problem is that i have to connect Linux server. I can connect it with SSH but because of the software of the Linux server, i need to press enter and after ctrl+D. Therefore, I need to write a script that should press enter and ctrl+D. How can i write it in KSH shell script. HELP ME! (7 Replies)
Hello,
We are having a problem with running dtterm off a RHEL server. Logging into an HP-UX server from a RHEL 5.1 desktop, setting DISPLAY and running dtterm, the caps lock does not work. We have been playing with xmodmap & stty but to no avail.
Any help appreciated.
mgb (7 Replies)
Hello,
We are having a problem with running dtterm off a RHEL server. Logging into an HP-UX server from a RHEL 5.1 desktop, setting DISPLAY and running dtterm, the caps lock does not work. We have been playing with xmodmap & stty but to no avail.
Any help appreciated.
mgb (1 Reply)
Hi all,
My query is... in the runtime, you are getting any input string. Unfortunately, you have pressed some ctrl keys or esc keys or arrow keys while typing input string. You can get the input value like that...
input string as
welcome^
So ,I want to remove those unwanted keys... (4 Replies)
Hi
I have tried to disable the CTRL-Z key inside a shell(sh) script using the command trap "`echo "Ctrl-Z key disabled"`" 20But I am not able to exit from the script after pressing CTRL-Z key.
How to proceed this? Need reply soon (11 Replies)