0
#!/bin/bash
mylanip=$(hostname -I | awk '{print $1}')
touch /opt/portmapper/0prelims
echo `java -jar /opt/portmapper/portmapper.jar -list | grep $mylanip > /opt/portmapper/0prelims`
# HTTPS
portprelim=$(cat /opt/portmapper/0prelims | grep ${mylanip}:443 | awk '{print $4}')
if [ "$portprelim" = $mylanip:443 ]; then
    echo "Nothing Altered for HTTPS"
fi
if [ "$portprelim" != $mylanip:443 ]; then
    java -jar /opt/portmapper/portmapper.jar -lib org.chris.portmapper.router.sbbi.SBBIRouterFactory -delete -protocol TCP -internalPort 443 -externalPort 443  >/dev/null ; sleep 2 ;
    java -jar /opt/portmapper/portmapper.jar -lib org.chris.portmapper.router.sbbi.SBBIRouterFactory -add -protocol TCP -internalPort 443 -externalPort 443 -ip $mylanip -description HTTPS >/dev/null ; sleep 2 ;
    echo "Added port forwarding for HTTPS"
fi
# Private TTY
portprelim=$(cat /opt/portmapper/0prelims | grep ${mylanip}:57 | awk '{print $4}')
if [ "$portprelim" = $mylanip:57 ]; then
    echo "Nothing Altered for Private TTY"
fi
if [ "$portprelim" != $mylanip:57 ]; then
    java -jar /opt/portmapper/portmapper.jar -lib org.chris.portmapper.router.sbbi.SBBIRouterFactory -delete -protocol TCP -internalPort 57 -externalPort 57  >/dev/null ; sleep 2 ;
    java -jar /opt/portmapper/portmapper.jar -lib org.chris.portmapper.router.sbbi.SBBIRouterFactory -add -protocol TCP -internalPort 57 -externalPort 57 -ip $mylanip -description PrivateTTY  >/dev/null ; sleep 2 ;
    echo "Added port forwarding for Private TTY"
fi
# HTTP
 portprelim=$(cat /opt/portmapper/0prelims | grep ${mylanip}:80 | awk '{print $4}')

if [ "$portprelim" = $mylanip:80 ]; then
    echo "Nothing Altered for HTTP"
fi
if [ "$portprelim" != $mylanip:80 ]; then
    java -jar /opt/portmapper/portmapper.jar -lib org.chris.portmapper.router.sbbi.SBBIRouterFactory -delete -protocol TCP -internalPort 80 -externalPort 80 >/dev/null ; sleep 2 ;
    java -jar /opt/portmapper/portmapper.jar -lib org.chris.portmapper.router.sbbi.SBBIRouterFactory -add -protocol TCP -internalPort 80 -externalPort 80 -ip $mylanip -description HTTP  >/dev/null ; sleep 2 ;
    echo "Added port forwarding for HTTP"
fi
exit

How do I only get the 443 line and not the 4433 line? its entering both into the variable for checking. If I manually do the string it places the string below into the variable.

10.0.0.30:443 10.0.0.30:4433

Here is my ending output repeatedly:

Added port forwarding for HTTPS
Nothing Altered for PrivateTTY
Nothing Altered for HTTP

here is an excerpt from my 0prelims

12:45:43.322 [main] INFO  o.f.cling.transport.spi.StreamServer - Created server (for receiving TCP streams) on: /10.0.0.30:44725
12:45:43.324 [main] INFO  o.f.cling.transport.spi.DatagramIO - Creating bound socket (for datagram input/output) on: /10.0.0.30
TCP :7881 -> 10.0.0.30:7881 enabled Deluge 1.3.12 at 10.0.0.30:7881
TCP :4433 -> 10.0.0.30:4433 enabled Deluge 1.3.12 at 10.0.0.30:4433
UDP :7881 -> 10.0.0.30:7881 enabled Deluge 1.3.12 at 10.0.0.30:7881
TCP :57 -> 10.0.0.30:57 enabled PrivateTTY
TCP :443 -> 10.0.0.30:443 enabled HTTPS
TCP :80 -> 10.0.0.30:80 enabled HTTP
1

Output of your script on ShellCheck. Correct the problems:

Line 4:
echo `java -jar /opt/portmapper/portmapper.jar -list | grep $mylanip > /opt/portmapper/0prelims`
     ^-- SC2046: Quote this to prevent word splitting.
     ^-- SC2005: Useless echo? Instead of 'echo $(cmd)', just use 'cmd'.
     ^-- SC2006: Use $(..) instead of legacy `..`.
                                                            ^-- SC2086: Double quote to prevent globbing and word splitting.

Line 6:
portprelim=$(cat /opt/portmapper/0prelims | grep ${mylanip}:443 | awk '{print $4}')
                 ^-- SC2002: Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead.
                                                 ^-- SC2086: Double quote to prevent globbing and word splitting.

Line 7:
if [ "$portprelim" = $mylanip:443 ]; then
                     ^-- SC2086: Double quote to prevent globbing and word splitting.

Line 10:
if [ "$portprelim" != $mylanip:443 ]; then
                      ^-- SC2086: Double quote to prevent globbing and word splitting.

Line 12:
    java -jar /opt/portmapper/portmapper.jar -lib org.chris.portmapper.router.sbbi.SBBIRouterFactory -add -protocol TCP -internalPort 443 -externalPort 443 -ip $mylanip -description HTTPS >/dev/null ; sleep 2 ;
                                                                                                                                                                ^-- SC2086: Double quote to prevent globbing and word splitting.

Line 16:
portprelim=$(cat /opt/portmapper/0prelims | grep ${mylanip}:57 | awk '{print $4}')
                 ^-- SC2002: Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead.
                                                 ^-- SC2086: Double quote to prevent globbing and word splitting.

Line 17:
if [ "$portprelim" = $mylanip:57 ]; then
                     ^-- SC2086: Double quote to prevent globbing and word splitting.

Line 20:
if [ "$portprelim" != $mylanip:57 ]; then
                      ^-- SC2086: Double quote to prevent globbing and word splitting.

Line 22:
    java -jar /opt/portmapper/portmapper.jar -lib org.chris.portmapper.router.sbbi.SBBIRouterFactory -add -protocol TCP -internalPort 57 -externalPort 57 -ip $mylanip -description PrivateTTY  >/dev/null ; sleep 2 ;
                                                                                                                                                              ^-- SC2086: Double quote to prevent globbing and word splitting.

Line 26:
 portprelim=$(cat /opt/portmapper/0prelims | grep ${mylanip}:80 | awk '{print $4}')
                  ^-- SC2002: Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead.
                                                  ^-- SC2086: Double quote to prevent globbing and word splitting.

Line 28:
if [ "$portprelim" = $mylanip:80 ]; then
                     ^-- SC2086: Double quote to prevent globbing and word splitting.

Line 31:
if [ "$portprelim" != $mylanip:80 ]; then
                      ^-- SC2086: Double quote to prevent globbing and word splitting.

Line 33:
    java -jar /opt/portmapper/portmapper.jar -lib org.chris.portmapper.router.sbbi.SBBIRouterFactory -add -protocol TCP -internalPort 80 -externalPort 80 -ip $mylanip -description HTTP  >/dev/null ; sleep 2 ;
                                                                                                                                                              ^-- SC2086: Double quote to prevent globbing and word splitting.
0

Instead of

grep ${mylanip}:443

do

grep "${mylanip}:443 "

so the whitespace after 443 get part if the search expression.

  • Quoting treats it as exact text. Turns the string yellow and does not express the variable. – Vincent Bird Apr 10 '16 at 19:16

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.