source: main/trunk/greenstone3/bin/script/IPv4.sh@ 32664

Last change on this file since 32664 was 32664, checked in by ak19, 5 years ago

On Kathy's machine, there is no eth0, as the default ethernet device has a different name, so doing ifconfig eth0 to get IPv4 address doesn't work. But on her machine, the older code that did just ifconfig to get the IPv4 does work, so using this method as a fallback.

  • Property svn:executable set to *
File size: 2.1 KB
Line 
1#!/bin/bash
2
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
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
15#echo `ifconfig eth0 | grep "inet " | grep -v 127.0.0.1`|cut -d' ' -f 2|cut -d':' -f 2
16
17tmp=`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
19if [[ $tmp = *"Device not found"* ]] || [[ -z "${tmp// }" ]]; then
20 # eth0 is not found on Kathy's machine where the default ethernet device has a different name
21 # But ifconfig on its own works there, so try that next
22 tmp=`ifconfig 2>&1 | grep "inet " | grep -v 127.0.0.1`
23 if [[ $tmp = *"Device not found"* ]] || [[ -z "${tmp// }" ]]; then
24 echo "|"
25 fi
26fi
27
28# grab just the portion of the line we want
29tmp=`echo $tmp|cut -d' ' -f 2|cut -d':' -f 2`
30
31# if requested to format the IPv4 for the solr.xml tomcat context file
32if [ "$1" == "-format-for-tomcat-context" ]; then
33
34 #http://stackoverflow.com/questions/13210880/replace-one-substring-for-another-string-in-shell-script
35 #${original_string//searchterm/$string_to_replace_searchterm_with}
36 replace="."
37 replacement="\."
38
39 tmp=${tmp//$replace/$replacement}
40
41 # next, if the tmp variable is not the empty string, prefix the | operator
42 # http://unix.stackexchange.com/questions/146942/how-can-i-test-if-a-variable-is-empty-or-contains-only-spaces
43 # Can just return | if $tmp is empty, as that doesn't really matter. Will be consistent with windows
44 if [[ ! -z "${tmp// }" ]]; then
45 tmp="|$tmp"
46 else
47 tmp="|"
48 fi
49
50fi
51
52echo $tmp
53
Note: See TracBrowser for help on using the repository browser.