Run a command for specific amount of time with an auto key press


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Run a command for specific amount of time with an auto key press
# 1  
Old 05-16-2015
Run a command for specific amount of time with an auto key press

Hi,

I have been trying to do a small fun project for myself.

I want to run a command for 45 seconds.

And to get the final output of this command, the script requires I push the "q" key on my keyboard and then the final output file becomes available.

I tried the following script. But it is not taking the "q" key and also runs for more than 45 seconds. I was wondering if someone could lead me on how to press a key automatically inside a bash script.

Code:
#! /bin/bash
end=$((SECONDS+45))
while [ $SECONDS -lt $end ];
do
my command execution
done
echo q

Thanks

Last edited by jacobs.smith; 05-16-2015 at 08:33 PM..
# 2  
Old 05-17-2015
Not sure if this is the optimal solution, and it requires "my command" to read from stdin:
Code:
mkfifo YY
{ sleep 45; echo q >YY; } &

read -p "my command execution" R <YY; [[ "$R" = "q" ]] && echo "back!" || echo "failed";    # this is mimicking "my command"
rm -f YY


Last edited by RudiC; 05-17-2015 at 06:00 AM.. Reason: removed "echo" from prompt
This User Gave Thanks to RudiC For This Post:
# 3  
Old 05-17-2015
Quote:
Originally Posted by RudiC
Not sure if this is the optimal solution, and it requires "my command" to read from stdin:
Code:
mkfifo YY
{ sleep 45; echo q >YY; } &

read -p "my command execution" R <YY; [[ "$R" = "q" ]] && echo "back!" || echo "failed";    # this is mimicking "my command"
rm -f YY

Rudic,

Thanks for your response. But no luck so far. The script runs but it is creating a file called YY and no output is being written into it.

To briefly tell you what I am planning to do

Code:
I want to execute a command for 45 seconds, then press the key "q". I want to loop this for 3600 seconds.

Thanks again.
# 4  
Old 05-17-2015
Works for me, please provide screen output, like:
Code:
✔ tmp $ mkfifo YY
✔ tmp $ { sleep 45; echo q >YY; } &
[1] 30908
+ tmp $ 
✔ tmp $ read -p "my command execution" R <YY; [[ "$R" = "q" ]] && echo "back!" || echo "failed";    # this is mimicking "my command"
back!
[1]+  Fertig                  { sleep 45; echo q > YY; }
✔ tmp $ rm -f YY

This User Gave Thanks to sea For This Post:
# 5  
Old 05-17-2015
Quote:
Originally Posted by sea
Works for me, please provide screen output, like:
Code:
✔ tmp $ mkfifo YY
✔ tmp $ { sleep 45; echo q >YY; } &
[1] 30908
+ tmp $ 
✔ tmp $ read -p "my command execution" R <YY; [[ "$R" = "q" ]] && echo "back!" || echo "failed";    # this is mimicking "my command"
back!
[1]+  Fertig                  { sleep 45; echo q > YY; }
✔ tmp $ rm -f YY

Please see this.

Code:
user$ mkfifo YY
user$ { sleep 45; echo q >YY; } &

[1] 33912
Code:
user$ read -p "ls -lh" R <YY; [[ "$R" = "q" ]] && echo "back!" || echo "failed";

-bash: !": event not found
Code:
user$ read -p "ls -lh" R <YY; [[ "$R" = "q" ]] && echo "back" || echo "failed";

back
[1]+ Done { sleep 45; echo q > YY; }
Code:
user$ rm -f YY

No output file is generated.
# 6  
Old 05-17-2015
Quote:
Originally Posted by jacobs.smith
Please see this.
...
-bash: !": event not found
...
What is the output of:
Code:
uname -ro
$SHELL --version

I could only think of either one of these infos beeing related to your experienced issue, though i dont know a solution.

Quote:
Originally Posted by jacobs.smith
Code:
user$ rm -f YY

No output file is generated.
Obvious, after deleting it Smilie
You do not need that file anyway, as it simply 'redirects'(creates) the virtual keypress of q.

I do have it:
Code:
[1]+  Fertig                  { sleep 45; echo q > YY; }
+ tmp $ rm -f YY^C
130 tmp $ ll YY
prw-rw-r--. 1 sea sea 0 17. Mai 16:56 YY

hth
# 7  
Old 05-17-2015
Quote:
Originally Posted by sea
What is the output of:
Code:
uname -ro
$SHELL --version

I could only think of either one of these infos beeing related to your experienced issue, though i dont know a solution.


Obvious, after deleting it Smilie
You do not need that file anyway, as it simply 'redirects'(creates) the virtual keypress of q.

I do have it:
Code:
[1]+  Fertig                  { sleep 45; echo q > YY; }
+ tmp $ rm -f YY^C
130 tmp $ ll YY
prw-rw-r--. 1 sea sea 0 17. Mai 16:56 YY

hth
Thanks for the pointers. Here is the requested info

Code:
uname -ro

uname: illegal option -- o
usage: uname [-amnprsv]

Code:
user$ $SHELL --version

GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin10.0)
Copyright (C) 2007 Free Software Foundation, Inc.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to run commands at a specific time.

Hello All, I have written a script which which is working fine to a certain logic of it. But i want a part of the script to run two commands at 00:10 hrs every day. These two command are 1. rm -rf /path/to/folder 2. mail the content of a file. How do i achieve this. Thanks. ... (4 Replies)
Discussion started by: Siddheshk
4 Replies

2. Shell Programming and Scripting

How to detect key press in cgi shell script?

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)
Discussion started by: Shuinvy
2 Replies

3. Shell Programming and Scripting

Press Any Key script sequence using bash - HELP

hi to all. im a newbie in unix shell scripts. i want to make a simple unix shell script using the bash shell that asks a user to press any key after a series of commands, or an x if he wishes to exit. here's a sample script that i made: #!/usr/bin/bash pause(){ /usr/bin/echo "\t\t Press... (3 Replies)
Discussion started by: booghaw
3 Replies

4. Shell Programming and Scripting

Generate specific amount of same characters

Hi, Is there a command to print one character x amont of times? I need for example 10 comma's (,,,,,,,,,,). Instead of creating a loop, I was wondering if there is a way to do this with sed or awk? Thanks! (3 Replies)
Discussion started by: Subbeh
3 Replies

5. Shell Programming and Scripting

specific number jobs run at the same time

Hi, I have more that 100 jobs I can run background, if I want only 4 running at ther same time, how can I write a script to control it? Thanks peter (0 Replies)
Discussion started by: laopi
0 Replies

6. Shell Programming and Scripting

Any key press causing logout from shell

Hi all! I have written a shell script which will invoke perl script infinitly in the background in a loop. Code will do as:Within while loop, perl script will be run in background, get the pid and notify pid in though mail. then wait for pid to be completed before going for next iteration. I am... (1 Reply)
Discussion started by: jramesh1
1 Replies

7. UNIX for Dummies Questions & Answers

Run Command in Specific Time ...

Guy's I want to make script to run this command solevel every Saturday at 8:00 clock exactly . Can you please help me and teach me how to do this ... (9 Replies)
Discussion started by: IT Helper
9 Replies

8. Shell Programming and Scripting

Trap key press in a script

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 quit if q is pressed. I have seen the traping the signal , but they give option only for traping the defined interrupt signals. But those does not help me here. (3 Replies)
Discussion started by: praveenbvarrier
3 Replies

9. UNIX for Dummies Questions & Answers

Capture an empty key press...

I am trying to test input from the user, if they press enter with out an Y or N. I have the characheter thing sorted but when it comes to a blank or empty key press I am having trouble. if ; then clear echo "Sorry, that is an invalid choice!" exit fi I am using a KSH script in... (3 Replies)
Discussion started by: jagannatha
3 Replies

10. Shell Programming and Scripting

Read line with a single key press...

I would really like to have a script that will accept the key press from the user with out having to press the enter key afterwards. i.e. echo "Press Y to print \c" read YesNo At this point the user has to press the enter key to continue. Is there a way to accept the key press from the... (3 Replies)
Discussion started by: jagannatha
3 Replies
Login or Register to Ask a Question