简体   繁体   English

Bash从Bash中的变量读取CSV值

[英]Bash Read CSV value from Variable in Bash

So in a script I have a variable that changes each time the script is run. 因此,在脚本中,我有一个变量,该变量在每次运行脚本时都会更改。 The variable is IP_PING. 变量是IP_PING。 It is basically a list of if address seperated by commas. 它基本上是if地址列表,以逗号分隔。

I want the script to take each ip addess in the csv variable and ping it. 我希望脚本将csv变量中的每个ip addess进行ping操作。 The example below works fine if I only have one ip address in the variable. 如果我在变量中只有一个IP地址,则下面的示例工作正常。

IP_PING="192.168.1.1"
echo "$IP_PING" | while read -r -d, ip1 
 do
   ping -c 4 "$ip1" >/dev/null 
     if [ $?  -ne 0 ]; then
     echo "Ping fail"
     else
     echo "Ping success"
     fi
 done

But if I change the IP_Ping variable to = "192.168.1.1,192.168.100.1" The script only reads the first value 192.168.1.1. 但是,如果我将IP_Ping变量更改为=“ 192.168.1.1,192.168.100.1”,则脚本仅读取第一个值192.168.1.1。 Is "while read" the wrong command to use for this. “读取时”是否为此使用了错误的命令。 It is reading each value as a seperate column. 它正在将每个值作为单独的列读取。 I want all the values in a single column and then do the while on each value. 我希望所有值都在一个列中,然后在每个值上执行一会儿。 If that makes sense. 如果这样的话。

"192.168.1.1,192.168.100.1"

Here is your issue. 这是你的问题。 The problem is that when the while loop is reading, after the first iteration there is no more commas so it hits the end and quits. 问题在于,当while循环正在读取时,在第一次迭代之后不再有逗号,因此它到达结尾并退出。 "192.168.1.1,192.168.100.1," should show the way you are intending. "192.168.1.1,192.168.100.1,"应显示您的预期方式。

As other's have suggested in the comments, I would go with a for loop myself, but to with closet to how you were trying to implement it, this would work for you: 正如其他人在评论中所建议的那样,我本人会使用for循环,但是要紧跟您尝试实现的方式,这对您有用:

IP_PING="192.168.1.1,192.168.1.2,192.168.1.3"
while read -r ip1 ; do
   ping -c 4 "$ip1" >/dev/null 
     if [ $?  -ne 0 ]; then
     echo "Ping fail"
     else
     echo "Ping success"
     fi
done < <(echo $IP_PING |tr ',' '\n')

< <(echo $IP_PING |tr ',' '\\n') is process substitution. < <(echo $IP_PING |tr ',' '\\n')是进程替换。 To put it simply it will "pipe" the output into the while loop. 简单地说,它将“输出”到while循环中。 The difference from a normal pipe being that it creates one less process; 与普通管道的不同之处在于,它减少了一个过程。 the actual pipe creates a process itself. 实际的管道本身会创建一个过程。

Using process substitution will also keep the while loop in the current environment as well. 使用进程替换也将while循环保留在当前环境中。 (vs a while loop usually takes a 'snapshot' of the env and uses that during the loop, then it disappears when the loop ends) Meaning, with process substitution, that processes in the while loop will be able to see changes in env variables of the parent while it is looping, AND variables assigned within the loop will also still be available in the parent after it ends. (vs while循环通常会获取env的“快照”,并在循环期间使用它,然后在循环结束时消失)意味着,使用进程替换,while循环中的进程将能够看到env变量的变化循环时,在循环中分配的AND变量在循环中分配的AND变量在循环结束后仍然仍然可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM