keypress event in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting keypress event in shell script
# 1  
Old 08-13-2008
keypress event in shell script

Hi All,


How can I trap a character press in the shell script.
For eg:- I have a script runinng a infinite loops , I will need to dispay menu asking for run process of to stop process and process stauts like we do in glance


I have seen the traping the signal in glance command,
# 2  
Old 08-13-2008
The way I tend to do it is:

Code:
GetKeystroke () {
oldSttySettings=`stty -g`
stty -echo raw
echo "`dd count=1 2> /dev/null`"
stty $oldSttySettings
}

Then you can do something like:

action="X"
while [ `echo "DHPQS"  | grep -v $action` ]
do
  action=`GetKeystroke | cut -c1 | tr "[a-z]" "[A-Z]"`
done

echo $action

HTH - and any improvements on my code appreciated :-)
# 3  
Old 08-13-2008
hi,

please see below code. what i exactly need is if i press key
m then menu
h then help



while true ; do
m_date=`date +'%d/%m/%Y %H:%M:%S'`
process1=$(ps -ef | grep process1 |grep -v grep|wc -l)
process2=$(ps -ef | grep process2 |grep -v grep|wc -l)
count_process1_files=`ls /process1/|grep .txt |grep -v PROCESSED|wc -l 2> tt`
count_process2_files=`ls /process2/|grep .txt |wc -l 2> tt`
if [ $process1 -eq 0 ]
then
process1_status='Not Running'
else
process1_status='Running'
fi

if [ $process2 -eq 0 ]
then
process2_status='Not Running'
else
process2_status='Running'
fi

echo "\n\n\n"
echo ' ------------------------------------------------------------------------'
echo ' | Process : Running status : Intance : Files Pending |'
echo ' ------------------------------------------------------------------------'
echo ' | process1 : ' $process1_status' : ' $process1' : '$count_process1_files' |'
echo ' ------------------------------------------------------------------------'
echo ' | process2 : ' $process2_status' : ' $process2' : '$count_process2_files' |'
echo ' ------------------------------------------------------------------------'



sleep 8
clear
done


i want to do is if i press M/m key then script should display the menu
i.e
a. do u want to start process
b. do u want to stop process
if i select a then it should ask for
process 1 or process2 etc
# 4  
Old 08-14-2008
keypress event in shell script

please refer below code.
my script is running in while loop.
while true ; do
m_date=`date +'%d/%m/%Y %H:%M:%S'`
process1=$(ps -ef | grep process1 |grep -v grep|wc -l)
process2=$(ps -ef | grep process2 |grep -v grep|wc -l)
count_process1_files=`ls /process1/|grep .txt |grep -v PROCESSED|wc -l 2> tt`
count_process2_files=`ls /process2/|grep .txt |wc -l 2> tt`
if [ $process1 -eq 0 ]
then
process1_status='Not Running'
else
process1_status='Running'
fi
if [ $process2 -eq 0 ]
then
process2_status='Not Running'
else
process2_status='Running'
fi
echo "\n\n\n"
echo ' ------------------------------------------------------------------------'
echo ' | Process : Running status : Intance : Files Pending |'
echo ' ------------------------------------------------------------------------'
echo ' | process1 : ' $process1_status' : ' $process1' : '$count_process1_files' |'
echo ' ------------------------------------------------------------------------'
echo ' | process2 : ' $process2_status' : ' $process2' : '$count_process2_files' |'
echo ' ------------------------------------------------------------------------'

sleep 8
clear
done

i want to do is if i press M/m key then script should display the menu
i.e
a. do u want to start process
b. do u want to stop process
if i select a then it should ask for
process 1 or process2 etc
# 5  
Old 08-14-2008
hi all.

please help.
# 6  
Old 08-14-2008
please do not multiple post

This is a duplicate of the thread:

https://www.unix.com/unix-advanced-ex...ll-script.html

Also - pleaes do not "bump" your threads.

Both these activities are frowned upon......
# 7  
Old 08-14-2008
hi,


i am unable to use u r code . please use u r code in my sample script and revert with code.

thanx in advance!
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Simulate keypress in bash

Hello everybody, I am using Windows 10 and cygwin/bash. I need to write a script in bash which simulates opening of a program and then press some keys such as F5, ENTER and ALT+F4. I have written a VBScript which does the job Set WshShell = WScript.CreateObject("WScript.Shell")... (9 Replies)
Discussion started by: supernono06
9 Replies

2. Shell Programming and Scripting

Shell script to poll a directory and stop upon an event

Need shell script to: 1/keep polling a directory "receive_dir" irrespective of having files or no files in it. 2/move the files over to another directory "send_dir". 3/the script should only stop polling upon a file "stopfile" get moved to "receive_dir". Thanks !! My script: until do... (0 Replies)
Discussion started by: iaav
0 Replies

3. Shell Programming and Scripting

Shell script to execute condition until an event occurs

I need a script to keep polling "receive_dir" directory till "stopfile" get written in the directory. This has to run despite empty directory. So far i have this but fails if receive_dir is empty with no files with "unary operator expected". Help !! #!/usr/bin/ksh until do for i... (1 Reply)
Discussion started by: iaav
1 Replies

4. Programming

get keypress c++Qt

Hi i need to get the key pressed from the keyboard without focus on the application. i work under CentOS 5.5 with QtC++ 4.7 and Python Script (0 Replies)
Discussion started by: HanyM.Magdy
0 Replies

5. UNIX for Dummies Questions & Answers

How do i detect a keypress such as Page-Down ?

Hi, New user here, I have googled this but found nothing that helps me. I have a file of data and want to display it to a user from within a "frame/window/screen" that allows the user to pageup/pagedown through the data. I can do this mostly, but I cannot detect the pressing of the... (6 Replies)
Discussion started by: gcraill
6 Replies

6. Shell Programming and Scripting

Sending an event from another bash shell

I have two BASH shells and would like to send data from one to the other. To achieve this, I have thought the following: 1) Shell #1 runs a script in order to create the file "output.out" with the data. 2) Meanwhile Shell #2 is waiting for "output.out" is created for starting with a second script... (6 Replies)
Discussion started by: hresquivelo
6 Replies

7. UNIX for Dummies Questions & Answers

Can we trigger an shell script on an event

Hi, My program A updates a log called logA. I have a shell script S that is responsible to send emails reading from the log. I want to trigger execution of the script whenever there is an update to the log. Thanks in advance. (8 Replies)
Discussion started by: cv_pan
8 Replies

8. Programming

keypress signals

How-to use keypress action signals in C? Is there any nice select or poll functions for that? (1 Reply)
Discussion started by: Esaia
1 Replies
Login or Register to Ask a Question