[命令] Linux 命令 xargs (替代参数)

内容一:理解 xargs 的作用

1.1 Linux 命令可接收的内容

1) 第 1 种内容是输入
2) 第 2 中内容是参数

1.2 Linux 命令对可接收内容的态度

1) 有些命令既可以接收输入也可以接受参数
2) 有些命令只能接收输入
3) 有些命令只能接收参数

1.3 xargs 的作用

当管道符号 “|” 将前一个命令的输出结果转换为下一个命令的输入时,再将此输入转换为参数

1.4 理解 xargs 作用的案例

1.4.1 准备一个用于测试的文件
# echo "This is a test file" > test.txt
1.4.2 测试没有 xargs 命令的情况
# echo test.txt | cat
test.txt

(补充:此时 test.txt 对于 cat 而言是输入)

1.4.3 测试有 xargs 命令的情况
# echo test.txt | xargs cat
This is a test file

(补充:此时 test.txt 对于 cat 而言是参数)

内容二:xargs 的使用案例

2.1 案例一:xargs 对参数进行分行

# echo aa bb cc dd ee | xargs -n 2
aa bb
cc dd
ee

(补充:这里以设置每 2 个参数为 1 行为例)

2.2 案例二:xargs 指定分割参数的分割符

# echo aa@bb@cc@dd@ee | xargs -d @
aa bb cc dd ee

(补充:这里以将 @ 设置为分割符为例)

2.3 案例三:xargs 不输出参数,而是将参数传输给后面的命令,并让此命令使用此参数执行

# echo aa@bb@cc@dd@ee | xargs -d @ -p echo
echo aa bb cc dd ee
 ?...y
aa bb cc dd ee

(补充:这里以需要在执行命令 echo aa bb cc dd ee 前输入 y 为例)

2.4 案例四:xargs 只输出前几个参数

# echo aa bb cc dd ee | xargs -E cc echo
aa bb

(补充:这里以只输出在 cc 之前的参数,不包括 cc 本身为例)

(注意:当使用了 -d 参数后,-E 参数会失效)

2.5 案例五:xargs 将参数放到后面命令的指定位置

2.5.1 案例五:第 1 个具体案例
# echo test | xargs -I '{}' find '{}' -name a.txt


补充:
1) 这里以将命令 echo test 输出结果的值,传递给命令 find ‘{}’ -name a.txt 的 ‘{}’ 选项为例
2) xargs 默认会将处理后的参数整体传递到下 1 个命令的问题

或者:

2.5.1 案例五:第 2 个具体案例
# awk -F: '{print $1}' /etc/shadow | xargs -I{} bash -c 'chage -l {} '


补充:
1) 这里以将命令 awk -F: ‘{print $1}’ /etc/shadow 输出结果的值,传递给命令 bash -c ‘chage -l {} ‘ 的 ‘{}’ 选项为例
2) xargs 默认会将处理后的参数整体传递到下 1 个命令的问题

[步骤] team 网卡捆绑组的添加 (CentOS Linux & RHEL 版)

步骤一:显示现有的网卡有哪些

# nmcli connection show

步骤二:添加网卡捆绑组

2.1 添加网卡捆绑组

2.1.1 添加网卡捆绑组的命令格式
# nmcli connection add type team con-name <connection name of network card binding group> ifname <name of network card binding group> config ‘{“runner”:{“name”:”activebackup”}}’
2.1.2 添加网卡捆绑组的案例
# nmcli connection add type team con-name team0 ifname team0 config ‘{“runner”:{“name”:”activebackup”}}’

2.2 添加网卡捆绑组的子网卡

2.2.1 添加网卡捆绑组子网卡的命令格式
# nmcli connection add type team-slave con-name <network card connection name> ifname <subnet card name> master <name of network card binding group>
2.2.2 添加网卡捆绑组子网卡的案例
# nmcli con add type team-slave con-name team0-port0 ifname eth0 master team0
# nmcli con add type team-slave con-name team0-port1 ifname eth1 master team0

步骤三:给网卡捆绑组配置 IP 地址

# nmcli connection modify team0 ipv4.addresses 192.168.100.5/24 ipv4.gateway 192.168.100.1 ipv4.method manual autoconnect yes

步骤四:启动网卡捆绑组

4.1 显示现有的网卡捆绑组和对应的子网卡有哪些

# nmcli connection show

4.2 启动网卡捆绑组里的子网卡

4.2.1 启动网卡捆绑组里子网卡的命令格式
# nmcli connection up <subnet card name>
4.2.2 启动网卡捆绑组里的子网卡
# nmcli connection up team0-port0
# nmcli connection up team0-port1

或者:

# nmcli connection reload

4.3 启动网卡捆绑组

4.3.1 启动网卡捆绑组的命令格式
# nmcli connection up <name of network card binding group>
4.3.2 启动网卡捆绑组的案例
# nmcli connection up team0
4.3.3 显示网卡捆绑组的状态
# teamdctl team1 status

步骤五:确认网卡捆绑组的 IP 地址配置成功

# ip address show 

(补充:如果网卡组里出现了我们配置的 IP 地址,则代表 IP 地址配置成功)

步骤六:测试 team 网卡捆包组实现了主从互备

6.1 检查第 2 张子网卡是否工作正常

6.1.1 停用第 1 张子网卡
# ifconfig team0-eth0 down

(补充:这里以停用子网卡 team0-eth0 为例)

6.1.2 检查现在网络是否正常

(步骤略)

6.1.3 启动第 1 张子网卡
# ifconfig team0-eth0 up

(补充:这里以启用子网卡 team0-eth0 为例)

6.2 检查第 1 张子网卡是否工作正常

6.2.1 停用第 2 张子网卡
# ifconfig team0-eth1 down

(补充:这里以停用子网卡 team0-eth1 为例)

6.2.2 检查现在网络是否正常

(步骤略)

6.2.3 启动第 2 张子网卡
# ifconfig team0-eth1 up

(补充:这里以启用子网卡 team0-eth1 为例)

[内容] Linux 网卡速率的显示

方法一:通过网卡当前的工作状态得知

# ethtool ens192
Settings for ens192:
        Supported ports: [ TP ]
        Supported link modes:   1000baseT/Full 
                                10000baseT/Full 
        Supported pause frame use: No
        Supports auto-negotiation: No
        Supported FEC modes: Not reported
        Advertised link modes:  Not reported
        Advertised pause frame use: No
        Advertised auto-negotiation: No
        Advertised FEC modes: Not reported
        Speed: 10000Mb/s
        Duplex: Full
        Port: Twisted Pair
        PHYAD: 0
        Transceiver: internal
        Auto-negotiation: off
        MDI-X: Unknown
        Supports Wake-on: uag
        Wake-on: d
        Link detected: yes

(补充:由上面的输出结果可以得知 ens192 网卡目前工作在 1000baseT/Full 状态)

方法二:通过网卡的型号得知

# lspci -vvv | grep Ethernet
0b:00.0 Ethernet controller: VMware VMXNET3 Ethernet Controller (rev 01)
        Subsystem: VMware VMXNET3 Ethernet Controller

(补充:由上面的输出结果可以得知,网卡设备为 VMware VMXNET3 Ethernet Controller (rev 01) ,我们可以在网上搜索此设备名以得知此网卡的速率)