Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

script question

Posted on 2014-11-15
2
Medium Priority
?
281 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
2 Comments
 
LVL 23

Accepted Solution

by:
savone earned 1000 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 1000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

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…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month7 days, 17 hours left to enroll

824 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