1

I have two variables, that I need to concatenate together to form a server name with its fqdn. Because of permission problems, I can only access certain files to get information. Maybe below isn't the best way to get the variables in ruby but they work. If there is a more efficient way, I'm all ears. Anyway, below are the variables. Since I can't get the server/fqdn from one spot I had to pull from two different resources, a file and from the hostname command. I need to get them to be server.fqdn.com:

$server=`grep server /etc/config_file | awk '{print $2}'`
$domain=`hostname -d`

Again, I'm trying to get them together to make server.fqdn.com

Thanks!!

1
  • Use the {} button to properly format code snippets. Commented Sep 4, 2014 at 17:15

3 Answers 3

8

It's not a good idea to use global variables like this in Ruby. $ indicates a global, not just any ordinary variables as is the case in PHP or Perl.

To concatenate two strings:

fqdn = server + domain

Or to join with a dot:

fqdn = [ server, domain ].join('.')

Keep in mind that the grep and awk operations can be internalized to Ruby to avoid the mess of having to shell out twice.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! I'm porting this script over from bash and already had those variables set and they still worked in ruby. I need to change how I call my variables in the script to a more ruby like. This helps with that.
What would be the most efficient way in ruby, using grep/awk, to pull a string from a file to create a variable?
Ruby has similar functions to this, like File.readlines('/etc/config_file').grep(/server/).first.split(/\s+/)[1] might be close to what you want.
2

1) I know zero about shell scripting, but why can't you just write:

awk '/server/ {print $2}' /etc/config_file

?? Why do you need grep?

2) Your program won't work on Mac OSX because its hostname command doesn't have a -d flag (at least in version 10.6.8). Does the following:

require 'socket'
puts Socket.gethostname 

get you the info you need?

If so, you can do the whole thing in ruby. Isn't that the reason you use ruby in the first place, i.e. so you don't have to scratch around in the dirt with the shell scripting chickens?

require 'socket'

domain = Socket.gethostname
server_name = nil

IO.foreach('/etc/config_file') do |line|
  if line.match /server/
    _, server_name = line.split(" ", 3)
    break
  end
end

something = "#{server_name}.#{domain}"
puts something

4 Comments

+1 for "scratch around in the dirt...". (Actually, +1 for the answer, but that expression is priceless.)
Thank you for your answer. Yes I could just do the awk command that way. I'm coming from bash to ruby. This was a bash script that I've ported over to ruby. I'm still learning and switching my mindset from bash to ruby. Thank you very much for your input, it was very useful!
@TonyD, Does Socket.gethostname provide the same info has hostname -d on your system? If it doesn't, could you post the differences at the bottom of your questions(if needed substitute in alternate names to hide your personal info)?
Socket.gethostname gives me the server.test.com. hostname -d gives me test.com
0

Also try using the binary left shift operator! (AKA the less than sign)

var = "Server"
var2 = "Domain"
var << var2

Its the same as saying:

var = "Server"
var2 = "Domain"
var3 = var.concat(var2)

Hope this gives you more options!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.