admin管理员组文章数量:1122846
I have to read an IP from def.cfg file that is set in a line named TERMINAL_IP (TERMINAL_IP = "192.168.0.140"), then do an append to the end of the def.cfg file with the last digit read that is not a 0 number, in this example have a 0 in the last digit so the value to show must be 4 and if the last number is .100 the value to show will be 1 but if the last number is .145 the value to show must be 5.
I'm using the following script to do that:
#!/bin/bash
CONFIG_FILE="def.cfg"
TERMINAL_IP=$(grep -oP '(?<=TERMINAL_IP = ).*' "$CONFIG_FILE")
if [[ -z "$TERMINAL_IP" ]]; then
echo "TERMINAL_IP not found in the configuration file."
exit 1
fi
LAST_DIGIT=$(echo "$TERMINAL_IP" | grep -oE '[0-9]+' | tail -n 1)
if [[ "$LAST_DIGIT" == "0" ]]; then
# If the last digit is 0, take the second-to-last digit
LAST_DIGIT=$(echo "$TERMINAL_IP" | grep -oE '[0-9]+' | tail -n 2 | head -n 1)
fi
echo "LSTD = \"${LAST_DIGIT}\"" >> "$CONFIG_FILE"
The problem is that instead append the number 4, it is appending the number 140, for some reason it does not isolate the number 4.
Any ideas to solve this?
I have to read an IP from def.cfg file that is set in a line named TERMINAL_IP (TERMINAL_IP = "192.168.0.140"), then do an append to the end of the def.cfg file with the last digit read that is not a 0 number, in this example have a 0 in the last digit so the value to show must be 4 and if the last number is .100 the value to show will be 1 but if the last number is .145 the value to show must be 5.
I'm using the following script to do that:
#!/bin/bash
CONFIG_FILE="def.cfg"
TERMINAL_IP=$(grep -oP '(?<=TERMINAL_IP = ).*' "$CONFIG_FILE")
if [[ -z "$TERMINAL_IP" ]]; then
echo "TERMINAL_IP not found in the configuration file."
exit 1
fi
LAST_DIGIT=$(echo "$TERMINAL_IP" | grep -oE '[0-9]+' | tail -n 1)
if [[ "$LAST_DIGIT" == "0" ]]; then
# If the last digit is 0, take the second-to-last digit
LAST_DIGIT=$(echo "$TERMINAL_IP" | grep -oE '[0-9]+' | tail -n 2 | head -n 1)
fi
echo "LSTD = \"${LAST_DIGIT}\"" >> "$CONFIG_FILE"
The problem is that instead append the number 4, it is appending the number 140, for some reason it does not isolate the number 4.
Any ideas to solve this?
Share Improve this question edited yesterday Mario asked yesterday MarioMario 135 bronze badges 5 |2 Answers
Reset to default 1With GNU bash and its Parameter Expansion:
TERMINAL_IP="192.168.0.140"
TERMINAL_IP="${TERMINAL_IP/%0/}" # remove an existing trailing 0
echo "${TERMINAL_IP: -1:1}" # extract last digit
Output:
4
Space before -1
is important.
If you're already using grep -P
, you can use it to extract the last non-zero digit, too:
last_digit=$(grep -Po '(?<=TERMINAL_IP = ).*' "$CONFIG_FILE" \
| grep -Po '[1-9](?=0*$)')
Note that $last_digit
will be empty if the IP ends in .0
.
Also note that I didn't use upper case letters for a non-environment variable.
本文标签: linuxIsolate last non 0 digit from IP addressStack Overflow
版权声明:本文标题:linux - Isolate last non 0 digit from IP address - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736281553a1926355.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
tail -n 1
outputs the last line,tail -c 1
would output the last character instead. – choroba Commented yesterday192.168.0.140
?140
and0
or0
and4
? – Cyrus Commented yesterday+
from yourgrep
commands? – Cyrus Commented yesterday111.222.333.000
?3
or.
or an error message or something else? – Ed Morton Commented 2 hours ago