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.
#! /bin/bash
end=$((SECONDS+45))
while [ $SECONDS -lt $end ];
do
my command execution
done
echo q
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:
$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>
If I read your requirement correctly, this is a starter piece...
OSX 10.7.5, default bash terminal...
#!/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"...
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> _
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.
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
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
Again I am really not sure why you want the 'q' character so:-
OSX 10.7.5, default bash terminal..
#!/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.
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> _
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.