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
# 8  
Old 05-17-2015
Anway, if you havent tried yet, start a new terminal window via gui-icon and retry RudiC's suggestion.
At some times, shell raises errors for standard stuff it supports, if one has been tinkering too much with it in previous tryouts...

But if there is no change, i'd guess bash beeing the reason, as there's been some updates of it:
Code:
$SHELL --version
GNU bash, Version 4.3.33(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL Version 3 or newer <http://gnu.org/licenses/gpl.html>

Sorry cant help you further.
Have a good day.
# 9  
Old 05-17-2015
If I read your requirement correctly, this is a starter piece...
OSX 10.7.5, default bash terminal...
Code:
#!/bin/sh
# key.sh
# This will run as a timer and once a key-value, (line 8 in this code), is set then NOT pressing a key on the keyboard will run for $TIMER length.
# However if you want to exit pressing a key will exit immedaitely and not wait for the $TIMER to timeout...
# Pressing q manually will just rerun the command again IMMEDIATELY... 
TIMER=5
echo "q" > /tmp/YY
read key < /tmp/YY
echo "Character read is $key."
while true
do
	read -n1 -s -t$TIMER key
	if [ "$key" = "q" ]
	then
		echo "Run command(s) here."
	else
		break
	fi
done
echo "This is the key pressed:- $key."

Results:-
Note the second and third "Run command(s) here." was with a key press of "q"...
Code:
Last login: Sun May 17 18:32:39 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> chmod 755 key.sh
AMIGA:barrywalker~/Desktop/Code/Shell> ./key.sh
Character read is q.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
This is the key pressed:- e.
AMIGA:barrywalker~/Desktop/Code/Shell> _

# 10  
Old 05-17-2015
Quote:
Originally Posted by wisecracker
If I read your requirement correctly, this is a starter piece...
OSX 10.7.5, default bash terminal...
Code:
#!/bin/sh
# key.sh
# This will run as a timer and once a key-value, (line 8 in this code), is set then NOT pressing a key on the keyboard will run for $TIMER length.
# However if you want to exit pressing a key will exit immedaitely and not wait for the $TIMER to timeout...
# Pressing q manually will just rerun the command again IMMEDIATELY... 
TIMER=5
echo "q" > /tmp/YY
read key < /tmp/YY
echo "Character read is $key."
while true
do
	read -n1 -s -t$TIMER key
	if [ "$key" = "q" ]
	then
		echo "Run command(s) here."
	else
		break
	fi
done
echo "This is the key pressed:- $key."

Results:-
Note the second and third "Run command(s) here." was with a key press of "q"...
Code:
Last login: Sun May 17 18:32:39 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> chmod 755 key.sh
AMIGA:barrywalker~/Desktop/Code/Shell> ./key.sh
Character read is q.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
This is the key pressed:- e.
AMIGA:barrywalker~/Desktop/Code/Shell> _



@wisecracker - Thanks for a near good solution.

Your script still requests me to manually push the q button to generate the final output.

Here is an example for your reference.

1. I want to get an image say - www.server.com/unix_is_nice.jpg
2. I write a command -
Code:
wget www.server.com/unix_is_nice.jpg

3. Now, I want to run this wget command for 45 seconds and then the jpg file becomes ready only if I push the "q" button on my keyboard.
4. Writing step2 and the timer is fine for me. But my question is how to give the "q" input automatically in the bash script without me having to keep pushing "q" for every 45 seconds.

To keep everything in shape

Code:
while true
do
wget www.server.com/unix_is_nice.jpg #for 45 seconds
#push "q" button - This is what I want to know
sleep 45
done

# 11  
Old 05-17-2015
Again I am really not sure why you want the 'q' character so:-
OSX 10.7.5, default bash terminal..
Code:
#!/bin/sh
# key.sh
echo "q" > /tmp/q
read key < /tmp/q
echo "Character read is $key."
# TIMER should be 45, set to 1 for testing.
TIMER=1
# OFFSET should be 3600, set to 5 for testing.
OFFSET=5
COUNT=$(($(date +%s)+$OFFSET))
while [ $(date +%s) -le $COUNT ]
do
	if [ "$key" = "q" ]
	then
		echo "Run command(s) here."
		read -n1 -s -t$TIMER key
	else
		echo "Exiting loop manually..."
		break
	fi
done
echo "You are here!"
echo "Character is $key..."

Results:-
First letting the loop take its course.
Second pressing any key for quick exit.
Third pressing the q key several times.
Code:
Last login: Sun May 17 21:50:57 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> ./key.sh
Character read is q.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
You are here!
Character is q...
AMIGA:barrywalker~/Desktop/Code/Shell> ./key.sh
Character read is q.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Exiting loop manually...
You are here!
Character is p...
AMIGA:barrywalker~/Desktop/Code/Shell> ./key.sh
Character read is q.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
Run command(s) here.
You are here!
Character is q...
AMIGA:barrywalker~/Desktop/Code/Shell> _

This User Gave Thanks to wisecracker For This Post:
# 12  
Old 05-17-2015
Thanks for your time @wisecracker. No luck. Let's move on.
# 13  
Old 05-18-2015
Quote:
Originally Posted by jacobs.smith
Please see this.

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

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

No output file is generated.
THERE is your output! If that "back" was printed 45 seconds after you issued the snippet, it is EXACTLY what you were after. It proves that a "q" has been sent and received after the desired amount of time.
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