Page 1 of 1

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

Posted: Sun 29 Dec 2019, 20:06
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.

Posted: Sun 29 Dec 2019, 21:16
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.

Posted: Sun 29 Dec 2019, 22:42
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

Posted: Mon 30 Dec 2019, 06:13
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 

Posted: Mon 30 Dec 2019, 08:16
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

Posted: Mon 30 Dec 2019, 17:19
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

.

Posted: Wed 01 Jan 2020, 21:34
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?

Posted: Wed 01 Jan 2020, 23:38
by jafadmin
try putting an ampersand '&' after each "xplanet' command: "xplanet3 &"

Posted: Thu 02 Jan 2020, 10:06
by riedzig
Many thanks, & is the solution.

Posted: Fri 03 Jan 2020, 07:17
by jafadmin
riedzig wrote:Many thanks, & is the solution.
Go ahead and edit the first post title and add "[Solved] to the title.