`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)"

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

相关内容