LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash script detect if ip address is correct (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-detect-if-ip-address-is-correct-4175717658/)

tonj 10-11-2022 12:41 PM

bash script detect if ip address is correct
 
I'm not very good at bash scripts and I have searched google but can't find a script example that suits my needs.
I run a linux server and I test my WAN address with curl ifconfig.me.
if the system is working correctly it gives me ip address (example) 11.22.33.44.
but if its not working it gives me address (example) 99.88.77.66.
I'm looking for a script that will run curl ifconfig.me and if it returns 11.22.33.44 it will no nothing and if it returns 99.88.77.66 it will execute a command.
thanks for any pointers

boughtonp 10-11-2022 12:53 PM


 
You're asking how do to an if statement in Bash?

You will find plenty of examples by searching (e.g.) "bash if statement" or you can go direct to the Conditional Constructs section of the Bash Manual.

(It's probably also covered in the Bash FAQ.)


michaelk 10-11-2022 12:56 PM

Something like:
Code:

#!/bin/bash
ip=$(curl ifconfig.me)
if [[ "$ip" == "99.88.77.66" ]]; then
  some_command
fi

I feel generous today and sometimes people just need somewhere to start...

tonj 10-11-2022 05:33 PM

thankyou michaelk that script example has been a big help


All times are GMT -5. The time now is 06:26 AM.