2

I am setting up game servers via SSH and was wondering if I could execute one file that could be run with arguments (like Windows Batch files) without having to type it out so long.

What I want is a file that will execute

./srcds_run -game csgo -console -usercon +game_type 0 +game_mode 1 +mapgroup mg_active +map de_dust2 -tickrate 128

without me having to type all that out or copy it from a wiki page every time.

Just to clarify for people coming after this has been solved, my server is running CentOS.

3
4

Just put a simple shell script in the directory where your game resides

#!/bin/sh
cd $(dirname $0)
./srcds_run -game csgo -console -usercon +game_type 0 +game_mode 1 +mapgroup mg_active +map de_dust2 -tickrate 128

Should you want to put the script somewhere else, you should use the full path

#!/bin/sh
/path/to/srcds_run -game csgo -console -usercon +game_type 0 +game_mode 1 +mapgroup mg_active +map de_dust2 -tickrate 128
4
  • 2
    I agree with this answer, but this script will work only if launched in the same directory of the srcds_run program. Use /path/to/file/srcds_run ... instead. – dr_ Jul 12 '15 at 14:11
  • @dr01 - what is the point of hardcoding a pathname in a script? Why write a script at all in that case? – mikeserv Jul 12 '15 at 14:29
  • This worked for me. I copied this and put it in to the folder containing srcds_run, and then I typed bash game.sh (what I named it) when I was in that directory via SSH and it worked! Now I am planning to try the answer below this one too. Thanks a lot! – Geotan Jul 12 '15 at 14:34
  • @Geotan: You can simply chmod +x game.sh and then game.sh. It's what the first line is for – marmistrz Jul 13 '15 at 6:51
1

So you need to create an alias in your .bash_profile:

alias game="./srcds_run -game csgo -console -usercon +game_type 0 +game_mode 1 +mapgroup mg_active +map de_dust2 -tickrate 128"

After source .bash_profile or relogin (to load the new game command) and finally game.

2
  • Why do you suggest an alias, why not a plain shell script? – Basile Starynkevitch Jul 12 '15 at 7:37
  • Man there are different ways for to do the same, a script is another option. – Joe Mantil Jul 12 '15 at 12:28
1

After adding the alias you can also bind a fast key in your keyboard settings (depends on distribution ofc) to run it really fast n easy.

(like windows+g)

2
  • Why suggesting an alias, why not a plain shell script? – Basile Starynkevitch Jul 12 '15 at 7:37
  • That works fine aswell, I'm not aware of whats best practice. – Philip Wiberg Jul 12 '15 at 8:04
1

I suggest you use a shell function rather than an alias.

runds(){
    set srcds_run \
            -game      "${1-csgo}"      \
            -console                    \
            -usercon                    \
            +game_type "${5-0}"         \
            +game_mode "${6-1}"         \
            +map       "${2-de_dust2}"  \
            +mapgroup  "${3-mg_active}" \
            -tickrate  "${4-128}"
    if   command -v "./$1" >/dev/null
    then "./$@"; else "$@"
    fi
}

The above is a shell function which you might run at a command-prompt with copy/paste right now, or else paste into your shell's environment file (such as ~/.bashrc) which will parameterize your command. After the function has been defined in your shell's environment you can enter the command runds without arguments to use only the argument defaults you named in your question, or else, if you do give it arguments it will use those you specify to replace the defaults assigned for $1...$6 in order.

I tried to guess at which arguments you were more likely to want to change occasionally than others and ordered them accordingly, but also preserved the command-line order in the question in case it mattered. This is why they do not appear in numerical order in the set statement.

I also set it up so the function will try first to run ./srcds if it might, but, failing that, it will still attempt to run a srcds command which might be elsewhere located in $PATH just in case.

Anyway, the point is, if you do...

runds

...at your shell prompt then the command the function will attempt to run will be...

./srcds_run \
    -game      csgo      \
    -console             \
    -usercon             \
    +game_type 0         \
    +game_mode 1         \
    +map       de_dust2  \
    +mapgroup  mg_active \
    -tickrate  128

... if ./srcds_run is a file which the shell might attempt to execute, or else it will try the same command but without the leading ./, or else it will fail with error and let you know.

But if instead you do...

runds other_game new_map

Then the command attempted will be changed somewhat...

./srcds_run \
    -game      other_game \
    -console              \
    -usercon              \
    +game_type 0          \
    +game_mode 1          \
    +map       new_map    \
    +mapgroup  mg_active  \
    -tickrate  128

...will be the command it attempts to run instead, with the same notes as before about ./ and so on, of course.

2
  • Why a shell function, why not a plain shell script? – Basile Starynkevitch Jul 12 '15 at 7:38
  • 1
    @BasileStarynkevitch - because the assumption is there already is some kind of script/executable involved in ./srcds. Also, this has a simpler copy/paste appeal without my having to make any dangerous guesses about the filesystem. I guess I could as easily ask you, why a script and not a function? – mikeserv Jul 12 '15 at 7:54
1

Create a file called csgo (or whatever name you want to type) in a directory that's listed in echo $PATH. The typical directory would be /usr/local/bin if you want all users to be able to run the script, or ~/bin (bin under your home directory) if the script is just for you. Write the following content in the script:

#!/bin/sh
/path/to/srcds/srcds_run -game csgo -console -usercon +game_type 0 +game_mode 1 +mapgroup mg_active +map de_dust2 -tickrate 128

Replace /path/to/srcds by the path where you installed the program (the directory where the srcds_run executable is).

If you use a Windows editor to edit the script, make sure to tell it to use Unix (LF) line endings; Windows (CRLF) line endings won't work.

If you add "$@" with a space before it at the end of the line, you'll be able to pass arguments to the program by passing arguments to the script.

#!/bin/sh
/path/to/srcds/srcds_run -game csgo -console -usercon +game_type 0 +game_mode 1 +mapgroup mg_active +map de_dust2 -tickrate 128 "$@"

If the program can't find its files when it's invoked from a different directory, switch to its directory first.

#!/bin/sh
cd /path/to/srcds &&
./srcds_run -game csgo -console -usercon +game_type 0 +game_mode 1 +mapgroup mg_active +map de_dust2 -tickrate 128 "$@"

Once you've saved the file, make it executable. On the command line (using the correct path for where you saved the script):

chmod +x ~/bin/csgo

You can now run the program by typing the command csgo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.