LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-05-2011, 08:40 PM   #1
bcyork
LQ Newbie
 
Registered: Dec 2010
Location: Chicago, IL
Distribution: Linux Mint 11 / Debian 6
Posts: 29

Rep: Reputation: 1
BASH Script. Result of command input into variable


Hi, I have been trying to figure this out and also new to bash scripting as well. How can I put the result of a command into a variable such as this.

CURRENT='wget -O - -q icanhazip.com'

So if I would run

echo $CURRENT

I would get an IP address?

Right now echo returns the command. I can't seem to figure out how exactly to do this. In the end I need two variables. One an IP address returned as my current external IP and the second a variable read from a file that would be an IP address as well.

Thanks!
 
Old 12-05-2011, 08:43 PM   #2
corp769
Guru
 
Registered: Apr 2005
Posts: 5,807

Rep: Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996
Here is what you need:
Code:
#!/bin/bash
var1=$(wget -O - -q icanhazip.com)
echo $var1
You need to tell it to execute the command, instead of trying to display it. You can alternatively use backticks, as such:
Code:
`wget -O - -q icanhazip.com`
Cheers,

Josh
 
1 members found this post helpful.
Old 12-05-2011, 08:58 PM   #3
bcyork
LQ Newbie
 
Registered: Dec 2010
Location: Chicago, IL
Distribution: Linux Mint 11 / Debian 6
Posts: 29

Original Poster
Rep: Reputation: 1
Thanks Josh, did the trick!

-Brian
 
Old 12-05-2011, 10:45 PM   #4
corp769
Guru
 
Registered: Apr 2005
Posts: 5,807

Rep: Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996
No problem! Please mark your thread as solved using the thread tools located at the top of the page, thanks!

Cheers,

Josh
 
1 members found this post helpful.
Old 12-06-2011, 12:57 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Note that $(..) is highly recommended over `..`.

A while back I wrote up a script/function that polls a random site for your external ip address, and keeps trying until it gets a successful hit. You can easily edit the list if you discover new ones or one goes bad. I currently have seven working addresses.

Code:
#!/bin/bash

get_current_ip() {
	local -a ipsite
	local -i xc num i

	ipsite=(
		"http://automation.whatismyip.com/n09230945.asp"
		"http://www.showmyip.com/simple/"
		"http://cfaj.freeshell.org/ipaddr.cgi"
		"https://secure.informaction.com/ipecho/"
		"http://icanhazip.com/"
		"http://wooledge.org/myip.cgi"
		"http://ifconfig.me/ip"
	       )

	xc=1
	num=${#ipsite[@]}

	until (( xc == 0 )) ; do
		(( i = RANDOM % num ))
		ip=$( wget -t 1 -T 5 -q -O- "${ipsite[i]}" )
		xc=$?
	done

	#some of the sites may output a newline or other junk, so remove any cruft.
	printf "%s" "${ip//[^0-9.]}"
}

get_current_ip

exit 0
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH Script will not get use result of command when run as cron job dahweeds Linux - Newbie 3 07-01-2011 07:01 PM
Execute command with spaces from variable in bash script klo_2k Linux - Newbie 4 04-13-2008 02:59 AM
bash script: how to assign an output (not the result) to a variable? Singing Banzo Programming 8 10-01-2006 06:29 PM
cannot export result from awk into a variable in a bash script Emmanuel_uk Linux - Newbie 4 03-07-2005 01:54 AM
Bash script; command and args in variable. magjo813 Programming 2 02-16-2004 09:22 AM


All times are GMT -5. The time now is 12:23 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration