The key to matching is a regular expression. Tools can consider grep or sed.
plan 1:
With grep:
ifconfig | grep -Eo 'inet (addr:)? ([0-9] * \.) {3} [0-9] *' | grep -Eo '([0-9] * \.) {3} [0-9] *' | grep -v '127.0.0.1'
Scenario 2:
With the help of sed:
ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'
Note: The -E option of sed depends on the specific sed version. If -E is invalid, use -r instead.
In addition, you can specify the interface to be displayed through ifconfig.
ifconfig eth0 | ...
In my specific practice, I used scheme 2 and specified the interface.
SERVER_IP=$(/sbin/ifconfig eth0 | sed -nr 's/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p')
See also: SO