How to break array and extract individual components in a shell script
Hey Guys,
I have a shell script below, basically, the intent of the script is to enable firewall rule on remote Windows servers for WinRM so that ANsible can perform configuration management. Code:
Code:
./temp.sh |
Made it simpler, all what we want is individual hostnames from an array.
Code:
#!/bin/bash Output: Looks good but it not going for next hostname until end of the array: Code:
./temp.sh |
Your quoted array is not splitting, is this intentional?
For example. Code:
$ foo='this is a test' |
@orbea
Thanks, heaps for that, I cannot believe that double quotes around the array were causing the grief! I am now confused, normally it a good practice to reference a variable like "${env}", so this does not hold true for an array? Please enlighten Thank you |
It depends your goal: it you don't wish your variable space-splitted, use qutes: "$env", otherwise don't: $env or ${env}
|
Aah! Got it, Thanks @Nevem Teve!
|
This is an extremely bad example.
You should in fact double quote if it was actually an array. As it is a simple string the ${[@]} expander is not being used correctly hence the output issues. Here is a quick demo using orbea's example: Code:
foo='this is a test' |
It would be nice to see an mcve.
https://stackoverflow.com/help/mcve |
Quote:
|
In this case, it probably makes more sense to use a while read loop (and drop useless use of cat and grep):
Code:
awk '/NXT/ {print $1}' "$LOGFILE" | while read i |
All times are GMT -5. The time now is 03:04 PM. |