我正在尝试编写一个用于读取存储的连接的脚本。这些连接存储在:
/etc/NetworkManager/system-connections
但我不需要所有信息,只想要姓名和密码。
我使用此命令列出所有不带空格的文件:
ls -b1A /etc/NetworkManager/system-connections
我使用 cat 命令来 grep 我想要的数据
cat MyWifi.nmconnection | grep -v -e ssid -e uuid | grep -e psk= -e id
id=WifiName
psk=WifiPass
我有一个简单的脚本可以运行这两个命令,但是当 .nmconnection 文件有空格时就会出现问题。
bash script.sh
My\ Wifi\ Connection.nmconnection
MyWifi.nmconnection
lets cat and grep those files
cat: 'My\': No such file or directory
cat: 'Wifi\': No such file or directory
cat: Connection.nmconnection: No such file or directory
id=WifiName
psk=WifiPass
我尝试使用“IFS=”多次但无法找到解决方案。
这是我的脚本:
#!/bin/bash
#list files with ls -b1A to get one file for each line
files=$(ls -b1A *.nmconnection)
#display it to see it's ok
echo "$files"
#friendly echo before starting to cat and grep
echo -e "\nlets cat and grep those files"
for f in $files
do
cat $f | grep -v -e ssid -e uuid | grep -e psk= -e id
done