Solved

BASH - last charachter of an variable

Posted on 2007-04-06
6
236 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Suggested Courses

739 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