| Author |
Message |
greengeek
Joined: 20 Jul 2010 Posts: 1184 Location: New Zealand
|
Posted: Sun 10 Mar 2013, 23:22 Post subject:
Bash/Shell script - make it loop forever??[SOLVED] Subject description: How? |
|
I would like to know how to make a script go back to the beginning and start all over again once it reaches the end.
example:
Lets say I want to use xdotool to move my mousepointer up and down (for no particular reason) I could do this:
#!/bin/sh
xdotool mousemove 10 10
xdotool mousemove 10 30
xdotool mousemove 10 10
xdotool mousemove 10 30
xdotool...etc etc ad infinitum.
or, I could just use the first two lines and repeat them somehow. Is there a simple string that I can add to the bottom of a script to say "return to top line and repeat" so that the script will run forever?
eg:
#!/bin/sh
xdotool mousemove 10 10
xdotool mousemove 10 30
repeat.
Last edited by greengeek on Thu 14 Mar 2013, 04:15; edited 2 times in total
|
|
Back to top
|
|
 |
puppyluvr

Joined: 06 Jan 2008 Posts: 3052 Location: Chickasha Oklahoma
|
Posted: Mon 11 Mar 2013, 00:08 Post subject:
|
|
Hello,
Look at
case
*
*
*
esac
Or
x=10
if x <11 then
*
fi
In both cases the code will loop until "case" or "if" are met..
I have a slideshow code which displays random (*) images, and since there is always another random image it continues on...
_________________ "Close the "Windows", and open your eyes, to a whole new world"
http://puppylinuxstuff.meownplanet.net/puppyluvr/
http://theplpd.webs.com/
Nothing but Puppy since 2.15CE...
|
|
Back to top
|
|
 |
technosaurus

Joined: 18 May 2008 Posts: 3843
|
Posted: Mon 11 Mar 2013, 02:17 Post subject:
|
|
there is no goto.
here is the most concise infinite loop I can think of
| Code: | while :; do
#infinite loop unless "break" is called
done |
_________________ Puppy Web Desktop Now with pet packages - Pet Packaging 100 & 101
|
|
Back to top
|
|
 |
simargl

Joined: 11 Feb 2013 Posts: 373
|
Posted: Mon 11 Mar 2013, 04:28 Post subject:
|
|
| Code: | | while true; do sleep 1 && echo ""; done |
|
|
Back to top
|
|
 |
greengeek
Joined: 20 Jul 2010 Posts: 1184 Location: New Zealand
|
Posted: Mon 11 Mar 2013, 04:48 Post subject:
|
|
| puppyluvr wrote: |
case
*
*
*
esac
|
Not sure if I understood this correctly? I did the following but it did not seem to work:
| Code: |
#!/bin/sh
case
xdotool mousemove 20 20
sleep 3
xdotool mousemove 20 40
sleep 1
esac
|
and I couldn't get this to work either:
| Code: |
#!/bin/sh
x=10
if x <11 then
xdotool mousemove 20 20
sleep 3
xdotool mousemove 20 40
sleep 1
fi
|
| technosaurus wrote: |
while :; do
#infinite loop unless "break" is called
done |
Yep, that works, thanks.
Last edited by greengeek on Mon 11 Mar 2013, 04:55; edited 1 time in total
|
|
Back to top
|
|
 |
greengeek
Joined: 20 Jul 2010 Posts: 1184 Location: New Zealand
|
Posted: Mon 11 Mar 2013, 04:54 Post subject:
|
|
| simargl wrote: | | Code: | | while true; do sleep 1 && echo ""; done |
|
I'm obviously doing this wrong too:
| Code: | #!/bin/sh
while true; do sleep 1 && echo ""; done
xdotool mousemove 20 20
sleep 3
xdotool mousemove 20 40
sleep 1 |
|
|
Back to top
|
|
 |
simargl

Joined: 11 Feb 2013 Posts: 373
|
Posted: Mon 11 Mar 2013, 05:04 Post subject:
|
|
I don't have xdotool installed to test, but this should work:
| Code: | #!/bin/sh
while true; do
xdotool mousemove 20 20
sleep 3
xdotool mousemove 20 40
sleep 1
done |
|
|
Back to top
|
|
 |
technosaurus

Joined: 18 May 2008 Posts: 3843
|
Posted: Mon 11 Mar 2013, 05:44 Post subject:
|
|
btw ":" is a shortcut to true and also a useful way to make inline comments
also there should be an example in the xdotool thread that I wrote months ago that was designed to intentionally shake the mouse randomly to be annoying
ex.
printf "hello";: cruel;echo world
which is not the best example it is more useful when you have a long pipe and want to make comments
| Code: | ....long pipe |cat `: inline comments`| \
continuation |cat `: inline comments`| \
... | here cat just acts as a pass through
but really most of the really long pipes you come across could be reimplemented much faster in a couple lines of awk (learn shell first though ... it will help)
_________________ Puppy Web Desktop Now with pet packages - Pet Packaging 100 & 101
|
|
Back to top
|
|
 |
Karl Godt

Joined: 20 Jun 2010 Posts: 2675 Location: Kiel,Germany
|
Posted: Mon 11 Mar 2013, 13:01 Post subject:
|
|
simargl gave a short example.
A while or until loop needs to be programmed with some sleep lines as stated above. Otherwise they would run inside commands as fast as possible, heating up your cpu.
| Code: | until [ "`pidof poweroff`" -o "`pidof reboot`" ]; do
yes
done |
Explaination :
* until tests for existence of a process-id of poweroff -or reboot. Until one of these two appear it runs the
* yes command, that printfs 'y' into the terminal or standard output?error as much as the cpu can deliver.
* done closes the do command.
You can alternatively use the while command :
| Code: |
SCR_SIZ=`xwininfo -root | grep '\-geometry' | awk '{print $2}'`
SCREENSIZE="${SCR_SIZ%%+*}"
MAX_X="${SCREENSIZE%%x*}"
MAX_Y="${SCREENSIZE##*x}"
while
test "`pidof X`";
count=0
do
count=$((count+10))
xdotool mousemove $count $count
sleep 5
if test "$count" -ge "$MAX_X" -o "$count" -ge "$MAX_Y"
then
count=0
fi
done |
Explaination :
* xwininfo -root gives the current screen resolution ie -geometry 1280x1024+0+0
* MAX_XY are the maximum values formatted using bash string manipulation
* while test for the existence of a process-id of X. If there is a X-PID
* count is set zero
* do
* count increases by 10 each do loop
* xdotool whatever variable count variable count should move the cursor then from upper left to bottom right
* sleep five seconds to give you some chance to move the mouse and not occupying the whole cpu
* if tests if the cursor still would fit into the screen, if not resets the count variable to 0
* done ends the commands in the do loop.
_________________ «Give me GUI or Death» -- I give you [[Xx]term[inal]] [[Cc]on[s][ole]] .
Macpup user since 2010 on full installations.
People who want problems with Puppy boot frugal 
|
|
Back to top
|
|
 |
greengeek
Joined: 20 Jul 2010 Posts: 1184 Location: New Zealand
|
Posted: Mon 11 Mar 2013, 13:47 Post subject:
|
|
| simargl wrote: |
| Code: | #!/bin/sh
while true; do
xdotool mousemove 20 20
sleep 3
xdotool mousemove 20 40
sleep 1
done |
| Thanks - that works.
| technosaurus wrote: | | also there should be an example in the xdotool thread that I wrote months ago that was designed to intentionally shake the mouse randomly to be annoying |
| technosaurus wrote: | annoying mouse fun
| Code: | | while :; do xdotool mousemove_relative -- -$(($RANDOM % 10)) $(($RANDOM % 10)); xdotool mousemove_relative -- $(($RANDOM % 10)) -$(($RANDOM % 10)); done |
| Too dangerous for me to fiddle at this stage of my learning curve I'm finding it hard enough to keep control of the mouse, let alone deliberately LOSING control of it...
| Quote: | | Code: | ....long pipe |cat `: inline comments`| \
continuation |cat `: inline comments`| \
... | here cat just acts as a pass through...but really most of the really long pipes you come across could be reimplemented much faster in a couple lines of awk (learn shell first though ... it will help) | I'm trying...but even the baby steps seem big to me...
| Karl Godt wrote: | A while or until loop needs to be programmed with some sleep lines as stated above. Otherwise they would run inside commands as fast as possible, heating up your cpu.
| Code: | until [ "`pidof poweroff`" -o "`pidof reboot`" ]; do
yes
done |
| This moves the moves the mouse as expected but does not loop. Have I copied it wrongly?
| Code: | #!/bin/sh
until [ "`pidof poweroff`" -o "`pidof reboot`" ]; do
xdotool mousemove 20 20
sleep 3
xdotool mousemove 20 40
sleep 3
yes
done |
|
|
Back to top
|
|
 |
SFR

Joined: 26 Oct 2011 Posts: 570
|
Posted: Mon 11 Mar 2013, 13:57 Post subject:
|
|
Hey Greengeek
| greengeek wrote: | This moves the moves the mouse as expected but does not loop. Have I copied it wrongly?
| Code: | #!/bin/sh
until [ "`pidof poweroff`" -o "`pidof reboot`" ]; do
xdotool mousemove 20 20
sleep 3
xdotool mousemove 20 40
sleep 3
yes
done |
|
Delete 'yes' line - apparently it loops on its own.
BTW: Although I always use 'while true; do ... done' method, a famous "fork-bomb"-like syntax (recursive, infinite loop, but deprived of deadly "fork" mechanism) can be utilized as well:
| Code: | | :(){ echo "Loop..."; sleep 1; :; };: |
But of course this is not a good idea to have something like that in your code, so consider this as a "tip of the day".
Greetings!
_________________ [O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource
Omnia mea mecum porto.
|
|
Back to top
|
|
 |
greengeek
Joined: 20 Jul 2010 Posts: 1184 Location: New Zealand
|
Posted: Mon 11 Mar 2013, 14:18 Post subject:
|
|
Thanks - removing the "yes" does allow the script to work correctly.
How easy is it to explain what you mean by the "fork bomb". Can you clarify the danger you refer to or if I google it will I be likely to find the explanation (without drawing the attention of Echelon and Homeland Security)?
|
|
Back to top
|
|
 |
Karl Godt

Joined: 20 Jun 2010 Posts: 2675 Location: Kiel,Germany
|
Posted: Mon 11 Mar 2013, 14:21 Post subject:
|
|
Yup the yes is one of the commands that are somehow not useful. It would need a -r MANY_TIMES option .
| http://www.manpages.spotlynx.com/solaris/man/yes.1 wrote: | | yes can be used to respond programatically to programs that require an interactive response. |
I got that from linuxquestions.org some longer time ago.
|
|
Back to top
|
|
 |
SFR

Joined: 26 Oct 2011 Posts: 570
|
Posted: Mon 11 Mar 2013, 14:31 Post subject:
|
|
| greengeek wrote: | | How easy is it to explain what you mean by the "fork bomb". Can you clarify the danger you refer to or if I google it will I be likely to find the explanation (without drawing the attention of Echelon and Homeland Security)? |
No danger - it's in Wikipedia: http://en.wikipedia.org/wiki/Fork_bomb
Basically it's a nice looking one-liner, which multiplies itself (or rather its own process) ad infinitum, what makes system completely freezed.
Sadly, Puppy isn't immune for that kind of attack...
Greetings!
_________________ [O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource
Omnia mea mecum porto.
|
|
Back to top
|
|
 |
greengeek
Joined: 20 Jul 2010 Posts: 1184 Location: New Zealand
|
Posted: Mon 11 Mar 2013, 15:00 Post subject:
|
|
| SFR wrote: | infinite loop, but deprived of deadly "fork" mechanism) can be utilized as well:
| Code: | | :(){ echo "Loop..."; sleep 1; :; };: |
But of course this is not a good idea to have something like that in your code, so consider this as a "tip of the day". | Do you mean that it's not a good idea because it's so easy to get it wrong (and include a recursive fork by mistake)?
|
|
Back to top
|
|
 |
|