Category Archives: Technology

These posts are primarily focused on my experiences with technology. Like faith and family, so much of what we do in our lives is affected by technology and learning new things here can help technology be less scary and more beneficial.

How to switch email providers without losing your emails

Recently, I needed to switch to a new email server and I didn’t want to lose all my emails. If I were switching from one cPanel web host to another it would be easy because cPanel has a migration process. But What if you’re moving between two different types of systems? I figured there has to be an easy, generic, way to switch email providers without losing all your emails.

Switch email providers easily, with imapsync

Say hello to imapsync. Imapsync is a Linux program you can migrate an email account from any provider to any other one.

And because it’s a Linux command line program you can easily script a migration for hundreds, or even thousands, of mailboxes with a simple script.

You can also use this to switch from Hotmail to Gmail.

Head on over to the author’s page and see how easy imapsync is to use. He even provides a basic script for you to use when migrating a group of mailboxes!

The source code to this amazingly simple, yet effective, tool is on github.com, for you tech-savy geeks.

Switch email addresses without linux

So, you’re not a linux person. No worries, there’s a solution for you too! The creator of imapsync also created an online tool that lets you run imapsync in your browser. There’s no geeky skills required, but your friends may start considering you a smart geek once you tell them that you were able to switch to a new email address without losing your emails!

For you linux gurus who don’t like installing tools on your server that you’re only going to use once, this is also a viable choice!

Check out this awesome, web-based tool at https://imapsync.lamiral.info/X/.

Conclusion

what have you done to keep from losing all your emails? Have you had to wrestle with a custom script in order to migrate from one system to another? Tell me about it in the comments.

 

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!

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.

Links, 1st Edition

.comTechnology is such a useful thing especially when it integrates with our daily lives. And with that, the internet can be a great source of knowledge, and resources, if you can find what you’re looking for. I will be sharing a collection of links Continue reading

Why I left WordPress.com and how I did it

Goodbye WordPress.com

If you’re interested in having a free blog with basic functionality and a few options, WordPress.com may fit the bill. Your blog will be subjected to their branding and and advertising, especially if you choose a free template Continue reading

Announcing chrisanesbit.com!

I’ve enjoyed all the cool experiences of starting my own blog. It’s been fun. But up to this point I’ve been blogging for free on WordPress.com. Hosting this blog on WordPress.com was convenient in many ways but I had to play by their Continue reading