シェルに入れ子になった区切り記号

シェルに入れ子になった区切り記号

シェルスクリプトでネストされたheredocsを操作する方法は? EOFを認識していないようです。

#!/bin/bash

test(){

    cat > /users/shared/test.sh << EOF
#!/bin/bash

check_VPN_status(){
# Returns vpn_status as connected // disconnected

    anyconnect_status=$(/opt/cisco/anyconnect/bin/vpn status | grep 'Connected')
    globalprotect_status=$(ifconfig | grep -c flags=8050)

    if [[ ! -z $anyconnect_status \
        || $globalprotect_status == 0 && -d /Applications/GlobalProtect.app ]]; then
        vpn_status=connected
        else
        vpn_status=disconnected
    fi
}

create_pf_conf(){

# Set the network interface to be used
if="en0"

# Set ports to be allowed
allowed_ports="{22}"

cat > "$pfconf" << EOF

# Default Deny Policy
block in all

# Skip the loop back interface
set skip on lo0

# Allowed inbound ports
pass in quick on $if proto tcp to any port $allowed_ports keep state

EOF
}

#----------------------------------------------------------#
#                         Global Variables                 #
#----------------------------------------------------------#

pfconf="/var/client/pf.conf"

#----------------------------------------------------------#
#                         Start Workflow                   #
#----------------------------------------------------------#


# Check if firewall is enabled and enable if needed
enable_firewall

# Get VPN connection status
check_VPN_status

if [[ $vpn_status == "connected" ]]; then
    # If connected to VPN, create pf.conf and enable pf
    create_pf_conf
    /sbin/pfctl -e -f $pfconf
else
    # If disconnected from VPN, disable pf
    /sbin/pfctl -d
fi

}

    EOF

/bin/echo "hi"
test```

ベストアンサー1

EOF の代わりに別の「識別子」を外部区切り文字として使用します。


    cat > /users/shared/test.sh << ABC

    # ... inner heredoc using EOF goes here...

    ABC

ドキュメントを参照してください。https://tldp.org/LDP/abs/html/here-docs.html

「コマンドリストのどこにも表示されず、混乱を引き起こすのに十分な珍しい制限文字列を選択してください」と述べています。

おすすめ記事