Author |
Message |
riedzig
Joined: 01 Oct 2015 Posts: 40
|
Posted: Sun 29 Dec 2019, 16:06 Post subject:
Looking for script to execute commands in a loop [Solved] |
|
I need a script which would execute several commands one by one in a circle with still the same keystroke. Similar to Rox when pressing repeatedtly "Sort by...". I am sure there are many hints on the web, but I canˇt find the right one.
Last edited by riedzig on Fri 03 Jan 2020, 05:01; edited 1 time in total
|
Back to top
|
|
 |
musher0
Joined: 04 Jan 2009 Posts: 15041 Location: Gatineau (Qc), Canada
|
Posted: Sun 29 Dec 2019, 17:16 Post subject:
|
|
Hi riedzig.
What do you mean "in a circle"? What do you want to do with it?
Sounds like you wish to create a virus!!!
Simplest sample:
Code: | #/bin/sh
# /root/my-applications/bin/k.sh # make a symlink to k
# so when you hit the k key in console, it will run.
1stCommand
2ndCommand
3rdCommand
# etc.
exit |
You could also use a number of loops, even make it run unattended.
But I'll stop here and ask you to make your intentions clear!
BFN.
_________________ musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)
|
Back to top
|
|
 |
jafadmin
Joined: 19 Mar 2009 Posts: 1258
|
Posted: Sun 29 Dec 2019, 18:42 Post subject:
|
|
This one will loop, if that helps ..
cmd-loop.sh: Code: |
#! /bin/bash
#
# Looping commands
#
LoopFile="taskloop.txt"
if [ ! -e $LoopFile ]; then echo "1" > $LoopFile; fi
Task=$(cat $LoopFile)
case "$Task" in
1) printf "\tCommand 1\n"
echo "2" > $LoopFile
;;
2) printf "\tCommand 2\n"
echo "3" > $LoopFile
;;
3) printf "\tCommand 3\n"
echo "1" > $LoopFile
;;
*)
# do nothing
;;
esac |
|
Back to top
|
|
 |
jafadmin
Joined: 19 Mar 2009 Posts: 1258
|
Posted: Mon 30 Dec 2019, 02:13 Post subject:
|
|
This is even more fun. Get a bunch of them on your screen and watch them disappear.
cmd-loop.sh:
Code: | #! /bin/bash
#
# Looping commands
#
LoopFile="taskloop.txt"
if [ ! -e $LoopFile ]; then echo "1" > $LoopFile; fi
Task=$(cat $LoopFile)
case "$Task" in
1) yad --info --timeout=30 --height=100 --width=100 --center --title="Command looper" --text="Command one!" &
echo "2" > $LoopFile
;;
2) yad --info --timeout=30 --height=100 --width=100 --center --title="Command looper" --text="Command two!" &
echo "3" > $LoopFile
;;
3) yad --info --timeout=30 --height=100 --width=100 --center --title="Command looper" --text="Command three!" &
echo "1" > $LoopFile
;;
*)
# do nothing
;;
esac |
|
Back to top
|
|
 |
riedzig
Joined: 01 Oct 2015 Posts: 40
|
Posted: Mon 30 Dec 2019, 04:16 Post subject:
|
|
Thanks jafadmin
I think your first script might help, but I'm halfway.
How would it precisely look if:
The first executing the script opens e.g. roxfiler
Second kills roxfiler and opens geany
Third kills geany and opens leafpad
Fourth kills leafpad and opens again roxfiler
or for display rotating:
1. xrandr -o left
2. xrandr -o inverted
3. xrandr -o right
4. xrandr -o normal
5. xrandr -o left and so on
|
Back to top
|
|
 |
jafadmin
Joined: 19 Mar 2009 Posts: 1258
|
Posted: Mon 30 Dec 2019, 13:19 Post subject:
|
|
Just extend the "case" statement.
i.e.: replace the "printf" command lines with your own "xandr" command lines. Add more cases for however many commands you need.
Code: |
case "$Task" in
1) xrandr -o left # <- your command
echo "2" > $LoopFile # <- required indexer
;;
2) xrandr -o inverted
echo "3" > $LoopFile
;;
3) xrandr -o right
echo "4" > $LoopFile
;;
4) xrandr -o normal
echo "1" > $LoopFile # <- set index back to 1
;;
*);; # Ignore anything else
esac
|
The "echo" lines control the indexing. The last "echo" command sets it back to 1
.
|
Back to top
|
|
 |
riedzig
Joined: 01 Oct 2015 Posts: 40
|
Posted: Wed 01 Jan 2020, 17:34 Post subject:
|
|
The xrandr variant works perfectly.
Then I adapted your script to a working "slideshow" for xplanet which is a great app with many config options (scripts named xplanet1, xplanet2 and so on).
Now I am trying to combine the commands with killing the previous xplanet
1) killall xplanet;xplanet1
echo "2" > $LoopFile
;;
2) killall xplanet;xplanet2
echo "3" > $LoopFile
;;
3) killall xplanet;xplanet3
echo "4" > $LoopFile
;;
4) killall xplanet;xplanet4
echo "1" > $LoopFile
;;
This would also work except each script is being executed 2 times:
xplanet1
xplanet1
xplanet2
xplanet2
xplanet3 etc
What am I doing wrong?
|
Back to top
|
|
 |
jafadmin
Joined: 19 Mar 2009 Posts: 1258
|
Posted: Wed 01 Jan 2020, 19:38 Post subject:
|
|
try putting an ampersand '&' after each "xplanet' command: "xplanet3 &"
|
Back to top
|
|
 |
riedzig
Joined: 01 Oct 2015 Posts: 40
|
Posted: Thu 02 Jan 2020, 06:06 Post subject:
|
|
Many thanks, & is the solution.
|
Back to top
|
|
 |
jafadmin
Joined: 19 Mar 2009 Posts: 1258
|
Posted: Fri 03 Jan 2020, 03:17 Post subject:
|
|
riedzig wrote: | Many thanks, & is the solution. |
Go ahead and edit the first post title and add "[Solved] to the title.
|
Back to top
|
|
 |
|