1

I am trying to write a daemon using bash. I will enter my partner's IP address and the connection will start using nc partner_ip 2015, likewise on the server I will start the connection using nc -l -p 2015.

I want to print my name every time I send a message and also to print my partner's name before his message. So first I need to work out my partner's username and IP address.

#! /bin/bash

function readPip()
{
    echo "Enter your partener IP"
    read ip
    export PIP= $ip
}


function readMip()
{
    mip=ip addr list eth0 |grep "inet " |cut -d' ' -f6|cut -d/ -f1
    echo -n "'my IP is"$mip"
    export MIP=$mip
}

function chat()
{
    [[ $PIP != " " ]]&& nc $PIP 2015
    [[ $PIP == " " ]] && nc -l -p 2015
}
function testConnect()
{
    nc $PIP 2015  > /dev/null
    echo "am here"
    return $1
}

readPip
testConnect
readMip
echo "$1"
  • Have you made any progress on this? Can you show us what you have achieved so far? – geedoubleya Oct 21 '14 at 11:42
  • am in my first steps in bash it is my second program so do not laugh – user2278127 Oct 21 '14 at 11:44
  • As a community we are all here to help and learn from one another. – geedoubleya Oct 21 '14 at 11:50
  • @user2278127 : It's hard to read code in comments! It's much easier to read if you paste it into your question as a code block (select your code then hit the {} button). Tiny bits of code are ok in comments, but when putting code into comments you should surround it with backticks so stuff doesn't get treated as formatting commands. – PM 2Ring Oct 21 '14 at 12:56
  • okay I edited the post now – user2278127 Oct 21 '14 at 13:02
1

function readPip()
{
echo "Enter your partener IP"
read ip
export PIP= $ip
}

The space after the equals sign is a syntax error. But in addition, all of this could be refactored to just

readPip () {
    read -p "Enter your partner's IP: " PIP
}

The export is unnecessary as you don't need external child processes to have access to this variable.

function readMip()
{
mip=ip addr list eth0 |grep "inet " |cut -d' ' -f6|cut -d/ -f1
echo -n "'my IP is"$mip"
export MIP=$mip
}

Again, this contains an error; you assign to mip the value ip and then (try to) run the command addr list ..., where apparently the intent is to run ip addr list ... and assign the result of that to MIP.

readMip () {
    ip addr list eth0 |grep "inet " |cut -d' ' -f6|cut -d/ -f1`
}

Here, I would simply leave the assignment to the caller (readPip could be refactored the same way for consistency).

In the main code, you'd have

MIP=$(readMip)

Again, the export is neither necessary nor useful.

function chat()
{
[[ $PIP != " " ]]&& nc $PIP 2015
[[ $PIP == " " ]] && nc -l -p 2015
}

This is clearly "if - then - else" and should be written accordingly.

chat () {
    if [[ "$PIP" == " " ]]; then
        nc -l -p 2015
    else
        nc "$PIP" 2015
    fi
}

or even

[[ "$PIP" == " " ]] && nc -l -p 2015 || nc "$PIP" 2015

Notice also that I dropped the Bash-only function keyword. You still have a couple of Bashisms here but those could trivially be refactored for a POSIX-portable script.

0

Wrap both nc calls with unbuffered sed calls:

sed -u "s/^/My name: /" | nc PARTNER_IP 2015

sed -u "s/^/Partner name: /" | nc -l 2015
  • #! /bin/bash function readPip() { echo "Enter your partener IP" read ip export PIP= $ip } function readMip() { mip=ip addr list eth0 |grep "inet " |cut -d' ' -f6|cut -d/ -f1 echo -n "'mكIP is"$mip" export MIP=$mip } function chat() { [[ $PIP != " " ]]&& nc $PIP 2015 [[ $PIP == " " ]] && nc -l -p 2015 } function testConnect() { nc $PIP 2015 > /dev/null echo "am here" return $1 } readPip testConnect readMip echo "$1" – user2278127 Oct 21 '14 at 11:48
  • that is my code where should i replace it – user2278127 Oct 21 '14 at 11:49
  • code is duplicated i think that at least 2 functions are doing the same work am i right? – user2278127 Oct 21 '14 at 11:49
  • when sed -u "s/^/My name: /" | nc 127.0.0.1 2015 (UNKNOWN) [127.0.0.1] 2015 (?) : Connection refused – user2278127 Oct 21 '14 at 12:29

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.