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?
The problem is here. The Ruby always returns the last expression in a method.
In this case, the last expression is:
It will return the return value of
raise(which don’t gonna happen actually) ifhost == nilor will returnnilifhost != nil. So your method will never return something other thannil. Replace it by: