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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
12-05-2011, 08:40 PM
|
#1
|
|
LQ Newbie
Registered: Dec 2010
Location: Chicago, IL
Distribution: Linux Mint 11 / Debian 6
Posts: 20
Rep:
|
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!
|
|
|
|
12-05-2011, 08:43 PM
|
#2
|
|
Guru
Registered: Apr 2005
Location: /dev/null
Distribution: technixOS
Posts: 5,723
|
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.
|
12-05-2011, 08:58 PM
|
#3
|
|
LQ Newbie
Registered: Dec 2010
Location: Chicago, IL
Distribution: Linux Mint 11 / Debian 6
Posts: 20
Original Poster
Rep:
|
Thanks Josh, did the trick!
-Brian
|
|
|
|
12-05-2011, 10:45 PM
|
#4
|
|
Guru
Registered: Apr 2005
Location: /dev/null
Distribution: technixOS
Posts: 5,723
|
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.
|
12-06-2011, 12:57 PM
|
#5
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,577
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:59 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|