다음에 확인할 때 IP 주소가 변경되지 않은 경우 bash 스크립트 실행을 중지하는 방법은 무엇입니까?

다음에 확인할 때 IP 주소가 변경되지 않은 경우 bash 스크립트 실행을 중지하는 방법은 무엇입니까?

나는 헤더를 사용하려고 최선을 다했지만 bash 스크립트에서 수행해야 하는 작업에 대한 간단한 예를 제공하는 것이 더 좋을 것입니다.

curl ident.me
#this one checks my current public IP address

curl [a post request]
#sending a curl post request

curl ident.me
#again, it checks my current public IP address

curl [a different post request]
#it will send the curl post request ONLY if the IP address we got with the previous curl request is DIFFERENT than the one we got when we previously used the same command to check for the current IP address. If it's not different, it should stop/pause the script.

어떤 아이디어가 있나요? 이 질문을 기술적이고 정확하게 공식화하는 것이 어렵기 때문에 Google은 아무런 도움도 제공하지 않았습니다.

답변1

각 컬의 결과를 변수에 저장한 다음 각 컬의 결과를 비교하여 if 문에서 서로 일치하는지 확인합니다.

a=$(curl ident.me)
curl [a post request]
b=$(curl ident.me)

if [ "$a" != "$b" ]
then
  echo "do not match" && ...
else 
  exit
fi

답변2

함수를 사용하면 작업이 더 쉬워집니다.

die() { printf>&2 '%s\n' "$@"; exit 1; }

ip= prev_ip=
retrieve_public_ip() {
  ip=$(curl --no-progress-meter https://ident.me) && [ -n "$ip" ] ||
    die "Can't determine my public IP address"

  [ "$ip" != "$prev_ip" ] ||
    die "My public IP address has not changed. Aborting"

  prev_ip=$ip
}

retrieve_public_ip
curl [a post request]

retrieve_public_ip
curl [a different post request]

# and so on.

관련 정보