LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 11-04-2015, 11:05 AM   #1
sleftar
Member
 
Registered: Sep 2015
Posts: 51

Rep: Reputation: Disabled
Sending a random message to screen


Hi guys,

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

Quote:
screen -S overworldserverconsole -X stuff 'c_announce"Hello people. What's up?"^m'
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.

Last edited by sleftar; 11-04-2015 at 11:26 AM.
 
Old 11-04-2015, 11:54 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
Old 11-04-2015, 12:06 PM   #3
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,624

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
you can take a look at what some "toys" do , like
"cowsay"
"cowthink"
and who can forget
"fortune"
 
Old 11-04-2015, 12:15 PM   #4
jamison20000e
Senior Member
 
Registered: Nov 2005
Location: ...uncanny valley... infinity\1975; (randomly born:) Milwaukee, WI, US( + travel,) Earth&Mars (I wish,) END BORDER$!◣◢┌∩┐ Fe26-E,e...
Distribution: any GPL that work on freest-HW; has been KDE, CLI, Novena-SBC but open.. http://goo.gl/NqgqJx &c ;-)
Posts: 4,888
Blog Entries: 2

Rep: Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567Reputation: 1567
"
Code:
toilet
" anyone?
http://www.linux.com/learn/docs/6869...let-and-toilet

Last edited by jamison20000e; 11-04-2015 at 12:17 PM.
 
Old 11-04-2015, 03:04 PM   #5
sleftar
Member
 
Registered: Sep 2015
Posts: 51

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rtmistler View Post
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

Quote:
eval echo `shuf -n 1 randmsg.txt`
this to a screen...

Last edited by sleftar; 11-04-2015 at 03:11 PM.
 
Old 11-05-2015, 07:30 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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:
Code:
screen -S screenname -X stuff '`eval echo `shuf -n 1 randmsg.txt``'
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.

Last edited by rtmistler; 11-05-2015 at 07:33 AM.
 
Old 11-05-2015, 07:55 AM   #7
sleftar
Member
 
Registered: Sep 2015
Posts: 51

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rtmistler View Post
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:
Code:
screen -S screenname -X stuff '`eval echo `shuf -n 1 randmsg.txt``'
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..

http://www.linuxquestions.org/questi...em-4175558054/
 
Old 11-05-2015, 07:58 AM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by sleftar View Post
well I tried this and it gives an error.. If you can check out this page I would be appreciated..

http://www.linuxquestions.org/questi...em-4175558054/
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.
 
Old 11-05-2015, 09:36 AM   #9
sleftar
Member
 
Registered: Sep 2015
Posts: 51

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rtmistler View Post
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.

Last edited by sleftar; 11-05-2015 at 10:16 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SMTP random ip address in sending email conflicker Linux - Newbie 5 12-19-2012 01:17 PM
wvdial keeps sending random garbage to screen / terminal (Debian) stevovo Linux - Networking 2 01-05-2010 03:07 AM
Random Syslog Message About IRQ Disabled? carlosinfl Linux - Hardware 4 09-20-2007 06:53 PM
Sending a winpopup message Reefcrazed Linux - Networking 2 08-29-2004 10:31 AM
Sending message ieuuk Linux - Networking 2 08-29-2004 08:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration