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
# 8  
Old 06-26-2011
Quote:
Originally Posted by ifthanwhile
... and I was wondering if adding the "$" before you evaluate an expression while defining a variable... as in...

variable=$(expression)
Not quite sure what you are asking, so I'm guessing here...
The construct varname=$(command) executes the command and assigns the output to the variable. This style obsoletes back quotes, or back ticks.

If you were asking something else, please ask. If I don't get back to it first, someone will likely post an answer.
# 9  
Old 06-26-2011
Ohhh, thanks, I completely forgot about the need for back ticks thing. Do you know if the $ thing will work in most shells or just particular newer ones. Are the parenthesis needed?
# 10  
Old 06-26-2011
Parens are certainly necessary. Works in modern Kshell and bash.

I rarely use bash, so I cannot say how far back it is supported. The version I have loaded on this machine is GNU bash, version 3.2.39(1)-release (i586-suse-linux-gnu) and it works.

I usually live on the edge with Kshell's latest release and have been using $(command) now for several years. I would doubt that you'll run into an instance that doesn't support it.

I really cannot speak for other sh like shells, so maybe, but maybe not.
# 11  
Old 06-27-2011
cant seem to get ifthanwhile's example working. When i execute the shell script i get the following.
Code:
./cc.sh: line 8: syntax error near unexpected token `)'
./cc.sh: line 8: `    2) echo 'test2/' >> mtest.txt'

Here is my script.
Code:
#!/bin/bash

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

case $rn in
    1) echo 'test1/' >> mtest.txt
    2) echo 'test2/' >> mtest.txt
esac
exit 0

By changing the ) to backslashes i get it to somewhat work, but it doesn't copy the test1 text to the end of the file it copies that everything after it and everything until the / on the next line.

---------- Post updated at 06:06 PM ---------- Previous update was at 12:38 AM ----------

Still cant get it working, ive tried multiple things. I know its a very simple script but im to new to shell scripting to troubleshoot this.

Last edited by Franklin52; 06-27-2011 at 03:09 AM.. Reason: Please use code tags for code and data samples, thank you
# 12  
Old 06-27-2011
There are two issues. First, the result of your mod operation (%) will be 0 through n-1, or in your case either 0 or 1. You need to adjust your case accordingly.

The problem that is causing the odd output is that you are missing the 'termination' semicolon pair at the end of each case action:

Code:
#!/bin/bash

nscripts=2
rn=$(( ($RANDOM % $nscripts)+1 ))

case $rn in
    1) echo 'test1/' >> mtest.txt;;
    2) echo 'test2/' >> mtest.txt;;
esac

If you were to run your script through ksh or bash with the -n option, it would identify the area of the problem. Bash reports this:

Code:
spot; bash -n t2
t2: line 8: syntax error near unexpected token `)'
t2: line 8: `    2) echo 'test2/' >> mtest.txt'

From experience, the unexpected ')' message means that the ';;' was dropped from the previous block. We all occasionally leave them out, or accidentally delete them while editing.

Hope this gets you going again.
This User Gave Thanks to agama For This Post:
# 13  
Old 06-27-2011
Quote:
Originally Posted by agama
There are two issues. First, the result of your mod operation (%) will be 0 through n-1, or in your case either 0 or 1. You need to adjust your case accordingly.

The problem that is causing the odd output is that you are missing the 'termination' semicolon pair at the end of each case action:

Code:
#!/bin/bash

nscripts=2
rn=$(( ($RANDOM % $nscripts)+1 ))

case $rn in
    1) echo 'test1/' >> mtest.txt;;
    2) echo 'test2/' >> mtest.txt;;
esac

If you were to run your script through ksh or bash with the -n option, it would identify the area of the problem. Bash reports this:

Code:
spot; bash -n t2
t2: line 8: syntax error near unexpected token `)'
t2: line 8: `    2) echo 'test2/' >> mtest.txt'

From experience, the unexpected ')' message means that the ';;' was dropped from the previous block. We all occasionally leave them out, or accidentally delete them while editing.

Hope this gets you going again.
Awseome, works well Smilie

So i assume where i have mtest.txt i can also jsut specify a path to a directy containing a tex file, like the following. home/test/user/mtest.txt.

Thank you for your help, i really appreciate it
# 14  
Old 06-27-2011
Quote:
Originally Posted by kylecn
So i assume where i have mtest.txt i can also jsut specify a path to a directy containing a tex file, like the following. home/test/user/mtest.txt.
Glad it is working for you. Yes, any path to a file will work.
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