[SOLVED] BASH Script. Result of command input into variable
Linux - NewbieThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.