Reputation: 139
Full disclosure: I don't really know Ruby. I'm mostly faking it.
I have a script I want to use to gather inventory in Casper, which I'm using to manage a bunch of Macs. I'm attempting to pass a variable into a shell command with %x
. Problem is, Ruby is treating the variable as a comment instead. Here is the relevant code:
def get_host
host=%x(/usr/sbin/dsconfigad -show | /usr/bin/awk '/Computer Account/ {print $4}').chomp
raise Error, "this machine must not be bound to AD.\n try again." if host == nil
end
def get_ou
host = get_host
dsout = %x(/usr/bin/dscl /Search -read /Computers/#{host}).to_a
ou = dsout.select {|item| item =~ /OU=/}.to_s.split(",")[1].to_s.gsub(/OU=/, '').chomp
end
I tried using back ticks instead of %x
, but got the same result. The command should return a bunch of information about the host it's run on, but instead it returns the result of dscl /Search -read /Computers
, which is always name: dsRecTypeStandard:Computers
.
How can I accomplish what I want to do?
Upvotes: 1
Views: 228
Reputation: 8293
The problem is here. The Ruby always returns the last expression in a method.
def get_host
host=%x(/usr/sbin/dsconfigad -show | /usr/bin/awk '/Computer Account/ {print $4}').chomp
raise Error, "this machine must not be bound to AD.\n try again." if host == nil
end
In this case, the last expression is:
raise Error, "this machine must not be bound to AD.\n try again." if host == nil
It will return the return value of raise
(which don't gonna happen actually) if host == nil
or will return nil
if host != nil
. So your method will never return something other than nil
. Replace it by:
def get_host
host=%x(/usr/sbin/dsconfigad -show | /usr/bin/awk '/Computer Account/ {print $4}').chomp
raise Error, "this machine must not be bound to AD.\n try again." if host == nil
host
end
Upvotes: 5