Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to make an automated message for my game server running in linux.. Ubuntu 14.
I know how to send a message to screen
Code:
screen -S screenname -X stuff 'ls -l^M'
for example if you are in the linux terminal. The server game server has a different console so for example to send a command specific to the console I should enter it like
This is the method to send announcement to server console from a detached screen. From server console you can also manually send it by typing
Quote:
c_announce"Hello people. What's up?"
and pressing enter..
What I want to do is, for example I got a txt file and taking a random sentence from there with this command
Quote:
shuf -n 1 randmsg.txt
I want to make an announcement with the random line that is selected from randmsg.txt. How can I do it?
Many thanks.
Here is my sh file that I am messing to understand how it works. I came to a place that I can get random lines from file, arrange it but I dont know how I can send it to console screen.
Quote:
while true
do
set `grep "Join Announcement" ~/.klei/DoNotStarveTogether/chat_log.txt | tail -1`
grep "Join Announcement" ~/.klei/DoNotStarveTogether/chat_log.txt | tail -1
i=`grep "Join Announcement" ~/.klei/DoNotStarveTogether/chat_log.txt | wc -l`
name=$3
echo $name
echo $i
eval echo `shuf -n 1 randmsg.txt`
sleep 5
done
Here is my randmsg.txt file
Quote:
Hello $name.
Good to see you $name.
What is up $name.
Oh my god you are here $name.
$name wants to play with us. Welcome.
And here is the output
Quote:
[Join Announcement] JrShadowAssassin
JrShadowAssassin
76
What is up JrShadowAssassin.
I'm assuming that the command you cited does work and you're able to send messages to one specific, or all consoles and that this part works.
Therefore what you need to do is allow for flexibility and repeatability.
A script can do this for you. My favorite saying is "anything you can type in a command line, you can code into a BASH script". In my signature are links for beginning and advanced BASH programming guides as well as a pointer to my blog where I show some examples and how to debug scripts with some methods.
For flexibility, I don't see why you absolutely need a text file. Why not have a command where you could type something like:
Code:
announce -a "Everyone please welcome John to the game!"
And the script would be announce where arguments might be the -a for "all" or -c <console-name> for one given console, then followed by a string you type so you can be fully flexible in what you say versus need to anticipate stuff from within a text file and then have to figure out how to extract any given message from that file. What if gamers are misbehaving and you need to say "Hey! Lay of John! I will end the session if people don't behave, so let's all get along."
You may wish to figure a way to name and manage consoles in more easy to read and type fashion. For instance, you could write a script to check for all available consoles and then assign a number or letter to them. The -a argument could be for all, but a -c argument could be for one particular console and knowing that you could use "A" or "A1" is a lot better than something else. This would require something like additional scripts to report consoles and designate them in a uniform way so when you run the announcement script it also enumerates consoles the same way.
I'm assuming that the command you cited does work and you're able to send messages to one specific, or all consoles and that this part works.
Therefore what you need to do is allow for flexibility and repeatability.
A script can do this for you. My favorite saying is "anything you can type in a command line, you can code into a BASH script". In my signature are links for beginning and advanced BASH programming guides as well as a pointer to my blog where I show some examples and how to debug scripts with some methods.
For flexibility, I don't see why you absolutely need a text file. Why not have a command where you could type something like:
Code:
announce -a "Everyone please welcome John to the game!"
And the script would be announce where arguments might be the -a for "all" or -c <console-name> for one given console, then followed by a string you type so you can be fully flexible in what you say versus need to anticipate stuff from within a text file and then have to figure out how to extract any given message from that file. What if gamers are misbehaving and you need to say "Hey! Lay of John! I will end the session if people don't behave, so let's all get along."
You may wish to figure a way to name and manage consoles in more easy to read and type fashion. For instance, you could write a script to check for all available consoles and then assign a number or letter to them. The -a argument could be for all, but a -c argument could be for one particular console and knowing that you could use "A" or "A1" is a lot better than something else. This would require something like additional scripts to report consoles and designate them in a uniform way so when you run the announcement script it also enumerates consoles the same way.
What I want is to send the messages into a screen.... How can I do it? How can i send the result of
Quote:
eval echo `shuf -n 1 randmsg.txt`
this to a screen?
For example
Quote:
screen -S screenname -X stuff 'What is up JrShadowAssassin.^m'
This will send this message to any screen... But I want to send the result of
Have you tried using backticks around the whole eval statement and inserting that into the screen -S command?
Sorry I don't know the exact answer, but I think I understand what you wish to do which is instead of putting a static string of some type to another screen, you wish to send the outcome of that "eval" statement. Therefore one thing to try might be:
My thinking is that this would follow BASH rules because it's the command line. And as a result you can do something like assign that string to a variable and then use the variable:
Code:
MYSTRING=`eval echo `shuf -n 1 randmsg.txt``
And you could also use that variable in your screen -S command:
Code:
screen -S screenname -X stuff '$MYSTRING'
However that would possibly be the same as my original attempt. In that I may have problems with syntax, and for me I'd attempt it, and re-attempt it using various grammar forms until I got it right. Perhaps someone viewing maybe understands the grammar way better and can offer a suggestion.
Have you tried using backticks around the whole eval statement and inserting that into the screen -S command?
Sorry I don't know the exact answer, but I think I understand what you wish to do which is instead of putting a static string of some type to another screen, you wish to send the outcome of that "eval" statement. Therefore one thing to try might be:
My thinking is that this would follow BASH rules because it's the command line. And as a result you can do something like assign that string to a variable and then use the variable:
Code:
MYSTRING=`eval echo `shuf -n 1 randmsg.txt``
And you could also use that variable in your screen -S command:
Code:
screen -S screenname -X stuff '$MYSTRING'
However that would possibly be the same as my original attempt. In that I may have problems with syntax, and for me I'd attempt it, and re-attempt it using various grammar forms until I got it right. Perhaps someone viewing maybe understands the grammar way better and can offer a suggestion.
well I tried this and it gives an error.. If you can check out this page I would be appreciated..
Please do not post duplicate questions. You'll just have to wait for someone who has a better grasp of the syntax to offer their solutions.
They are not really same questions... I asked here another question and my other thread is closed. Here I asked how to send a command to screen. I understood how to send but somehow screen does not accept that parameter. So i thought it would be whole another discussion.
It actually should send that command to screen but whatever i did does not send. I tried to copy the text i wanna send from a txt file and what not, it did not work.. tmux accepts that but screen has trouble to send that damn straightforward request.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.