sed 轉義“.”並使用$變數?

sed 轉義“.”並使用$變數?

我不明白這種逃避。有可用的發電機嗎?

我需要用變數 $NEW_DNS 替換普通文字 (185...)

sed -i "185.228.168.168,185.228.169.168|$NEW_DNS /etc/wireguard/wg0.conf

輸出

sed: -e expression #1, char 4: unknown command: `.'

更新:我希望您可以看到答案及其工作,只是 /etc/wireguard/wg0.conf 沒有用新資料重寫(但輸出是正確的。

# client start menu
echo "What can i do for you today? "
options=("Show Clients" "Show your DNS Server" "Change your DNS Server" "Install/add new Client" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "Show Clients")
            ls *.conf
            ;;
            "Show your DNS Server")
            if [ $( head -n1 $CONF | awk '{print $5}') == "1.1.1.1,1.0.0.1" ]; then
                echo "Your Public DNS Server is Cloudflare (1.1.1.1,1.0.0.1), great unfiltered choice for best speed worldwide! "
            fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "176.103.130.130,176.103.130.131" ]; then
                echo "Your Public DNS Server is Adguard (176.103.130.130,176.103.130.131), Advertising Filter kill them all! "
            fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "84.200.69.80,84.200.70.40" ]; then
                echo "Your Public DNS Server is WATCH.DNS (84.200.69.80,84.200.70.40), great unfiltered choice for German Clients! "
            fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "9.9.9.9, 149.112.112.112" ]; then
                echo "Your Public DNS Server is QUAD9 (9.9.9.9, 149.112.112.112), great TLS encrypted unfiltered choice! "
            fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "77.88.8.7,77.88.8.3" ]; then
                echo "Your Public DNS Server is Yandex (77.88.8.7,77.88.8.3), safe choice with Family Filter! "
            fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "185.228.168.168,185.228.169.168" ]; then
                echo "Your Public DNS Server is Clean Browsing (185.228.168.168,185.228.169.168), Uuh TLS encrypted safe choice with Family Filter and Youtube-Safe Option! "
                fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "10.8.0.1" ]; then
                echo "Nothing else then own encrypted and logless dedicated DNS Server! "
                 fi
                 ;;
            "Change your DNS Server")
             if [ $( head -n1 $CONF | awk '{print $5}') == "1.1.1.1,1.0.0.1" ]; then
                 echo "Your Public DNS Server is Cloudflare (1.1.1.1,1.0.0.1), great unfiltered choice for best speed worldwide! "
             fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "176.103.130.130,176.103.130.131" ]; then
                 echo "Your Public DNS Server is Adguard (176.103.130.130,176.103.130.131), Advertising Filter kill them all! "
             fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "84.200.69.80,84.200.70.40" ]; then
                 echo "Your Public DNS Server is WATCH.DNS (84.200.69.80,84.200.70.40), great unfiltered choice for German Clients! "
             fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "9.9.9.9, 149.112.112.112" ]; then
                 echo "Your Public DNS Server is QUAD9 (9.9.9.9, 149.112.112.112), great TLS encrypted unfiltered choice! "
             fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "77.88.8.7,77.88.8.3" ]; then
                 echo "Your Public DNS Server is Yandex (77.88.8.7,77.88.8.3), safe choice with Family Filter! "
             fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "185.228.168.168,185.228.169.168" ]; then
                      echo "Your want leave your Clean Browsing server and use another one?. You need generate new configs later, if you want to use new Server (Start Menu choice: 3 ) "
                      # client choice NEW DNS
                          read -rp "Do you really want to change your DNS Server? (y/n) " -e -i y NEW_DNS
                            if [ "$NEW_DNS" == "y" ]; then
                              echo "Which DNS do you want to use with the VPN?"
                                echo "You recognise encrypted DNS with "TLS" (Port 853 can be potentially blocked through Government etc.) Some Servers use logs, but no one of them log aWireguard IP. "
                              echo "   1) Cloudflare: log: yes 24h, Filter: no, + best speed worldwide"
                              echo "   2) AdGuard: Log: yes, Filter: advertising"
                              echo "   3) DNS.WATCH: Log: no, Filter: no, + great speed for Germany"
                              echo "   4) Quad9: TLS: yes, Log: yes, Filter: no"
                              echo "   5) Yandex Family: TLS:no, Log: yes, Filter: adult"
                              echo "   6) Clean Browsing Family: TLS: yes, Log: yes, Filter: adult and explicit sites, Youtube- safe mode"
                              read -p "DNS [1-6]: " -e -i 1 NEW_DNS

                              case $NEW_DNS in
                                  1)
                                      NEW_DNS="1.1.1.1,1.0.0.1"
                                  ;;
                                  2)
                                  NEW_DNS="176.103.130.130,176.103.130.131"
                                  ;;
                                  3)
                                  NEW_DNS="84.200.69.80,84.200.70.40"
                                  ;;
                                  4)
                                  NEW_DNS="9.9.9.9, 149.112.112.112"
                                  ;;
                                  5)
                                  NEW_DNS="77.88.8.7,77.88.8.3"
                                  ;;
                                  6)
                                  NEW_DNS="185.228.168.168,185.228.169.168"
                                  ;;
                              esac
                            fi
                      # EOF client choices DNS
                fi
                sed "s/185\.228\.168\.168,185\.228\.169\.168/$NEW_DNS/" /etc/wireguard/wg0.conf
               if [ $( head -n1 $CONF | awk '{print $5}') == "10.8.0.1" ]; then
                 echo "Nothing else then own encrypted and logless dedicated DNS Server! "
                 fi
                 ;;
        "Install/add new Client")
            break
            ;;
        "Quit")
              exit
            ;;
    esac
done

答案1

如果您想用變數中的內容替換該字串本身:

sed "s|185\.228\.168\.168,185\.228\.169\.168|$NEW_DNS|" /etc/wireguard/wg0.conf

你需要sed s|string|replacement|。您可以sed s|string|replacement|g在整個文件中使用它來替換它。您還需要使用.反斜線進行轉義,以便它不代表單個字元。我已經刪除了-i開關,以便將更改寫入標準輸出,以便您可以確保它是您想要的。確認無誤後,您可以將其放回命令中。

如果您想編輯檔案本身,請將-i開關新增到您的命令中。

sed -i "s|185\.228\.168\.168,185\.228\.169\.168|$NEW_DNS|" /etc/wireguard/wg0.conf

答案2

sin 指令以sed給定的替換文字取代與給定正規表示式相符的任何內容。它的語法是s/expression/text/flags.

在你的情況下,你會想使用表達式

185\.228\.168\.168,185\.228\.169\.168

我已經轉義了這些點,因為它們在正則表達式中是“特殊的”(未轉義的點與任何單個字元匹配,但您想要匹配實際的點)。

要用作替換文字的文字位於變數中。這意味著整個sed表達式必須是雙引號字串(因為 shell 會忽略單引號字串中的變數)。

另外,您不需要s此處命令的可選標誌,因此我們得到

sed "s/185\.228\.168\.168,185\.228\.169\.168/$NEW_DNS/" filename

在您親眼看到正確的結果之前,請勿sed -i進行就地編輯。

另請注意,由於我們將變數的值注入到腳本中,因此如果包含任何字符sed,則會失敗(我認為在這種特殊情況下不太可能發生)。$NEW_DNS/

如果您用逗號表示“要么這個地址,要么該地址”,那麼您將必須使用替代(帶有|):

sed -E "s/185\.228\.168\.168|185\.228\.169\.168/$NEW_DNS/" filename

我在這裡使用是sed -E因為我們需要啟用擴展正則表達式(這|不是預設的基本正則表達式語法的一部分)。

相關內容