`curl ifconfig.me` 在 shell 腳本中表現不同

`curl ifconfig.me` 在 shell 腳本中表現不同

我只是嘗試了一個簡單的基於選單的 shell 腳本來取得我係統的公共 IP。使用(curl ifconfig.me)..但我發現它的行為不同。

在這裡我嘗試過:

anupam@JAZZ:~/Desktop$ cat menuui
#
# Script to create simple menus and take action according to that selection
# menu item
#
while :
do
    clear
    echo "___________________________________"
    echo "Main Menu"
    echo "___________________________________"
    echo "[1] Show Today's date/time"
    echo "[2] Show files in current directory"
    echo "[3] Show calendar"
    echo "[4] Start a calculator"
    echo "[5] Start editor to write letters"
    echo "[6] Show your public ip"
    echo "[7] Exit/Stop"
    echo "===================================="
    echo -n "Enter your menu choice[1-5]:"
    read yourch
    case $yourch in
    1) echo "Today is `date` ,press a key..." ; read ;;
    2) echo "Files in `pwd` "; ls -l;echo "Press a key..."; read;;
    3) cal; echo "Press a key..." ; read ;;
    4) bc;;
        5) vi;;
        6) echo "Your public ip is `curl ifconfig.me`";;
    7) exit 0 ;;
    *) echo "Opps!!! Please select choices 1,2,3,4 or 5";
    echo "Press a key...";read;;
    esac
done

然後我執行了它... 在此輸入影像描述

這不是我所期望的..我該如何解決這個問題?

答案1

curl與選項一起使用-s。從man curl

-s, --silent
          Silent  or  quiet  mode. Don't show progress meter or error mes‐
          sages.  Makes Curl mute. It will still output the data  you  ask
          for, potentially even to the terminal/stdout unless you redirect
          it.

它不會顯示其他詳細信息,例如進度表或錯誤訊息。修改後的語句將如下所示:

echo "Your public ip is $(curl -s ifconfig.me)"

$()比命令替換引用更好用``

相關內容