LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-26-2015, 04:38 AM   #1
slockna
LQ Newbie
 
Registered: Jul 2015
Location: Sweden
Distribution: Slackware, zenwalk, debian
Posts: 28

Rep: Reputation: Disabled
need help matching string variables in a bash script


Hi, can't figure out how to best evaluate if a variable is an IP address or not.

the below example does not work, can someone help ?

Code:
MYIP=$(/usr/bin/wget -qO - http://ipecho.net/plain)
echo "$MYIP" |od -x
if [ $MYIP == [0-9]* ]; 
        then        TEXTEN="Connected, IP : $MYIP"
        else

            TEXTEN="No Internet connection"
        fi


echo "$TEXTEN"
 
Old 10-26-2015, 04:49 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
use the regexp match feature of bash: http://www.linuxjournal.com/content/...ar-expressions
 
1 members found this post helpful.
Old 10-26-2015, 05:00 AM   #3
slockna
LQ Newbie
 
Registered: Jul 2015
Location: Sweden
Distribution: Slackware, zenwalk, debian
Posts: 28

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
use the regexp match feature of bash: http://www.linuxjournal.com/content/...ar-expressions
i am trying, but i do not get a hang of the syntax; i am thinking i only need to test if the first character is a digit ^[0-9] then it is a match...

Code:
pi@plutten ~ $ cat test.bash
MYIP=$(/usr/bin/wget -qO - http://ipecho.net/plain)
echo "$MYIP" |od -x
if [ $MYIP =~ ^[0-9] ];
then        TEXTEN="Connected, IP : $MYIP"
    else
            TEXTEN="No internet connection"
    fi
echo "<p> $TEXTEN </p>"

pi@plutten ~ $ bash -x test.bash
++ /usr/bin/wget -qO - http://ipecho.net/plain
+ MYIP=95.192xxxxxx
+ echo 95.192xxxxxx
+ od -x
0000000 3539 312e 3239 32a
0000017
+ '[' 95.192xxxxxx '=~' '^[0-9]' ']'
test.bash: line 3: [: =~: binary operator expected
+ TEXTEN='No internet connection'
+ echo '<p> No internet connection </p>'
<p> No internet connection </p>

Last edited by slockna; 10-26-2015 at 05:05 AM.
 
Old 10-26-2015, 05:10 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
you can use an online regexp checker to understand how regexp works: http://www.myezapp.com/apps/dev/regexp/show.ws
You will see ^[0-9] will match only a single number, nothing else. To make it properly you need to use double brackets [[ ]] as it was used on the link I posted.
 
1 members found this post helpful.
Old 10-26-2015, 05:34 AM   #5
slockna
LQ Newbie
 
Registered: Jul 2015
Location: Sweden
Distribution: Slackware, zenwalk, debian
Posts: 28

Original Poster
Rep: Reputation: Disabled
Smile

Thanks,

i still haven't a single clue what i did, but it works now !

Code:
MYIP=$(/usr/bin/wget -qO - http://ipecho.net/plain)

if [[ $MYIP =~ ^[0-9].* ]];
then        TEXTEN="Connected, IP : $MYIP"
    else
            TEXTEN="No internet connection"
    fi
echo "<p> $TEXTEN </p>"
 
Old 10-26-2015, 05:47 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
have you checked it on the link I gave? That is: one digit and after that any number of anything - from the beginning of the line.
a bit better: https://regex101.com/r/sZ9lF4/2
 
Old 10-26-2015, 05:48 AM   #7
slockna
LQ Newbie
 
Registered: Jul 2015
Location: Sweden
Distribution: Slackware, zenwalk, debian
Posts: 28

Original Poster
Rep: Reputation: Disabled
I think i get it now :P

background info: this is part of a web page ( yes , i am using sudoed bash scripts for CGI for system management ) don't try this at home kids ! unless you know what you're doing, which would per se keep you from doing it in the first place.

Obviously i don't know what i'm doing, which is why i am doing it....
 
  


Reply

Tags
bash scripting



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script for searching matching content from two multirow variables bzzika Linux - General 1 04-11-2012 04:35 AM
Bash Script - Passing command string between variables Devcon Programming 13 01-10-2011 10:13 AM
Bash Script: parse active process stderr, strip, dump into variables, use variables TimeFade Programming 1 02-13-2010 06:09 AM
string matching in bash 2.0.5 texaganian Linux - Newbie 4 11-30-2009 09:34 AM
bash string matching Crafttype Linux - Newbie 2 05-26-2009 06:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:32 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