Local IP Address in Bash

How to get your local IP address in Bash

There are many ways to get your local IP address in Linux, and even more in Bash. If you know of one not mentioned here, please comment and mention it so I can add it. I’m putting this information here for future reference.

So, how do you get the local IP address on linux? Linux has a command-line command called “ifconfig” which is, basically, the linux version of Window’s “ipconfig”. Add some standard Linux commands to the mix and you can easily parse all that complicated output to give you just the local IP address for use in your Bash script. Here’s the full command:

echo $(ifconfig eth0 | grep "inet addr:" | cut --delimiter=: --fields=2 | awk '{print $1}')

If you aren’t familiar with these commands, here’s a quick overview:

  • echo” simply prints out whatever you pass to it. In this case, it will be the result of this “one liner” script.
  • ifconfig eth0″ prints out just the information about your “eth0” connection.
  • grep” widdles down our output even further, filtering out everything except the line that has our ip address in it.
  • cut” essentially splits our 1 string into an array of several strings.
  • awk” is a scripting language of it’s own, often used in Linux with a myriad of uses. In this script we’re using it to print the first string in our array (since that’s the IP address that we’re after). A note about Awk’s print function: while “$0” is valid, print is not zero-based. “$0” will print the whole line, while “$1” will print the first item.

And there you have it! Running the above command would give you the local IP address of the Linux computer you’re working on, like so…

chrisanesbit@ubuntu-rocks:~$ echo $(ifconfig eth0 | grep "inet addr:" | cut --delimiter=: --fields=2 | awk '{print $1}')
chrisanesbit@ubuntu-rocks:~$ 192.168.0.10

And “as is” that command can be dropped right into your very own Bash script. Simply assign it to a variable instead of echoing it to StdOut and you’ve got yourself a local IP to script against!

Know of another way, maybe even a better way, to accomplish this? Leave me a comment and let me know!

Dynamic CSS Classes in LESS

I’m always learning something new or getting better at things. Constant improvement is important to me. I recently began using LESS at work and figured out how to do something cool that I thought was worth documenting and sharing.

If you’re still reading this you probably already have some exposure to both LESS and CSS, so I’ll get right to it.

Repeating CSS

Sometimes you have several CSS rules that are very similar and only have slight variations. This can be kind of annoying to write and maintain, especially if the difference between each rule is predictable and logical.

The scenario that led me to learn this cool feature of LESS was indentation within my HTML table. I had several different levels of indentation that I needed to account for in a table of financial data. It didn’t make sense to use an unordered list, because my data was a table of dollars and sums; so I stuck with a table and created CSS rules I could apply to TD’s where necessary.

Here’s the raw CSS I used:

td.indent-level-1 {
    padding: 0 0 0 18px;
}
td.indent-level-2 {
    padding: 0 0 0 36px;
}
td.indent-level-3 {
    padding: 0 0 0 54px;
}

Functions in LESS

You probably noticed some similarities between the class names and rules, and a relationship between each class name and its rule.

Each class name starts with “indent-level” followed by an integer. Each padding rule is divisible by 18 and, coincidentally, the number in the class name multiplied by 18 equals the right padding I’m after; We’ll use those “coincidences” to our advantage when creating our LESS rules:

td {
    .indent-level(@depth) {
      /* do something */
    }
    .indent-level(1);
 }

“.indent-level(…)” is a LESS function that we will write. Functions in LESS – like any other Procedural Language – allow us to reuse your code. We can also pass variables to your function.

Defining and Using Variables in LESS Functions

So now that we’ve defined our function, and are calling it in our LESS stylesheet, let’s actually do something with it.

td {
    .indent-level(@depth) {
        @className: ~"indent-level-@{depth}";
        &.@{className} {
            padding: 0 0 0 (18px*@depth);
        }
    }
    .indent-level(1);
}

“@className: ..;” is a variable definition where we can use the parameter to dynamically create a string we’ll use as the class name. @{className} is how we use a variable (we’re actually doing that in our className definition too! So, LESS will take our rule and apply the variables to it and “drop it in” in place of our calls to the function.

Now, when we compile our LESS into CSS we get:

td.indent-level-1 {
    padding: 0 0 0 18px;
}

Notice how “(18px*@depth)” becomes “18px”? LESS knows that when we do math on a pixel the result will be a pixel. Also, see that we didn’t have to use “@{depth}” when doing math?

Calling LESS Function Iteratively

We have our LESS Function written and have created our first level of indentation. Great! But we need to create multiple, iterative, levels of indentation. We could call “.indent-level()” multiple times, with the numbers 1-3 hard coded into each call; or we could use a loop in LESS.

Now we can see that if we repeat our call to “.indent-leve(…)” 2 more times, we’ll get all 3 indentations defined thanks to reusable code. Our different levels of indentation will also be evenly spaced from the other levels because they will always be 18px apart!

td {
    .indent-level(@depth) when (@depth > 0) {
        @className: ~"indent-level-@{depth}";
        &.@{className} {
            padding: 0 0 0 (18px*@depth);
        }
        .indent-level(@depth - 1);
    }
    .indent-level(3);
}

Notice the call to “.indent-level(@depth – 1)” is inside our LESS Function. This is how LESS knows to continue looping. It’s not a true loop, it’s more like conditional recursion. Also notice that we only have to actually call the “.indent-level(…)” function 1 time to get things started. And in this case we’re calling it with the largest depth first, and decrementing as we go. Now here’s the result:

td.indent-level-3 {
    padding: 0 0 0 54px;
}
td.indent-level-2 {
    padding: 0 0 0 36px;
}
td.indent-level-1 {
    padding: 0 0 0 18px;
}

Pretty cool! Hopefully this is helpful for you. I’d love to hear how you are using LESS functions, variables, and such to make your CSS easier to maintain. I’m still constantly learning new things myself!

Easter Outfit

CarterThis past Sunday, local churches around the country were blessed with full congregations! So many people came to church, even those who don’t normally go except on Christmas or Easter. But Easter is unique. Each year thousands of people go to the mall and buy a new suit or a new dress; or they wait their turn at the dry cleaners hoping to get their best dress or best suit back in time to wear it to church on Easter Sunday morning.

Easter Sunday’s Best Dressed

There are countless reasons why we do this in America and everyone has their own reason. But here’s a few that stood out to me:

  • Sitting in an uncomfortable environment for an hour once a year is a small price to pay for the excuse to buy fancy new clothes.
  • It is also a small price to pay for political, corporate, or social recognition (especially when your clothes are such that people cannot help but notice you).
  • Unreligious members of the community, or irregular church attenders, break out in a “religious sweat” twice a year as part of their traditions or religious obligations to their conscience.
  • People like to show off their nice clothes, or their gorgeous kids, as a symbol of financial, or social, status.

These are just a few reasons that are common among church goers on Easter Sunday.

Jesus is the Reason for the Season

But think about this: If you were going to the Whitehouse to meet a President of the United States in a one-on-one meeting how should you dress? Why should you dress like that?

So, why should we dress our best for Easter Sunday (or Resurrection Sunday as Christians often refer to it)? Why does it matter?

Jesus, the true reason we celebrate Easter, died (as dramatized in The Passion Of The Christ Definitive Edition). He was brutally tortured and died the death of a Roman execution on a cross; and He did it for me, and for you! See, because we are all sinners powerless to save ourselves we need someone to take our place if we want any chance of escaping the eternal consequences of our sin! And that “Someone” is Jesus! Jesus died. He died to take our place so we could have eternal life!

Stained glass at St John the Baptist's Anglica...

Stained glass at St John the Baptist’s Anglican Church http://www.stjohnsashfield.org.au, Ashfield, New South Wales. Illustrates Jesus’ description of himself “I am the Good Shepherd” (from the Gospel of John, chapter 10, verse 11). This version of the image shows the detail of his face. The memorial window is also captioned: “To the Glory of God and in Loving Memory of William Wright. Died 6th November, 1932. Aged 70 Yrs.” (Photo credit: Wikipedia)

If you have asked God to forgive you for the pain you caused His Son on that cross 2,000 years ago and have received His forgiveness, this should inspire a sense of reverence and thankfulness in your heart that provokes you to give Jesus your best on the special day set aside to honor His loving sacrifice for you because Jesus is worth our best!

Does the way you dress for church reflect your reverence for your personal Savior?

Get Row Number Based on Physical Location in SQL Server

Getting Row Number in SQL Server

Assigning a row number to your results in SQL is a common task and is doing so is pretty straight-forward so I won’t spend a lot of time on it here. Take a look at the syntax here:

USE AdventureWorks2008R2;
GO
SELECT PersonType, FirstName, LastName, 
ROW_NUMBER() OVER(PARTITION BY PersonType ORDER BY FirstName, LastName DESC) AS Row
FROM Person.Person

Row number is determined by ordering your data by certain criteria, using the same syntax as a typical ORDER BY that you’d find at the end of a query. You optionally could specify a way to partition your results (group them into blocks of data). Each partition restarts the row number at 1. If no partition is specified, the entire result set is considered a single partition.

Note that the ORDER BY in your Row Number is mutually exclusive from the ORDER BY of your result set. So it is possible to number your rows in a different order than you return them.

Getting Physical Location of a Row in SQL Server

Mika Wendelius, a Code Project contributor, offers a great explanation of finding a rows physical location on SQL Server using an unsupported feature in SQL Server. Basically, each table has a “hidden” column called %%physloc%% which stores a row’s physical location in hexidecimal form. That’s pretty hard to read but, fortunately, SQL Server offers a way for us to read this information a little more easily: the sys.fn_PhysLocFormatter function, which I found on sqlskills.com (since this is also an unsupported feature on SQL Server). According to sqlskills.com “It gives the database file, page within the file, and slot number on the page in the format (file:page:slot).”

One of the commenters of this on SQL Skills shows a great way to use CROSS APPLY to bring in the results of the PhysLocFormatter function in a more usable fashion. This is the final building block we need in order to number our results based on physical location.

Getting Row Number Based on Physical Location in SQL Server

We know how to number our results based on ordering them a certain way. We also know how to find the physical location of a row in SQL Server.  Now we will put it all together.

USE AdventureWorks2008R2;
GO
SELECT PersonType, FirstName, LastName, 
ROW_NUMBER() OVER(ORDER BY file_id, page_id, slot_id) AS Row
FROM Person.Person 
CROSS APPLY sys.fn_PhysLocCracker(%%physloc%%)
ORDER BY Row

With our CROSS APPLY pulling in the physical location data for each row, we can now order our ROW_NUMBER() by file_id, page_id, and slot_id (largest to smallest address info). It really is that simple.

Should I use this in production?

I discovered this clever gem while feeding my curiosity. I have not tested this in a production environment as it was more of an academic excursive for me using unsupported features of SQL Server. I would love to hear if anyone has used these functions in production and how well they performed.

Are Ghosts Real?

The other day, I was driving past some creatively decorated homes, with spooky life-sized halloween “characters” on their front porch. I began thinking back to a Bible study I participated in some time ago about Ghosts, and other spooky halloween “apparitions”. I began dusting the cobwebs of my brain, trying to remember exactly what the Bible says about Ghosts.

Wouldn’t you know, the next day I saw an article in my Facebook Feed about that very thing. The article was published on the Answers In Genesis blog, an organization whose thoroughly Biblical views I’ve come to highly respect over the years. Answers in Genesis (AiG) is probably best known for their Creation Museum but, in the recent past, has gained a lot of attention from the public eye for hosting a debate of whether Creation A Viable Model of Origins between it’s leader Ken Ham and the famed Bill Nye “The Science Guy”.

Reading this AiG article about the existence (or lack thereof) of ghosts only validated the beliefs that I already had by further grounding me with Biblical proof for what I believe.

So, Are ghosts real? I strongly encourage you, Christian or not, to give this article an honest read and decide for yourself. How you answer this question could influence what happens to you beyond the grave!

answersingenesis.org

ALS Ice Bucket Challenge for Christians

ALS Ice Bucket Challenge

My day has finally come! (sense the sarcasm?) I have received the Ice Bucket challenge for ALS. I can choose to accept the challenge and donate $100 to ALS research or I can “cop out” and donate a smaller amount if I agree to dump a bucket of ice over my head. I’d like to say I donated but $100 is a lot of money for some of us. Besides, I have a bigger problem with the ALS Association (the recipient of donations by this viral trend).

Christians Need to Think Twice

What’s the big deal? It’s a charity donation to research a cure for an incurable disease. But things aren’t always what they seem. The ALS Association supports Embryonic stem cell research which destroys living human embryos in the name of finding a life-saving cure. In layman’s terms, this aborts a human life.

The Ethical Ice Bucket Challenge

Many Christians are not taking the challenge because of this. I almost didn’t do the challenge or donate because of this either. But ALS is still a terminal disease without a cure and needs research. Ethical research. Fortunately, there are alternatives. I will be donating to the Midwest Stem Cell Therapy Center (MSCTC) at the University of Kansas Medical Center since they do not participate in the unethical embryonic stem cell research. A variety of ethical ALS research centers can be found on lifenews.com’s website.

Here comes the Fun!

And without further ado, here’s my video for your viewing pleasure.

We are all Addicts: Part 2

Addiction

If you haven’t read Part 1 of this series, please take a moment to do so before reading this Part 2. I showed you that we are all addicts (yes, even me): How we all have something that captivates our focus and energy more than anything else. These “so-called” addictions can be perceived as good things but the fact is this: If an addiction takes our focus of Jesus Christ, God’s Son who died for our sins, then that addiction is a sin. Continue reading

Photo credit: procsilas / Foter / Creative Commons Attribution 2.0 Generic (CC BY 2.0)

We are all Addicts: Part 1

I know it’s been a couple weeks since I’ve posted anything to my blog. I apologize for not keeping in touch with you, as my valued reader. But this topic has such an impact on all of our lives I wanted – no, I needed – to take my time and prayerfully make sure that what I tell you is strictly from the Bible. This mini-series could have been titled “What does the Bible say about addiction?” but it’s important that we understand we are all addicted to something no matter how “good” we think we are. Yes even me and you.

Every single one of us is Continue reading

3 things you didn’t know about the Signers of the Declaration of Independence

Independence Day

Who signed it?

56 delegates from the 13 colonies signed the Declaration of Independence. Many, but not all, of these men were lawyers. There were several doctors, plantation owners, business men, preachers, even farmers!

Several religious denominations were represented by this group of men as well, yet God remained at the forefront of these delegates minds when creating the Declaration of Independence, as one can clearly see from its text (emphasis Continue reading