[30036] | 1 | #!/bin/bash
|
---|
| 2 |
|
---|
[30038] | 3 | # This script will echo the IPv4 of this unix machine. E.g. 100.200.300.45
|
---|
| 4 | # If passed -format-for-tomcat-context, it will echo the same with a pipe
|
---|
| 5 | # symbol up front and all the . escaped with a "\". E.g. "|100\.200\.300\.45"
|
---|
| 6 |
|
---|
[30036] | 7 | #http://www.wikihow.com/Find-Your-IP-Address-on-a-Mac#Finding_Your_Internal_IP_Using_the_Terminal_sub
|
---|
| 8 | #http://stackoverflow.com/questions/1469849/how-to-split-one-string-into-multiple-strings-in-bash-shell
|
---|
| 9 | #http://www.linuxforums.org/forum/red-hat-fedora-linux/193076-ifconfig-doesnt-display-ipv4-address.html
|
---|
| 10 |
|
---|
| 11 |
|
---|
| 12 | # The following echoes the IPv4, e.g. 100.200.300.45
|
---|
| 13 | # But need to still replace the . with \.
|
---|
| 14 |
|
---|
[32463] | 15 | #echo `ifconfig eth0 | grep "inet " | grep -v 127.0.0.1`|cut -d' ' -f 2|cut -d':' -f 2
|
---|
[30036] | 16 |
|
---|
[32463] | 17 | tmp=`ifconfig eth0 2>&1 | grep "inet " | grep -v 127.0.0.1`
|
---|
| 18 | # As below: if $tmp is empty, can just return |, as that doesn't really matter. Will be consistent with windows
|
---|
| 19 | if [[ $tmp = *"Device not found"* ]] || [[ -z "${tmp// }" ]]; then
|
---|
| 20 | echo "|"
|
---|
| 21 | fi
|
---|
[30036] | 22 |
|
---|
[32463] | 23 | # grab just the portion of the line we want
|
---|
[30036] | 24 | tmp=`echo $tmp|cut -d' ' -f 2|cut -d':' -f 2`
|
---|
| 25 |
|
---|
[30038] | 26 | # if requested to format the IPv4 for the solr.xml tomcat context file
|
---|
| 27 | if [ "$1" == "-format-for-tomcat-context" ]; then
|
---|
[30036] | 28 |
|
---|
[30038] | 29 | #http://stackoverflow.com/questions/13210880/replace-one-substring-for-another-string-in-shell-script
|
---|
| 30 | #${original_string//searchterm/$string_to_replace_searchterm_with}
|
---|
| 31 | replace="."
|
---|
| 32 | replacement="\."
|
---|
| 33 |
|
---|
| 34 | tmp=${tmp//$replace/$replacement}
|
---|
| 35 |
|
---|
| 36 | # next, if the tmp variable is not the empty string, prefix the | operator
|
---|
| 37 | # http://unix.stackexchange.com/questions/146942/how-can-i-test-if-a-variable-is-empty-or-contains-only-spaces
|
---|
| 38 | # Can just return | if $tmp is empty, as that doesn't really matter. Will be consistent with windows
|
---|
| 39 | if [[ ! -z "${tmp// }" ]]; then
|
---|
[30036] | 40 | tmp="|$tmp"
|
---|
[30038] | 41 | else
|
---|
[30036] | 42 | tmp="|"
|
---|
[30038] | 43 | fi
|
---|
| 44 |
|
---|
[30036] | 45 | fi
|
---|
| 46 |
|
---|
[30038] | 47 | echo $tmp
|
---|
[30036] | 48 |
|
---|