1

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
2
  • The << operator takes a string and then reads everything until it finds that string again. You need <<EOF\n$COUNTER\nEOF.
    – grochmal
    Commented Jun 12, 2016 at 0:10
  • 1
    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? Commented Jun 12, 2016 at 0:16

1 Answer 1

1

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

You must log in to answer this question.

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