I am using the function below to increment an outside script. The main purpose of this is just to return some data from ifconfig and ping test outputs. I am trying to set them inside the same function using variables, just to do some other "pipes" and concactenate them again with other information. But I got this behavior. Basically, what I am trying to do is:
> for i in $(ifconfig | grep "inet addr" | tr -s " " | cut -d" " -f 3 | cut -d: -f2) ; do ping -c1 $i | grep packet | a=`echo $i-$(cut -d, -f3)` | echo $a; done 4 4 4 4
As you can see, it returns the number 4 for each iteration.
If I just remove the variable attribution, I get the output I was expecting to set in variable 'a' at first place:
> for i in $(ifconfig | grep "inet addr" | tr -s " " | cut -d" " -f 3 | cut -d: -f2) ; do ping -c1 $i | grep packet | echo $i-$(cut -d, -f3); done 10.0.2.15- 0% packet loss 192.168.0.2- 0% packet loss 10.0.2.100- 0% packet loss 127.0.0.1- 0% packet loss
- Why am I getting number 4 as output in first function code execution?
- How can I set a variable inside the same function and echo it again after some pipelines? Is that possible?
Best regards !