我正在用这个把头撞到墙上...... 我正在尝试创建一个简单的脚本,将文件作为数组加载,并通过文件搜索每一行是否存在匹配。
脚本看起来像这样:
#!/usr/bin/env bash
echo "Please enter the full path to the directory where the log files are located:"
read -r log_directory
readarray -t lines < mac-addresses.txt
echo "Loaded ${#lines[@]} mac addresses to search for."
for mac_address in "${lines[@]}"
do
found=$(find "$log_directory" -name 'dhcpd.log.[1-30]' -exec grep "$mac_address" {} + | wc -l)
echo "Result for $mac_address $count"
done
这就是我调用脚本时发生的情况:
Please enter the full path to the directory where the log files are located:
./logs
Loaded 2 mac addresses to search for.
0
esult for d4:3d:7e:ce:24:14
0
esult for 00:12:34:56:78:9a
echo
命令的输出会被破坏?find "$log_directory" -name 'dhcpd.log.[1-30]' -exec grep "$mac_address" {} + | wc -l
的输出?我尝试过使用
$()
,尝试过使用反引号,但我似乎无法正确捕获命令。
这是一个输出示例:
$ find "./logs" -name 'dhcpd.log.[1-30]' -exec grep "d4:3d:7e:ce:24:14" {} + | wc -l
3
arned@Saturn MINGW64 /d/src/Scripts/mac-address-searcher
$ find "./logs" -name 'dhcpd.log.[1-30]' -exec grep "00:12:34:56:78:9a" {} + | wc -l
0
所以该命令正在执行我想要的操作,但我只是无法让它在我的 bash 脚本中正常工作...
感谢@KamilCuk 指出了 Windows 的问题:
# Remove special characters from Windows such as the carriage return.
# Otherwise the search will fail as the input gets messed up.
cleaned_entry=${mac_address//$'\r'}
由于输入文件来自Windows系统,因此每个条目都需要删除回车符。这使得命令正常工作。