Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

BASH - last charachter of an variable

Posted on 2007-04-06
6
Medium Priority
?
239 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 800 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
Stressed Out?

Watch some penguins on the livecam!

 

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 1200 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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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 this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

670 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