Register
It is currently Fri Jul 25, 2014 2:58 am

How would I do this?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Sun Aug 23, 2009 2:00 am   
User avatar

Joined: Fri Oct 03, 2008 5:48 pm
Posts: 8
Scenario: Set a script via cron to run maybe every 15 mins.

When script executed, it will pick a command from a list at random to run then run it.

How?

Example: 20 commands which will each pop up a zenity box with a quote at random everytime it's run.


Top
 Profile  
 PostPosted: Sun Aug 23, 2009 5:58 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
commands.txt
Code:
list
of
commands
enter
separated


script.sh
Code:
#!/bin/bash
commandfile="/path/to/commands.txt"
len=$(cat $commandfile|wc -l)
let rand=$RANDOM%$len
let rand=rand+1
command=$(head -n$rand $commandfile | tail -n1 )
echo $command


results:
Code:
sajko@hanna:~> for i in $(seq 1 20); do sh script.sh; done
command20
command8
command15
command4
command4
command17
command19
command20
command11
command6
command1
command3
command7
command17
command5
command14
command8
command4
command19
command4


Just remove the echo and it'll run the command instead :)

Best regards
Fredrik Eriksson


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP