I tried searching for answers but didn't find any.
When I grep a file results read
4.2.2.2
4.4.4.2
4.5.6.7
But I just want to select each result individually. For Example I want to be able to say
variable1="first grep result"
variable2="second grep result"
variable3="third grep result"
It would help a lot to analyse Your problem if:
1: You gave an actual sample of the source, ie the "file" content
2. What Your grep or other command sequence looks like right now
3. You give an example of expected output, that is for example, is this variable assignment part of a some script to be used elsewhere?
Thanks that works but it doesn't like it if the file is empty. I guess I can do my standard grep nameservers | wc -l to check if there is an entry first.
Thanks, looks like it works. Can you explain how this works? I just like to understand it and use it in the future. Also does this work if need to run a command and grab all the output from it....for example running the command /usr/sbin/networksetup -listallnetworkservices on a leopard machine I get all the network services. How would I use this command to only print anyone that contains with "Ethernet".
Since I'm used to using grep I run /usr/sbin/network -listallnetworkservices |grep Ethernet, but I get 3 responses and I need to select each on individually. Can you help?
This will give you the array var. You can access the values in var via
echo ${var[0]} ${var[1]} ${var[2]}
$(...) means run the command between the braces and give me the result.
var=( $(...) ) means run the command between the braces and save the result in an array called var. So should be able to capture the output from any command this way.
For an explanation of [[ -s ... ]] see
man test
[[ ... ]] && ... || ...
is a short form of if ... then ... else.
If you have other questions you should open a new post a give an exact description of your problem and your goal.