Solved

BASH - last charachter of an variable

Posted on 2007-04-06
6
234 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 

Author Comment

by:rares_dumitrescu
ID: 18863874
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
ID: 18864009

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
ID: 18864013
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rares_dumitrescu
ID: 18864072
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
ID: 18865113
anyone ?
0
 
LVL 58

Accepted Solution

by:
amit_g earned 300 total points
ID: 18865457
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

763 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