LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-13-2006, 04:37 PM   #1
mrgreaper
Member
 
Registered: May 2006
Posts: 167

Rep: Reputation: 15
Question random picking scripts inside a script


ok i run a server fro 4 of my mates on my linux box thes two scripts i use to start the server the first is irrelevent as its the stats server the second is the focus of my attention and the one that needs help with

Code:
#!/bin/sh

cd /home/mrgreaper/srcds_1/

screen -A -m -d -S css-server ./srcds_run -priority 3 -game cstrike -ip 172.16.1.37 -port 27015 -rcon_port 27015 +map as_oilrig_wk +maxplayers 32 -nomaster -insecure +sv_lan 1
the +map bit is the important bit now im on the wrong forum if i was to ask how to make that bit random? yes and no you see heres my idea;

i create 10 scripts maybe more each identicle except for the +map bit that would have a different map name in each

then one script that picks 1 script at random to run out of the 10

but i dont even know if thats possible im guessing if commands maybe even a random command i just dont know

the other alternative is a bit more complicated but i think may work ;
get script to delete a tempory script first (the reason will be clear later) then pick from a list of map names one map name and to record that name to a string (i think thats the name) then create a tempory script file and echo in all of the details in the top script except were +map as_oilrig_wk is it would place the string contents

im hoping someone out theree is reading this and thinking "interesting" or better yet "easy" maybe im asking to much of the linux shell system but it all seems doable in a theory sense i just dont have the knowledge to do it
 
Old 06-13-2006, 05:19 PM   #2
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
Quote:
i create 10 scripts maybe more each identicle except for the +map bit that would have a different map name in each

then one script that picks 1 script at random to run out of the 10

but i dont even know if thats possible im guessing if commands maybe even a random command i just dont know
What you ask is possible, but probably unnecessarily complicated for the required task.

bash supports both arrays and random numbers.

You can do it all in one simple script:

Code:
#!/bin/bash

# Initialize array (change these values to whatever you need)
array=("A" "B" "C" "D" "E" "F" "G" "H" "I" "J")

# Pick random number from 0 through 9
random_number=$(($RANDOM%10))

screen -A -m -d -S css-server ./srcds_run -priority 3 -game cstrike -ip 172.16.1.37 -port 27015 -rcon_port 27015 ${array[$random_number]} +maxplayers 32 -nomaster -insecure +sv_lan 1

Last edited by zhangmaike; 06-13-2006 at 05:21 PM.
 
Old 06-13-2006, 05:56 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531
You can do it all in one simple script
Simple is cool, definately.


Anyway. Here's my take on it. It's simple, really:
Code:
#!/bin/sh
mapdir=/some/dir/with/only/maps
maps=($(find $mapdir -type f)); count=${#maps[@]}; cwidth=${#count}; 
rand=$(date|sha1sum|tr -d [a-z]|cut -c 1-$cwidth); if [ "${#rand}" -lt "$cwidth" ]; 
then zeroes=$[$cwidth-${#rand}]; it=1; while [ $it -le $zeroes ]; do rand="0$rand"; 
((it++)); done; fi; if [ "$rand" -le $count ]; then screen -A -m -d -S css-server \
./srcds_run -priority 3 -game cstrike -ip 172.16.1.37 -port 27015 -rcon_port \
27015 ${maps[$rand]} +maxplayers 32 -nomaster -insecure +sv_lan 1; else \
mapSelect; fi; exit 0
 
Old 06-13-2006, 06:27 PM   #4
mrgreaper
Member
 
Registered: May 2006
Posts: 167

Original Poster
Rep: Reputation: 15
man you guys are quick i thought i`d have a quick check before me and the missus settle down to a film

i love the mapdir method but unfortunly the map dir has various other files and folders inside it so the script would have to look only at the *.bsp only then remove the .bsp when it inserts it into the correct place so were into the realms of the silly again

the fist option seems better as i could choose what maps have the odds of being the start map and i assume i can have an indeffinate number of maps in the array

thank you thank you guys i`m going to try this after the film and will edit or reply to say if it worked or not though i cant see why it wouldnt
 
Old 06-13-2006, 06:46 PM   #5
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
Quote:
Originally Posted by mrgreaper
i love the mapdir method but unfortunly the map dir has various other files and folders inside it so the script would have to look only at the *.bsp only then remove the .bsp when it inserts it into the correct place so were into the realms of the silly again
It's easier/simpler than you might think... here is a combination of both mine and unSpawn's suggestions, using only .bsp files and removing the .bsp suffix:

Code:
#!/bin/bash

mapdir=/some/dir/with/only/maps
maps=($(find $mapdir -type f -name "*.bsp" -exec basename "{}" .bsp \;))
count=${#maps[@]};

# Pick random number (from 0 to number of maps - 1)
random_number=$(($RANDOM%$count))

screen -A -m -d -S css-server ./srcds_run -priority 3 -game cstrike -ip 172.16.1.37 -port 27015 -rcon_port 27015 ${array[$random_number]} +maxplayers 32 -nomaster -insecure +sv_lan 1
The command in bold searches the contents of /some/dir/with/only/maps and finds all normal files (not directories) matching the pattern *.bsp, and runs basename on each file (the basename command removes a specified suffix, in this case ".bsp")

See the find and basename manpages for more information.

Last edited by zhangmaike; 06-13-2006 at 06:48 PM.
 
Old 06-13-2006, 09:26 PM   #6
mrgreaper
Member
 
Registered: May 2006
Posts: 167

Original Poster
Rep: Reputation: 15
Code:
#!/bin/sh

# Initialize array (change these values to whatever you need)
array=("A" "B" "C" "D" "E" "F" "G" "H" "I" "J")

# Pick random number from 0 through 9
random_number=$(($RANDOM%10))

screen -A -m -d -S css-server ./srcds_run -priority 3 -game cstrike -ip 172.16.1.37 -port 27015 -rcon_port 27015 +map ${array[$random_number]} +maxplayers 32 -nomaster -insecure +sv_lan 1
worked a treat thankyou very much
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Random number generator inside kernel space zirtik Programming 2 12-26-2005 05:02 PM
alias not workin inside a script sanjith11 Programming 7 08-31-2005 04:36 PM
export $PATH inside script gmitra Programming 3 02-01-2005 09:34 AM
How do I send mail from inside a script? bruno buys Linux - Software 3 10-06-2004 08:57 PM
Netdate inside of Startup Script thirdbanE Slackware 1 05-23-2003 07:17 PM


All times are GMT -5. The time now is 02:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration