
我想要做的是解析一个csv
包含服务器名称和 IP 地址的文件并将它们分配给变量,然后我使用每组变量运行命令。
因此,如果cvs
文件test.csv
如下所示:
servername, ip address
alphaserver, 192.168.1.2
betaserver, 192.168.1.3
... 等等
我该如何解析它以制作$server1=alphaserver $ipaddress1=192.168.1.2
每一行?
我所在位置的示例:
INPUT=test.cvs IFS=, [ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; } while read "server name" "IP Address" do echo "Server name : $servername" echo "IP Address : $ipaddress"
答案1
没关系。我懂了。只需留在 while 组中。获取变量,然后在设置它们之后执行与它们相关的任何操作,如下所示:
#!/bin/bash
IFS=","
while read f1 f2
do
echo "Server name : $f1"
echo "IP Address : $f2"
servername=$f1
ipaddress=$f2
echo "Servername variable: $servername"
echo "IP address variable: $ipaddress"
# Insert whatever you need to do with those variables and then the
# while loop will move on to the next line and do that same thing.
done < test.csv