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
Solved

BASH - last charachter of an variable

Posted on 2007-04-06
6
233 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
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

829 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