LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   random picking scripts inside a script (https://www.linuxquestions.org/questions/linux-newbie-8/random-picking-scripts-inside-a-script-454469/)

mrgreaper 06-13-2006 03:37 PM

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

zhangmaike 06-13-2006 04:19 PM

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


unSpawn 06-13-2006 04:56 PM

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


mrgreaper 06-13-2006 05:27 PM

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

zhangmaike 06-13-2006 05:46 PM

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.

mrgreaper 06-13-2006 08:26 PM

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


All times are GMT -5. The time now is 10:58 PM.