Looking for script to execute commands in a loop [Solved]

Using applications, configuring, problems
Post Reply
Message
Author
riedzig
Posts: 40
Joined: Thu 01 Oct 2015, 06:14

Looking for script to execute commands in a loop [Solved]

#1 Post by riedzig »

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, 09:01, edited 1 time in total.

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#2 Post by musher0 »

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: Select all

#/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)

jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

#3 Post by jafadmin »

This one will loop, if that helps ..

cmd-loop.sh:

Code: Select all

#! /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

jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

#4 Post by jafadmin »

This is even more fun. Get a bunch of them on your screen and watch them disappear.

cmd-loop.sh:

Code: Select all

#! /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 

riedzig
Posts: 40
Joined: Thu 01 Oct 2015, 06:14

#5 Post by riedzig »

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

jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

#6 Post by jafadmin »

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: Select all

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

.

riedzig
Posts: 40
Joined: Thu 01 Oct 2015, 06:14

#7 Post by riedzig »

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?

jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

#8 Post by jafadmin »

try putting an ampersand '&' after each "xplanet' command: "xplanet3 &"

riedzig
Posts: 40
Joined: Thu 01 Oct 2015, 06:14

#9 Post by riedzig »

Many thanks, & is the solution.

jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

#10 Post by jafadmin »

riedzig wrote:Many thanks, & is the solution.
Go ahead and edit the first post title and add "[Solved] to the title.

Post Reply