LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-11-2022, 12:41 PM   #1
tonj
Member
 
Registered: Sep 2008
Posts: 546

Rep: Reputation: 37
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
 
Old 10-11-2022, 12:53 PM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,612

Rep: Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553

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.)


Last edited by boughtonp; 10-11-2022 at 12:58 PM.
 
Old 10-11-2022, 12:56 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,729

Rep: Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919
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...

Last edited by michaelk; 10-11-2022 at 01:37 PM.
 
1 members found this post helpful.
Old 10-11-2022, 05:33 PM   #4
tonj
Member
 
Registered: Sep 2008
Posts: 546

Original Poster
Rep: Reputation: 37
thankyou michaelk that script example has been a big help
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get the correct full file path from a remote linux machine correct superwavelet Linux - Newbie 7 05-19-2018 06:28 PM
Rows in lists on my Debian are correct/black/correct/black... eikhorsholm Linux - Desktop 1 10-01-2009 05:24 PM
Winetools doesn't detect the correct version of wine doraimom Linux - Software 2 05-19-2007 08:27 PM
how to get ip address, broadcast address, mac address of a machine sumeshstar Programming 2 03-12-2005 04:33 AM
bash script to detect scsi address at boot ewto Programming 2 10-20-2003 02:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration