I want to edit the bashrc file to have a simple function called \"myip\" to run. As you might guess, the function myip prints only my internal IP address of my machine.
This code outputs IP addresses for all network connections (except loopback) and is portable between most OS X and Linux versions.
It's particularly useful for scripts that run on machines where:
The script is:
/sbin/ifconfig -a | awk '/(cast)/ {print $2}' | cut -d: -f2
This can be assigned to a variable in a script like this:
myip=$(/sbin/ifconfig -a | awk '/(cast)/ {print $2}' | cut -d: -f2)
Scripts can handle possible multiple addresses by using a loop to process the results, as so:
if [[ -n $myip ]]; then
count=0
for i in $myip; do
myips[count]=$i # Or process as desired
((++count))
done
numIPaddresses=$count # Optional parameter, if wanted
fi
Notes: