#need some help with the bash script 2

59 messages · Page 1 of 1 (latest)

remote dome
#

I'm writing a script to manage a server with linux. The script should include a notification system via in-game chat. This will work based on rcon servermsg. Here are some problems with the servermsg command:

rcon -c /usr/local/etc/rcon.yaml "servermsg test1"
test1 appears in the chat and on the screen

rcon -c /usr/local/etc/rcon.yaml "servermsg "test1""
rcon -c /usr/local/etc/rcon.yaml 'servermsg test1'
rcon -c /usr/local/etc/rcon.yaml 'servermsg "test1"'
rcon -c /usr/local/etc/rcon.yaml 'servermsg 'test1''
test1 appears

rcon -c /usr/local/etc/rcon.yaml "servermsg test1 test2"
rcon -c /usr/local/etc/rcon.yaml 'servermsg test1 test2'
rcon -c /usr/local/etc/rcon.yaml "servermsg 'test1 test2'"
nothing is displayed in chat or on the screen, sending fails

rcon -c /usr/local/etc/rcon.yaml "servermsg "test1 test2""
rcon -c /usr/local/etc/rcon.yaml 'servermsg 'test1 test2''
only test1 appears

rcon -c /usr/local/etc/rcon.yaml 'servermsg "test1 test2"'
test1 test2 appear

While you might suggest to write some direct notifications using the last option, I would like to use variables, but in bash you can't use a variable by placing it inside single quotes '_'. What should I do in this case? Can I expect the servermsg command to be improved? The quit or startrain commands, for example, work without having to include them in quotes.

dim radish
dim radish
#

I would like to use variables, but in bash you can't use a variable by placing it inside single quotes
Can variables just be "appended" to strings?
like

WelcomeMessage="test1 test2";
rcon -c /usr/local/etc/rcon.yaml 'servermsg "'$WelcomeMessage'"';

Since I'd assume you used something along the lines of this in the escape character test?

WelcomeMessage="test1 test2";
rcon -c /usr/local/etc/rcon.yaml "servermsg \"${WelcomeMessage}\"";

but yeah, if neither work I have no clue how to "work around" that, aside of potentially setting the variable before usage for each message like this:

WelcomeMessage='servermsg "test1 test2"';
rcon -c /usr/local/etc/rcon.yaml $WelcomeMessage
#or...
WelcomeMessage="test1 test2";
MyCommand='servermsg "'$WelcomeMessage'"';

#followed by
rcon -c /usr/local/etc/rcon.yaml "${MyCommand}"

Wish you good luck, nevertheless!

echo aurora
remote dome
#

rcon -c /usr/local/etc/rcon.yaml 'servermsg "$WelcomeMessage"';
No, it won't work, it will show $WelcomeMessage

remote dome
echo aurora
#

where is rcon from?

#

steamCMD?

#

oh actually it's in the debian repo

#

could you send me your rcon yaml?

#

I'm not too familiar with using rcon manually but I can have a look

remote dome
#

but it works fine with echo 'servermsg "$MSG"' | rcon -c /usr/local/etc/rcon.yaml and I can't use the variable as I posted above, $MSG will appear

echo aurora
#

oh the yaml is the actual configuration to your server

#

ok you don't need to send me that 😄

#

I installed it via apt and I'm looking at the rconclt now

remote dome
#

I'd send you that config, but you can believe me that I've used rcon successfully before, including for sending out notifications that were written explicitly without variables, so it's all about using variables

echo aurora
#

yea, I got you, I don't need it it's just the connection parameters right

remote dome
#

I used to have a script that said so and so rcon -c config_file 'servermsg "message"'

remote dome
echo aurora
#

ya the one in the debian repo seems to be slightly different command names but it's just a wrapper

#

all my SteamCMD installs are in docker containers and I just wanted to have a quick look at the syntax which I was able to do with the version in the debian repo

#

how about:
rcon -c /usr/local/etc/rcon.yaml $(echo servermsg $MSG)
or
echo servermsg $MSG | xargs rcon -c /usr/local/etc/rcon.yaml?

#

it may work if rcon is expecting standard input

remote dome
#

seems rcon expecting "servermsg and_the_text_next_to_it" because with MSG="test1 test2"; rcon -c /usr/local/etc/rcon.yaml $(echo "servermsg "$MSG"") it says Unknown command test1 and Unknown command test2

#

same with echo servermsg $MSG | xargs rcon -c /usr/local/etc/rcon.yaml

#

or with echo "servermsg $MSG"

echo aurora
#

what about echo 'servermsg "$MSG"' | xargs etcetc

remote dome
#

In your variant, when using xargs, rcon considered $MSG to be the next command:
Broadcast a message to all connected players. Use: /servermsg "My Message"

Unknown command $MSG

and no output in the game

echo aurora
#

ok we're getting closer, let's escape some quotes

#

echo \'servermsg \"$MSG\"\'

#

pipes to xarg etc etc

remote dome
#

Unknown command 'servermsg "test1 test2"'

echo aurora
#

ok lets try echo 'servermsg \"$MSG\"' ?

#

it's odd because it looks just like your test command here: rcon -c /usr/local/etc/rcon.yaml 'servermsg "test1 test2"' test1 test2 appear

remote dome
#

yay wait

#

seems that one:
MSG="test1 test2" echo -e \'servermsg \"$MSG\"\' | xargs rcon -c /usr/local/etc/rcon.yaml
works correctly

echo aurora
#

oh great

remote dome
#

I'll put your nickname in the info for my utility, I can put your name or mailing address if you send it to dm

#

thanks a lot

echo aurora
#

happy to help! good luck

#

send me a link to your github/repo for the project if its public

#

if you want

remote dome
#

I haven't done it yet, but I will later.

safe sleet
#

With all due respect, If the guy can't do the simplest of bash scripting, not sure id want the public really using their scripts

safe sleet
#

So then rcon -c /usr/local/etc/rcon.yaml "servermsg \"$msgvar\""

remote dome
echo aurora
remote dome
#

No need to argue, friends. I have tested your suggestions in my script. @echo aurora's option works either way. @safe sleet's option works too, but not under all conditions. I probably should have figured out about escaping quotes myself, and should have known about xargs, but it just so happens to be part of the learning curve. Thanks again, sir @echo aurora.

odd swift
#

Is your rcon opensource? Would love to see. Thanks.

safe sleet
safe sleet
echo aurora
safe sleet
#

True haha

safe sleet
#

@echo aurora did the method using xargs work with something you tested that the other method didn't? Only thing I can think of is if you have a " character in your message. I didn't think xargs would protect against that though, at least in the example given. I usually use xargs a bit differently for scenarios where i want to run a command multiple times, like for each line of input

echo aurora
#

I don't have a setup to test with; I don't have RCON installed locally and didn't want to install it just to test with. The other method didn't work for vsnegupal so I was suggesting a method that would isolate the string building from the cl options. Then the errors vsnegupal was getting suggested that rcon wasn't taking standard input from the pipe so used xargs to work around that.

safe sleet
#

Oh, I didn't even realize you weren't OP haha my bad