Ubuntu – need help creating an if statment that will work for the purpose in .bashrc

bashbashrccommand line

I have edited my .bashrc to look like this with some help from others on this forum … Thanks guys for the help on the weather segment of this script…enter image description here

An issue I have ran into is the code for the external ip address. If for some reason the site I use to check the external IP doesn't respond .. it locks up my bash at that point until it gets the information.

What I would like to do is change it so that I can have it so if it doesn't respond in a second or 500ms .. then to just move on so it doesn't hang. I hope that makes sense .. here is the code I'm using for it.

localnet()
{
declare -a INETARRAY
INETARRAY=( `/sbin/ifconfig | awk /'inet addr/ {print $2}' && /sbin/ifconfig | awk /'Bcast/ {print $3}'`)
echo -ne ${INETARRAY[@]} "ExtIP:" ; myip
}
myip ()
{
lynx -dump -hiddenlinks=ignore -nolist http://checkip.dyndns.org:8245/ | grep "Current IP Address" | cut -d":" -f2 | cut -d" " -f2
}

Basically I need an if statement that would do something like this:

if the site doesn't give whats expected withing a configurable amount of time load "line 1" else load "line 2"

Line 1 would contain the code without the external IP request
Line 2 would contain the code with the external IP request.

Now .. I'm not actually sure what is causing the hang .. I don't know if the site is not responding .. or if it is busy and taking longer to respond or if its getting information but not whats expected … it happens sporadically and if it hangs on me I can close the shell and open it and usually it will get the information .. I thought about ping but … again .. not sure if the site would still respond to the ping but not produce what the script is looking for and still hang.

I just don't know how to set up the statement correctly to make this happen or something similar

Thanks in advanced.

I tried the line suggested by glenn jackman

curl --silent --output - --connect-timeout 2 --max-time 3 http://checkip.dyndns.org:8245/ | grep "Current IP Address" | cut -d":" -f2 | cut -d"" -f2

It did get the IP address but the resulted in this:

xxx.xxx.xxx.xxx</body></html>

I will have to probably adjust the second cut command .. lol .. guess I need to learn cut 😀

If i use

curl --silent --output - --connect-timeout 2 --max-time 3 http://checkip.dyndns.org:8245/ | grep "Current IP Address"

I get the output of:

<html><head><title>Current IP Check</title></head><body>Current IP Address: xxx.xxx.xxx.xxx</body></html>

If I switch back to the lynx line instead of curl and use the -read_timeout=1 -connect-timeout=1 this does keep the shell from hanging if it has connections issues but .. it produces errors which was why I was hoping to make some sort of if statement

Network: addr:192.168.1.20 addr:127.0.0.1 Bcast:192.168.1.255 ExtIP:
Looking up checkip.dyndns.org:8245
Making HTTP connection to checkip.dyndns.org:8245
Alert!: Connection failed (too many retries).
Alert!: Unable to connect to remote host.

lynx: Can't access startfile http://checkip.dyndns.org:8245/

Best Answer

What seem to be a working solution is using the curl command suggested by glenn jackman and the address suggested by Terrance. I haven't run into the hang yet so I don't know if it will through any errors but it seems to work well.. the new line is now

curl --silent --output - --connect-timeout 1 --max-time 2 http://icanhazip.com/ 

which seems to be working .. I have launched the terminal 10 times without a hang or an error. I think you guys did it again... Man I wish where was something like this forum for Windows... lol you go on their forums and ask a question .. you get a non related 'form letter response' and then nothing.

You guys are great .. I think this has now got the terminal shell just the way I want it and no hanging .. lol I disabled the external IP thing for a while because of the annoyance but now I can have it back again :D

If anyone decides to use the .bashrc I linked lol its slightly plagiarized but I don't remember the original person I got it from when the script first started out but I have been playing with it ever since.. it was like in 2010 or something.

The file also includes a link to .bashrcc that you can create where you can add commands and aliases without having to put them in the main file. This was done because I sync my bashrc across several computers and there are times where I want a special alias for one computer but I don't it available on all the PC's that are synced to the file. The .bashrcc will allow me to add it for one user only. Also remember that you will have to change the weather function with your zip code or you will be viewing my area's temps :D

Related Question