Solved

BASH - last charachter of an variable

Posted on 2007-04-06
6
225 Views
Last Modified: 2013-12-26
Hello,
How can i see the last charachter of an variable ?
lets say:
var=123456.
and i want to echo just the '.'
0
Comment
Question by:rares_dumitrescu
  • 3
  • 2
6 Comments
 

Author Comment

by:rares_dumitrescu
Comment Utility
or, how can i check if an variable looks like an ip? (like this: xxx.xxx.xxx.xxx)
Where xxx are digits, and they can be 1 2 or 3 digits between the '.'
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
Comment Utility

You can convert to array and then check each portion:

#!/bin/bash
var='12.345.678.1'
IFS='.'
arr=($var)
echo "${arr[0]} ${arr[1]} ${arr[2]} ${arr[3]}"arr=(`echo $var|tr '.' ' ')
--or--
#!/bin/bash
var='12.345.678.1'
arr=(`echo $var|tr '.' ' '`)
echo "${arr[0]} ${arr[1]} ${arr[2]} ${arr[3]
0
 
LVL 29

Assisted Solution

by:MikeOM_DBA
MikeOM_DBA earned 200 total points
Comment Utility
Ooops, typo:

#!/bin/bash
var='12.345.678.1'
IFS='.'
arr=($var)
echo "${arr[0]} ${arr[1]} ${arr[2]} ${arr[3]}"
--or--
#!/bin/bash
var='12.345.678.1'
arr=(`echo $var|tr '.' ' '`)
echo "${arr[0]} ${arr[1]} ${arr[2]} ${arr[3]
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:rares_dumitrescu
Comment Utility
and how could i include it in a while, something like:

while -the variable is not xxx.xxx.xxx.xxx (xxx can be max 255)-
do ask for a new variable
0
 

Author Comment

by:rares_dumitrescu
Comment Utility
anyone ?
0
 
LVL 58

Accepted Solution

by:
amit_g earned 300 total points
Comment Utility
You could use something like this...

var="123.45.6.7"
if echo "$var" | egrep -e '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$'
then
    IFS='.'
    arr=($var)
    if [ ${arr[0]} -le 255 -a ${arr[1]} -le 255 -a ${arr[2]}  -le 255 -a ${arr[3]} -le 255 ]
    then
        echo "IP"
    else
        echo "Non-IP"
    fi
else
    echo "Non-IP"
fi
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now