Shell 脚本 cat | grep 的问题

Shell 脚本 cat | grep 的问题
#!/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

我如何只获取 443 行而不获取 4433 行?它将两者都输入到变量中进行检查。如果我手动执行字符串,它会将下面的字符串放入变量中。

10.0.0.30:443 10.0.0.30:4433

以下是我反复结束的输出:

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

以下是我的 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

脚本的输出壳牌检测. 纠正问题:

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.

答案2

代替

grep ${mylanip}:443

grep "${mylanip}:443 "

因此 443 后面的空格成为搜索表达式的一部分。

相关内容