You are not logged in.

#1 2009-12-17 09:12:53

SiegeMachine
Member
Registered: 2009-03-26
Posts: 157

Newb bash script

Well I've had linux for about a year or so now....Well I suppose longer than that but I didn't use it for a while but here I am full time user of Linux for about a year now, wahoo!

Anyways, I suppose its time to dabble into some coding so I can understand those pesky pkgbuilds when I wanna change them...

Basically right now I have the linux client Team Fortress 2 dedicated server installed and it has a command to run which starts the script.  I made a script in /usr/bin that I can call on quickly with just 'tf2server' to fire up the server when I want.  It already has the options in that script such as the players and which map to load.

./srcds_run -game tf +maxplayers 5 +map

is basically the line that it calls. What I want to do though is instead of having to go to the folder where it is and running that command with different options when I want to each time I'd like to be able to just do something like...

tf2server cp_well

for example, and then it would use well for the map.  I know that in terminal you can type --help on most commands and it will show you usage, so basically my question is....Is there a guide, or someone willing to give an example on how I can accomplish this? I'm assuming it will only take bash and some wildcards of some kind to do.

Help is appreciated smile

Offline

#2 2009-12-17 09:49:03

chpln
Member
From: Australia
Registered: 2009-09-17
Posts: 361

Re: Newb bash script

SiegeMachine wrote:
./srcds_run -game tf +maxplayers 5 +map

Unless your script is explicitly cd'ing into the directory for 'srcds_run', then attempting to invoke the command found in the current directory is probably not what you want.  If this is the case, I'd either have the script change to the correct directory itself, or ensure the path to srcds_run is in your $PATH and remove the leading "./".  When running a script, the current directory is that which the script was invoked from and not that which the script is located.

To accept options as in your example is quite simple:

./srcds_run -game tf +maxplayers 5 +map $@

$@ evaluates to the argument(s) specified.  So, assuming the '+map' option takes the name of a map as an argument:

tf2server cp_well

Should work fine.  With this you can also specify any additional settings for the server.  E.g.,:

tf2server cp_well +timelimit 30

(Note: I know nothing about Team Fortress. '+timelimit' probably doesn't exist; I'm just making things up wink)

If you're interested in learning shell scripting, Advanced Bash Scripting (PDF) is an excellent resource.

Hope this helps.

Last edited by chpln (2009-12-17 09:54:15)

Offline

#3 2009-12-17 11:04:28

lswest
Member
From: Munich, Germany
Registered: 2008-06-14
Posts: 456
Website

Re: Newb bash script

I second what was said above, but will add a little bit of extra info to it.

If you want to allow certain settings to be set, you could write a script that checks the first argument ("$1"), and if it's "--help", echo "Usage: $0 [map name] [players]" and so forth.  Actual code:

if [[ "$1" == "--help" ]]; then
echo "Usage: $0 [map name] [players]"
else
./srcds_run -game tf +maxplayers $2 +map $1
fi

Of course, you'll have to run a check to see if the other arguments are blank, and if so, set a default value for them.  This can be done with an extended if statement (if "$1" != "" and "$2" != "", etc. etc.).

Then, if you want to set the players too, you'd simply have to do this:

./srcds_run -game tf +maxplayers $2 +map $1

To clarify how this is different from above:
"$@" takes in anything after the script name as one argument, whereas $1 and $2 take each space-seperated word as an argument.  It may be a bit more complicated to implement, but it allows for a bit more creativity, in case you're interested in that.  Otherwise you can do as was suggested above, use "$@" and remove all things after "-game tf" and type it everytime you want to start the script (or create an alias for it in your .bashrc).  Either way, you have plenty of options.

Hope that helps,
Lswest


Lswest <- the first letter of my username is a lowercase "L".
"...the Linux philosophy is "laugh in the face of danger". Oops. Wrong one. "Do it yourself". That's it." - Linus Torvalds

Offline

#4 2009-12-17 14:42:54

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: Newb bash script

Off-topic somewhat, but if you plan to have more scripts in the future to do tasks, you can create a local script environment in your home directory to keep all your scripts together and easier to manage.  Link in siq.


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#5 2009-12-18 03:57:10

SiegeMachine
Member
Registered: 2009-03-26
Posts: 157

Re: Newb bash script

Yea sorry I didn't mention that, in the startup script it has a cd line in it before it runs the command. But yea as I figured it was wildcards, I just wasn't sure what I had to put inside of the script to allow the wildcards to work but the $1 and $2 looks about what I want so I'll give that a try.

Offline

Board footer

Powered by FluxBB