gpt4 book ai didi

bash - 将 MAC 地址读取到 shell 变量会导致奇怪的行为

转载 作者:行者123 更新时间:2023-12-04 18:57:59 25 4
gpt4 key购买 nike

我正在运行这个脚本

#!/bin/bash

if [ $# != 2 ]
then
echo argument one must be the site number
echo argument two must be the vlan
fi

site=$1
vlan=$2
deviceNum=1

while read MACLine
do
echo arp -s 10.${site}.${vlan}.${deviceNum} "${MACLine}" temp
deviceNum=`expr $deviceNum + 1`
done < MAC.txt

我用这个作为输入
AC:CC:8E:0A:EF:67
AC:CC:8E:0B:AE:4B
AC:CC:8E:0B:CF:17
00:40:8C:FC:6D:86
00:40:8C:FA:61:B1
AC:CC:8E:0F:B1:C3
AC:CC:8E:0B:E5:2B
AC:CC:8E:0F:B1:D7
AC:CC:8E:0F:B1:DF

我得到这个奇怪的输入......当我将粘贴复制到 Windows 时(继续阅读)
arp -s 10.10.20.1 AC:CC:8E:0A:EF:67
temp
arp -s 10.10.20.2 AC:CC:8E:0B:AE:4B
temp
arp -s 10.10.20.3 AC:CC:8E:0B:CF:17
temp
arp -s 10.10.20.4 00:40:8C:FC:6D:86
temp
arp -s 10.10.20.5 00:40:8C:FA:61:B1
temp
arp -s 10.10.20.6 AC:CC:8E:0F:B1:C3
temp
arp -s 10.10.20.7 AC:CC:8E:0B:E5:2B
temp
arp -s 10.10.20.8 AC:CC:8E:0F:B1:D7
temp
arp -s 10.10.20.9 AC:CC:8E:0F:B1:DF
temp

但是,在 Ubuntu shell 中,我看到以下内容
temps 10.10.20.1 AC:CC:8E:0A:EF:67
temps 10.10.20.2 AC:CC:8E:0B:AE:4B
temps 10.10.20.3 AC:CC:8E:0B:CF:17
temps 10.10.20.4 00:40:8C:FC:6D:86
temps 10.10.20.5 00:40:8C:FA:61:B1
temps 10.10.20.6 AC:CC:8E:0F:B1:C3
temps 10.10.20.7 AC:CC:8E:0B:E5:2B
temps 10.10.20.8 AC:CC:8E:0F:B1:D7
temps 10.10.20.9 AC:CC:8E:0F:B1:DF

这种疯狂的方法是什么?

注意 : 这与冒号有关。如果您将 MAC 地址替换为“hello...hello...hello”之类的内容,它会按预期工作

最佳答案

您的输入文件中似乎有回车符。做dos2unix在输入文件上,这应该可以解决您的问题。

关于bash - 将 MAC 地址读取到 shell 变量会导致奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29751912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com