I'm new to shell programming and I have created a script that opens a connection to a server of mine. I want to have this script listen for an input from a client node and use that to run a function.

This is my process. Run script > opens listener > on second computer use netcat to connect > run a function in the script on the server called nodefunction

I have server_port coded to '4444'

File name: run_hangman

nc -l -k -v -p 4444 | bash hangman

File name: hangman



initServer() {
    hostIP=`ip -o addr show dev "eth0" | awk '$3 == "inet" {print $4}' | sed -r 's!/.*!!; s!.*\.!!'`
    hostOws=`echo $hostIP | cut -d . -f 4`
servermsg(){ #message    //if = --n, echos on same line
    if [ "$1" != "--n" ]
        echo `date +"%T"` "[SERVER] "$1
        echo -n `date +"%T"` "[SERVER] "
owsmsg(){    #message    //if = --n, echos on same line
    if [ "$1" != "--n" ]
        echo `date +"%T"` "[OWS]    "$1
        echo -n `date +"%T"` "[OWS]    "
playermsg() {
    if [ "$1" != "--n" ]
        echo `date +"%T"` "[PLAYER]    "$1
        echo -n `date +"%T"` "[PLAYER]    "
question(){  #question, read, example
    servermsg "$1"
    if [ -n "$3" ]
        servermsg "$3"
    read $2
    echo ""
owsArray(){   #
    for targetOws in $player_list
    echo -n ${owsArray[*]}
openSocket() {
    servermsg "Starting the Game Listener"
    servermsg "Opening Listener on port "$server_port
    #nc -k -l $server_port |bash    
    #nc -kl -q 1 -p $server_port # This should create the listener & This is where everything stops.
    servermsg "Now listening on port "$server_port

owsmsg "Starting server on OWS"$hostOws"..."

question "Enter all the OWSs that will play:" player_list "Example: 1 9 14 23"
echo $player_list
question "Type a category hint:" game_cat "Example: Type of Animal"
question "Type your word:" game_word "Example: zebra"
question "How many guesses:" game_guesses "Example: 7"

servermsg "OWS"$host_ows "has created a Hangman session"
servermsg "Players are:"; servermsg --n; owsArray
servermsg "Your word is "${#game_word}" letters long and players have "$game_guesses" guesses"
question "If this is all correct press enter, or CTRL+C to cancel"


# I think I need a While script here to read the RAW input and run the playermsg function with the input?

I run the run_hangman file and then I connect to it via my node computer. I enter the following line and echo "1 2 3" because that is what I need. I also can't enter "1 2 3" directly into the window running "run_hangman" as if I press enter it goes to a new line.

echo "1 2 3" >/dev/tcp/xxx.xxx.xxx.xxx/4444

The server shows that it connected

Listening on [] (family 0, port 4444)
14:52:24 [OWS]    Starting server on OWS225...
14:52:24 [SERVER] Enter all the OWSs that will play:
14:52:24 [SERVER] Example: 1 9 14 23
Connection from [xxx.xxx.xxx.xxx] port 4444 [tcp/*] accepted (family 2, sport 41564)
Connection closed, listening again.

1 2 3

Now once it gets to openSocket it will allow me to send one more echo and then it closes on the server. I need to get what I presume is a while statement and have it listen for an input like "playermsg 'has started a game'" and have it actually run that function on the server.

Will I be able to get this to run, almost seems like it has to be in the background? I've been using nc(1) for reference and some websites said to try -d and that didn't work either.

  • Is your bashscript named nodefunction.sh?
    – eyoung100
    Apr 27, 2016 at 4:17
  • No its not, if it was wouldn't it run the whole thing again? Apr 27, 2016 at 13:21
  • Command not found indicates that the script cannot be found. I'll write up an answer fir you to accept soon, but for now try renaming it, and then issuing chmod +x
    – eyoung100
    Apr 27, 2016 at 18:32
  • I appreciate the help. I'm going to try to give you a better example as well Apr 27, 2016 at 18:37

1 Answer 1


I got it figured out. I did indeed need a while statement.

while read -r value; do
    if [[ "$value" == playermsg* ]]; then 
        playermsg $val
    elif [[ "$value" == servermsg* ]]; then 
        servermsg $val
        echo "Returned "$value
        echo "Value was "$val

So now on the second computer I simply run

echo "playermsg testing" >/dev/tcp/xxx.xxx.xxx.xxx/4444

Then the server displays the following:

15:29:36 [SERVER] Starting the Game Listener
15:29:36 [SERVER] Opening Listener on port 4440
15:29:36 [SERVER] Now listening on port 4440
15:29:37 [PLAYER] testing
  • Great work... Now add a cron job to check that the port stays open if your net is flaky
    – eyoung100
    Apr 28, 2016 at 20:23
  • I was so excited to test this, but it turns out I can't use netcat. I'm running servers that are on an intranet only and can't install it manually by transferring files via usb as that is prohibited. So unless you have an idea how to do this an other way, this won't work. Apr 29, 2016 at 18:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .