Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am attempting to write a bash loop that passes a bash string into netcat. The command that I execute is:

nc -nv [ipaddress] [port] << EOF
10000
EOF

I am writing a bash loop to perform this and I've tried a number of options. Here's the basic one I've tried that doesn't seem to work:

#!/bin/bash
# declare number
COUNTER=0
END=$(bc <<< -2^10)
while [ $COUNTER -gt $END ]; do
        nc -nv [IPADDRESS] [PORT] << $COUNTER
        let COUNTER=COUNTER-1
done
share|improve this question
    
The << operator takes a string and then reads everything until it finds that string again. You need <<EOF\n$COUNTER\nEOF. – grochmal Jun 12 '16 at 0:10
    
Since you appear to want to send only a single value, why not use a here string <<< "$COUNTER" rather than trying to use a here document? – steeldriver Jun 12 '16 at 0:16

Thanks to grochmal and steeldriver for the ideas. The answer was to use <<<

#!/bin/bash
# declare number
COUNTER=0
END=$(bc <<< -2^10)
while [ $COUNTER -gt $END ]; do
    nc -nv [IPADDRESS] [PORT] <<< $COUNTER
    let COUNTER=COUNTER-1
done

If you want more information about here strings it can be found at: http://www.tldp.org/LDP/abs/html/x17837.html

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.