Shell script that runs a random shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script that runs a random shell script
# 1  
Old 06-25-2011
Question Shell script that runs a random shell script

Hi, im trying to make a shell script that basically runs a random shell script form a list of shell scripts i specify. Im not very good at writing shell scripts, and am new to linux.

Thanks in advance Smilie
# 2  
Old 06-25-2011
So you want to invoke the script foo and it picks and runs a script from an internal list.... If that's the case, then here is a simple example:

Code:
#!/usr/bin/env ksh

nscripts=5
rn=$(( $RANDOM % $nscripts ))

case $rn in
    0)  echo script_1;;
    1)  echo script_2;;
    2)  echo script_3;;
    3)  echo script_4;;
    4)  echo script_5;;
esac
exit 0

Replace the echo script_n with the command you wish to exeecute and you're in business.
# 3  
Old 06-25-2011
Quote:
Originally Posted by agama
So you want to invoke the script foo and it picks and runs a script from an internal list.... If that's the case, then here is a simple example:

Code:
#!/usr/bin/env ksh

nscripts=5
rn=$(( $RANDOM % $nscripts ))

case $rn in
    0)  echo script_1;;
    1)  echo script_2;;
    2)  echo script_3;;
    3)  echo script_4;;
    4)  echo script_5;;
esac
exit 0

Replace the echo script_n with the command you wish to exeecute and you're in business.
Wonderful, thank you so much for your help you've made my dreams come true Smilie

---------- Post updated at 12:47 PM ---------- Previous update was at 12:39 PM ----------

Could I request some more help ;D

Now that ive got this down, it will use nearly 60 shell scripts. Ive got all of them made but the only difference is each shell script just copies to a different line of text to the end of a text file. Would it be possible to use a scipt similar to this but to have it randomly copy a line of text (actually its just one word) from a list of words to the end of a text file?

Then i could cut it down to only a few scripts.
# 4  
Old 06-25-2011
Are you looking to randomly write one of a series of different texts to the end of a text file. If so you can use the same script as above but replace the case option with something like

case $rn in
1) echo "your text here" >> text_file.txt
esac


the double redirect symbol (I believe it is called something like append) will put the "your text here" at the bottom of the text_file.txt

I hope that helps a little Smilie
This User Gave Thanks to ifthanwhile For This Post:
# 5  
Old 06-25-2011
Quote:
Originally Posted by ifthanwhile
Are you looking to randomly write one of a series of different texts to the end of a text file. If so you can use the same script as above but replace the case option with something like

case $rn in
1) echo "your text here" >> text_file.txt
esac


the double redirect symbol (I believe it is called something like append) will put the "your text here" at the bottom of the text_file.txt

I hope that helps a little Smilie
Yes, that's exactly what i wish to do. Thanks for the info i,ll try it out.
# 6  
Old 06-26-2011
Rather than maintaining a case of 60 lines in your script, and making it easy to add/change the words/strings later, you could try something like this:

Code:
#!/usr/bin/env ksh

biscuits=/path/filename          # name of file with one string per line
nstr=$( wc -l <$biscuits)        # count number of biscuits (cookies in the US)
if (( $nstr > 0 ))                   # prevent disaster on an empty file
then
    rn=$(( ($RANDOM % $nstr) + 1 ))      # compute a random one based on the number of biscuits
    rstr=$( tail -$rn $biscuits | head -1 )    # read the biscuit
    echo $rstr                         # for testing
    echo $rstr >>/path/target-file
fi

With this you can modify your strings later without needing to retest your script. You could also use the same script with different lists of words/strings.

Have fun!
# 7  
Old 06-26-2011
I had not even thought of it that way (I guess I am so used to modifying my scripts under development I was not thinking about easy changing). I do have one question for you though agama I am fairly new to shell scripting and I was wondering if adding the "$" before you evaluate an expression while defining a variable... as in...

variable=$(expression)

Thanks for the help if you can.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sql command inside shell script runs without giving anything back as outout

#!/bin/sh # This script returns the number of rows updated from a function echo "The execution is starting ....." sqlplus -silent $UP <<EOF set serveroutput on set echo off set pagesize 0 VAR no_rows_updated NUMBER; EXEC :no_rows_updated :=0; DECLARE CURSOR c_update is SELECT * FROM... (4 Replies)
Discussion started by: LoneRanger
4 Replies

2. AIX

Script runs in shell but not cron

We run some menu driven software that has the ability to batch menu paths and generate reports quickly. Normally you run a batch like: $ BATCH BATCHNAME The batch program then prompts you for the date you want the report run for. I got some help from some folks on IRC to do the following: BATCH... (2 Replies)
Discussion started by: herot
2 Replies

3. Shell Programming and Scripting

Part of the Shell script is not running via crontab, runs fine manually

Hello Team, As a part of my job we have made a script to automate a service to restart frequently. Script having two functions when executing it's should find the existing service and kill it, then start the same service . Verified the script it's working fine when executing... (18 Replies)
Discussion started by: gowthamakanthan
18 Replies

4. Shell Programming and Scripting

Shell Script runs good manually but not through Cron tab

Hello Every one, I have a shell script which is running fine manually, but its giving me hard time when running tru cron job. :wall:. Am using #!/usr/bin/ksh >echo $SHELL /usr/bin/ksh Cron Job is as below, it execues but dosent do what i want it to do. 47 15 * * *... (1 Reply)
Discussion started by: naren.chowdhary
1 Replies

5. Shell Programming and Scripting

CRON shell script only runs correctly on command line

Hi, I'm new to these forums, and I'm hoping that someone can solve this problem... To make things short: I have DD-wrt set up on a router. I'm trying to run a script in CRON that fetches the daily password from my database using SSH. CRON is set like so(in web interface): * * * *... (4 Replies)
Discussion started by: louieaw
4 Replies

6. Shell Programming and Scripting

Shell script runs fine in Solaris, in Linux hangs at wait command

HI, I have a strange problem. A shell script that runs fine on solaris. when i ported to linux, it started hanging. here is the core of the script CFG_FILE=tab25.cfg sort -t "!" -k 2 ${CFG_FILE} | egrep -v "^#|^$" | while IFS="!" read a b c do #echo "jobs output" #jobs #echo "jobs... (13 Replies)
Discussion started by: aksaravanan
13 Replies

7. Shell Programming and Scripting

$RANDOM does not work inside a shell script

Hi folks I'm coding on Ubuntu 9.04 standard shell. I'm writing a script that needs to generate a random number at some point of its execution. When I do echo $RANDOMas a command inside shell, I clearly get some randomly generated number However when I do i=`$RANDOM` echo $ior even... (14 Replies)
Discussion started by: ksk
14 Replies

8. Shell Programming and Scripting

Shell script which runs sql script

Hi all, I need a shell script which runs a sql script but I couldn't find how to finish it. This is the code that I have: #! /usr/bin/ksh export SHELL=/bin/ksh export ORACLE_SID=database export ORACLE_HOME=/opt/oracle/product/9.2.0.8 sqlplus user <<EOF @/path/path/path/scriptname.sql... (3 Replies)
Discussion started by: Geller
3 Replies

9. Solaris

Sunsolaris shell script runs only as super user

Hi Friends, I am new to Sun solaris unix.I am facing problem while runing my kornshell script just as an ordinary user.The script works fine while i am working as a super user.the script just uses awk to check the first charcter of a file and then copies the file to another folder. Do i... (4 Replies)
Discussion started by: gjithin
4 Replies

10. Shell Programming and Scripting

Shell Script: want to insert values in database when update script runs

Hi , I am new to linux and also also to shell scripting. I have one shell script which unpacks .tgz file and install software on machine. When this script runs I want to insert id,filename,description(which will be in readme file),log(which will be in log file) and name of unpacked folder... (1 Reply)
Discussion started by: ring
1 Replies
Login or Register to Ask a Question