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
  • 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
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

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.