使用脚本将 Nic 卡的 IP 地址打印到 bash

Printing IP address of Nic card to bash using script

目前我正在 Linux 使用 Vim 完成一项学校作业,我必须编写一个脚本来显示当前登录的用户、时间和日期,并且仅列出Nic卡的IP地址。除了 IP 地址部分,我一切正常。如果有人能提供帮助,我将不胜感激。

编辑以包含我目前拥有的代码。

#!/bin/bash
Time=$(date)
IP=$(ifconfig ens33)
echo "The following user is currently logged in $USER"
echo ""
echo "The current time is $Time"
echo ""
echo "The IP information is $IP"

您可以像这样使用 awk 过滤 ifconfig 的结果 (IPv4):

$ ifconfig ens33 | awk '/inet addr/{print substr(, 6)}'

结果:

10.10.xx.xx

inet addr:表示IPv4地址。
inet6 addr:表示IPv6地址。

这一行

IP=$(ifconfig ens33| grep inet | sed 's/ */ /'  | cut -d" " -f3)