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
  • tail -n 1 outputs the last line, tail -c 1 would output the last character instead. – choroba Commented yesterday
  • What's last and second last digit of 192.168.0.140? 140 and 0 or 0 and 4? – Cyrus Commented yesterday
  • in .140, 4 is the last non 0 digit must to show. with tail -c 1 don't show nothing. – Mario Commented yesterday
  • Remove + from your grep commands? – Cyrus Commented yesterday
  • What should the output be if the value is 111.222.333.000? 3 or . or an error message or something else? – Ed Morton Commented 2 hours ago
Add a comment  | 

2 Answers 2

Reset to default 1

With 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