Solved

script question

Posted on 2014-11-15
2
275 Views
Last Modified: 2015-01-09
Can you please help me write a shell script for the following

when I execute the following i should be getting this output as follows

I need to take this argument and split the 0xffffff00 as follow

ff ff ff 00 --> 255.255.255.0   (how do i convert this to this way)



 $ ./network.sh 172.16.200.123/0xffffff00 -hex
            0xac10c8ff
0
Comment
Question by:linuxperson
[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
2 Comments
 
LVL 23

Accepted Solution

by:
savone earned 250 total points
ID: 40444925
This was fun to figure out and I learned a lot.  I am sure there might be a better way to do this, but seems to work.

Here is my test:
$ ./convert.sh 172.16.200.123/0xffffff00
Here is the conversion
255.255.255.0

Here is the code I used:
#!/bin/bash
ARG1=`echo $1 | cut -d/ -f2 | tr '[:lower:]' '[:upper:]'`
pair=($(echo $ARG1|sed  's/\(..\)/\1 /g'))

p1="$(echo -e "ibase=16; ${pair[1]}" | bc)" 
p2="$(echo -e "ibase=16; ${pair[2]}" | bc)"
p3="$(echo -e "ibase=16; ${pair[3]}" | bc)"
p4="$(echo -e "ibase=16; ${pair[4]}" | bc)"

echo "Here is the conversion"
echo $p1\.$p2\.$p3\.$p4

Open in new window

0
 
LVL 12

Assisted Solution

by:tel2
tel2 earned 250 total points
ID: 40446315
Hi linuxperson,

I'm not totally clear on the bottom line of your question, but if you want to take this as input:
    172.16.200.123/0xffffff00
and convert it to this:
    255.255.255.0
(as savone seems to think), then you could do it in Perl like this:
    echo "172.16.200.123/0xffffff00" | perl -ne '/0x(.+)/; map{push @dec, hex} unpack "(A2)*",$1; print join(".",@dec)'

But if you really want a shell solution, then savone's might be the way to go.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

1. Introduction As many people are interested in Linux but not as many are interested or knowledgeable (enough) to install Linux on their system, here is a safe way to try out Linux on your existing (Windows) system. The idea is that you insta…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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