Trim a new line

Okay, I am trying to make a bash script to get a certain domains IP address (my home ip). My home is on a DHCP lease from my ISP, so I cannot always trust the IP address to remain constant.

This is what I have so far for it:

alias ip-home="ping -c 1 example.com | grep 'PING' | cut -d'(' -f2 | cut -d')' -f1";

But that outputs a line break afterwords. What I would really like is something that matches the output of:

curl -q www.whatismyip.com/automation/n09230945.asp

which is...

dev:~ tnanek$ curl -q www.whatismyip.com/automation/n09230945.asp
0.0.0.0dev:~ tnanek$

The IP was altered above, as well as the domain in the first statement.

I am on Snow Leopard 10.6.4, though it is built off of a Unix core.

For background on what I am trying to do ultumatly...

I am a Drupal developer, and I would like my .bashrc file to set my host entry appropriately to use a specific domain (not one set via my router) to point to my home computer. I will ultimatly be adding that, but meanwhile, now I'm only getting the ip address in a usable form.

Try:

host example.com | awk 'NR==1{printf $NF}'

Scrutinizer's is better, but tr -d '\n' is often handy to know.

# ping -c 1 google.com | grep 'PING' | cut -d'(' -f2 | cut -d')' -f1 | tr -d '\n'
74.125.65.104# 

When entering that in the command line it works just fine; however when putting that in a bash script it does not. Here is an example of how I'm getting it (I know echo adds a line break after, but thats not the issue).

What I want is just the IP address, no line breaks before or after, in a variable that I can then insert into my local host file, after some tweaking of adding my desired access domain to it.

Here's my command line:

dev:~ tnanek$ ip-home
hostdev:~ tnanek$

And here's the relevant portion of the .bashrc file:

homeip="host example.com | awk 'NR==1{printf $NF}'";
alias ip-home="echo $homeip";

---------- Post updated at 11:45 AM ---------- Previous update was at 11:35 AM ----------

And same with your method fubaya; works fine directly on the command line, but doesn't work in .bashrc. Here are my modified examples:

My command line:

dev:~ tnanek$ ip-home
dev:~ tnanek$ 

My .bashrc:

homeip="ping -c 1 example.com | grep 'PING' | cut -d'(' -f2 | cut -d')' -f1 | tr -d '\n'";
alias ip-home="echo $homeip";

---------- Post updated at 12:07 PM ---------- Previous update was at 11:45 AM ----------

Got it using backquotes as per

http://ubuntuforums.org/showthread.php?t=330936

Working version of fubaya's method:

homeip=`ping -c 1 example.com | grep 'PING' | cut -d'(' -f2 | cut -d')' -f1 | tr -d '\n'`;
alias ip-home="echo $homeip";

And of Scrutinizer's method:

homeip=`host example.com | awk 'NR==1{printf $NF}'`;
alias ip-home="echo $homeip";

Alternatively you could use a function instead of an alias:

function ip-home 
{
  host example.com | awk 'NR==1{printf $NF}'
}

Command substitution trims trailing newlines, so I don't even understand how you're having a problem with a trailing newline(s) in a variable whose value is assigned from command substitution.

In the following shell session, while the first printf statement generates 5 trailing newlines (verified with od), not a single one is assigned to the variable $v (again, verified with od). If your shell does not work this way, then it's an egregious violation of the posix standard. It's more likely that you are doing something wrong when you are accessing/printing the variable's value:

$ printf 'a\n\n\n\n\n'
a




$ printf 'a\n\n\n\n\n' | od -c
0000000    a  \n  \n  \n  \n  \n                                        
0000006
$ v=$(printf 'a\n\n\n\n\n')
$ echo $v
a
$ printf %s "$v" | od -c
0000000    a                                                            
0000001
$ printf %s "$v"        
a$<my shell prompt here without preceding newline>

Regards,
Alister