Showing posts with label simple. Show all posts
Showing posts with label simple. Show all posts

Tuesday, March 24, 2009

Finding Simple Solutions To Complex Problems In An Insane World

Hey there,

The title of today's post probably doesn't sound all that impressive. And, to my way of thinking, it really shouldn't. Oddly enough, the only reason I'm writing this post is because I had a bastard of a time figuring out a problem today and drove myself nuts in the process. I'm still working on getting it perfect, so this is (SPOILER ALERT) going to be an opinion piece today. I've been in the biz since 1995 and here I was thinkin' all the new-fangled Operating Systems would have a simple command to do everything and anything with ;)

On my way to scripting myself out of a figurative paper bag, I ran across a few other solutions to my problem involving c structures in the proc filesystem that could get you the very information I was looking for, but I wasn't looking to re-invent the wheel, either. Generally, if something exists on your Unix or Linux system (at the lower levels) it's been implemented in a tool. Sometimes finding that tool can be a challenge. Sometimes it's easy. Sometimes, I'll fret for hours wondering why simple things sometimes seem so complicated. Then I wake up the next morning and realize what a tool I was being ;)

And I've always been hyper-aware of how more-and-more-ridiculous our society has become over the last few decades. I see my fair share of panic attacks and what look like suicides-in-the-making all around me. Back when I was a kid, you'd only see that sort of behaviour in unrepentant alcoholics and good hard-working folk who'd either gotten scammed out of (or somehow, otherwise, managed to lose) their entire life's savings.

It used to be that folks like Michael Milken would have to go out of their way to ruin many many other people's lives. Now it seems like it's happening every day in every way. Was that a millisecond delay in our "Virtual Meeting" with the Hong Kong, Helsinki and Bora Bora offices??? That's all it takes now. Just suck it up and watch a few commercials on TV. All those people are so thankful that they can work no matter where they are in the world. Even on vacation! Am I the only person who finds this sickening? If you have kids, try to get them interested in psychiatry or social work. By the time they grow up, it probably won't even be dangerous anymore. The people who live in middle-to-lower-class neighborhoods will probably be much more together than the corporate elite.

13 years or so ago, I was the proud owner of a DX-50 PC. It had a 512Mb hard drive, 4 Mb's of memory and a screaming processor that I could overclock to 66Mhz. Vroooom!!!!! Some online services still required you to pay a premium to download at 9600 and watching each individual line of a picture paint itself across my screen in slo-mo was captivating. Now I sometimes want to throw my monitor out the window because it takes my computer 2 minutes to boot up. That doesn't even make sense! For some reason, it seems, the monitor is the recipient of a large percentage of misdirected rage in this computer age. The funny thing is that it's usually the only part of your setup that isn't pissing you off. The keyboard usually deserves it ;)

Hopefully (although it's not too late), you got to check out the vi with a Windows Paperclip picture we put up a few days ago. I've always wanted to trounce that thing, or at least bend it in the wrong direction. No matter what you do, it keeps finding a way to reinsert itself back into your life, along with that frickin' dog. I'm having a coronary while he's wagging his tail and licking a book (???) Hopefully we won't see any of this nonsense from OpenOffice. I use that (and UltraEdit) exclusively now. And, even though I screw up majorly every once in a while, at least I don't have to deal with the sideways looks from those to "icons" that loom just a bit too large in our modern Pantheon.

But, as we come back around to the titular portion of this rant, there's always a relatively simple way to do anything. The trick is having the patience to find it. And, even more confusing, the trick to the trick to finding it is to retrain yourself to take stock of what's really necessary in life. You have to be able to see through all the advertising, hype, jargon and pressure and realize that your own (and I'm including myself in the world now ;) insecurity is really the only product that's being trading in the marketplace today. It's like "Keeping up with the Jones's," except now it's really important and you're competing with Jones's from all over the world. What will happen if you make a mistake, or miss a call, or go on vacation to someplace that doesn't have photon beam Internet capability? The answers: You'll feel like an idiot for a little while, you'll eventually call someone back and you'll finally be able to relax on that faraway vacation isle where every asshole and his brother can't reach out and grope you :)

Time is money. In an abstract sense, this is true. I've never worked anywhere they wouldn't pay me to. I can make nothing by exerting a lot less effort ;) Just remember this: When the global economy gets to the point that we can do business in real-time from any point on this earth to any other point on this earth, what will be the next big thing that we'll have to worry about falling behind on? Think about it. There has to be a point where we can go no further (or begin colonizing other planets in our solar system so we can setup remote offices that need to get in touch with us "RIGHT NOW!!!!").

When we reach that point; when we touch that apex, it's going to either be a huge load off of all of our shoulders or (and, my money's on this, since there's money to be made preying upon people's fears) we'll be on a slippery slope to our "global community"'s nadir.

In the meantime, if life seems to hard, consider that you might just be allowing yourself, or others, to be too hard on you. Take a deep breath, relax and come to realize that (if you can get to that place of peace) you're probably going to be the one guy out of 1000 that doesn't have a stress-related breakdown this year. A long time ago, people traded things other than money for services, at a much slower pace, and the fact that we're here now is proof that they didn't screw up all that badly. You see, there's an upside to not freaking out, too.

Simple :)

Cheers,

, Mike




Discover the Free Ebook that shows you how to make 100% commissions on ClickBank!



Please note that this blog accepts comments via email only. See our Mission And Policy Statement for further details.

Monday, May 12, 2008

String Variables In Bash, Perl, C and Awk on Linux or Unix - Porting

Hey There,

Once again, we're back to porting. In this series of posts that's run the gamut from a somewhat brief explanation of the shebang line (The rightful starting place) to shell, Perl and C porting examples of a fully functional useradd program, we're finally coming back around to the beginning and getting back to basics. Trust me; this will eventually all fall together. When it does, I'll be sure to put up a road map so no one has to try the hit-and-miss blog-search method of information nesting ;)

Today, we're going to look at the simple variable (also referred to as scalar, string, etc); defining, assigning value to and extracting value from it on both Unix and Linux based systems. Our approach is going to be concept-based. That is to say that, for each post on cross-language porting, we'll be hitting on a single concept (such as the simple variable) and showing how each can be applied to our chosen four languages: bash (or shell), Perl, C and Awk (Some folks think Awk isn't a programming language for some reason, but we'll demonstrate, over time, that it must be, since it contains all the constructs that generally define a language as a "programming" language; as opposed to a "markup" language like HTML). I'm going to try and keep this linear, so we're starting out with the very basics and will, eventually, work toward more complex programming constructs.

Here we go:

The simple variable is relatively accurately described in its name. This is one of the simplest forms of variables, as it can be realistically thought of as having only two parts: Part 1 is the variable itself, and part 2 is that variables definition or value. If we say x=y, then the variable x (itself) equals y (its definition/value). Simple enough.

1.Defining, Initializing or Declaring the simple variable. This part is going to be simple for every kind of variable (simple and otherwise), because (except in C), no explicit declaration of most variables is necessary. For the simple variable, Bash, Perl and Awk allow you to define the variable when you assign it value. C requires that you define your variable before you use it. In Bash, Perl and Awk, you have the option to define your variable before use if you wish. Examples below (Note that all beginning and ending double quotes are for emphasis only and not actual code):

Ex: Defining a variable called MySimpleVariable.

In Bash: Just type "declare MySimpleVariable" (you can also use "typeset," and both have options to specify what type of variable you want your simple variable to be. For instance, you could type "declare -i MySimpleVariable" if you wanted your variable to be limited to only being an integer. For now, we're not imposing any restrictions.

In Perl: Just type "$MySimpleVariable;"

In Awk: Just type "declare MySimpleVariable"

In C: You "need" to declare/initialize your variables before you can use them. For this post, we'll stick to numbers and strings for the simple variable (even though, technically, a char string is an array in C). Pretty much everything else isn't simple ;)

For a simple integer variable, just type: "int MySimpleVariable;"
For a simple string variable, just type: "char *MySimpleVariable;" (This generally needs to be followed by a declaration of the size/memory-allocation-requirement of the string, like "MySimpleVariable = (char *)malloc(8*sizeof(char));" for an 8 character string)

2. Assigning values to the simple variable. This is very straightforward in all of our four languages:

Ex: We want to assign the value "MySimpleValue" to the simple variable named MySimpleVariable (Note that any values that contain spaces should be quoted).

In Bash: Just type "MySimpleVariable=MySimpleValue" - Spaces between the variable, "=" sign and value are not permitted.

In Perl: Just type "$MySimpleVariable = MySimpleValue;" - Spaces between the variable, "=" sign and value are optional.

In Awk: Just type "MySimpleVariable = "MySimpleValue"" - Spaces between the variable, "=" sign and value are not, technically, necessary, but recommended. Also, note that "MySimpleValue" is placed within double quotes in the assignment. This is sometimes necessary for string variables, but not for numeric variables (e.g. sometimes "a = b" doesn't work, but "a = 1" does. In this case "a = "b"" ( double quoted value) is required for the string variable assignment, but not the integer).

In C: Just type: "MySimpleVariable = MySimpleValue;" for an integer assignment. For a character, or string, assignment you must surround the value with double quotes (e.g. "MySimpleVariable = "MySimpleValue";").

3. Extracting the value from your simple variable. Finally, it's all going to pay off :)

Ex: We want to print the value of the MySimpleVariable variable. This is also fairly simple in all four languages (Okay, C is always going to be a bit more of a pain ;)

In Bash: Just type "echo $MySimpleVariable" - Note that the $ character needs to precede the variable name when you want to get the value.

host # echo $MySimpleVariable
MySimpleValue


In Perl: Just type "print "$MySimpleVariable\n";" - Note that the $ character needs to precede the variable name when you want to get the value - The \n, indicating a carriage-return, line-feed or new-line isn't necessary, but is nice if you don't want your output on the same line as your next command prompt:

host # perl -e '$MySimpleVariable = MySimpleValue;print "$MySimpleVariable\n";'
MySimpleValue


In Awk: Just Type "print MySimpleVariable" - Note that the $ character "must not" precede the variable name when you want to get the value.

host # echo|awk '{MySimpleVariable="MySimpleValue";print MySimpleVariable}'
MySimpleValue


In C: Just type "printf("%d\n", MySimpleVariable);" for an integer assignment. For a character, or string assignment, you would type: "printf("%s\n", MySimpleVariable);" -- The %s in printf indicates a "string" value and the %d indicates a simple decimal (or integer) value. Note that, for this post, we're going to skip the whole compile part of getting your C program to get you output. You can just take the examples from the preceding steps as guidance. There is a bit more to making a standalone C program than there is to making a standalone program with our other three languages.

host # ./c_program
MySimpleValue


And that's all there is to the simple variable (for the most part ;)

Enjoy, and Cheers,

, Mike

Sunday, December 16, 2007

Simple Encryption and Decryption For Fun And No Profit

Here's another little fun thing for the weekend. It's based on Perl's pack and unpack functions and is a good introduction to using them for other purposes. It's also a good way to rediscover the fun of "passing notes in class," even though you're older and have a real job now ;)

You can use this script any way you want to; in fact it's written with a few things left out (how you want to deal with shell special characters -- another complete post on its own -- and if you want to expand on it to read and/or write to STDIN/STDOUT so you can pipe one instance to another, even though that defeats the purpose to a certain degree).

Hopefully, it will pique your curiosity about Perl and its ability to compress and convert different data types (I'm using hexadecimal and character here, but the available list is voluminous).

I've included usage points in the comments section of the script, but the basic usage would be:

tranz.pl encode your message here
tranz.pl decode 458616e6b6370264f62702659637964796e6760245865602c496e657870216e6460255e6968702d456e6167656279656
<-- The Hex output from a message encoded with this script.

Enjoy, and have a safe Sunday :)


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

#!/usr/bin/perl

#
# 2007 - Mike Golvach - eggi@comcast.net
# Sanitize shell special characters in
# whatever manner you prefer - or just
# backslash them on the command line :)
#
# Sample usage:
# trans.pl encode hi there
# trans.pl encode hi there >FILE
# trans.pl encode `cat FILE`
# trans.pl decode 8696024786562756
# trans.pl decode `cat FILE`
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

$input = $ARGV[0];
shift @ARGV;
$message = join(" ",@ARGV);

if ( $input eq "encode") {
$output=unpack("h*",$message);
@length=$output=~/.{0,256}/g;
print("$output \n");
} elsif ( $input eq "decode" ) {
chomp($message);
$output.=pack"h*",$message;
print "\n$output\n";
} else {
print "Usage: $0 [encode|decode] whatever you want to type\n";
exit(1)
}


, Mike





Friday, November 2, 2007

Simple Arithmetic in the Bourne Shell

Afternoon,

Most folks use shells like bash and ksh (myself included) because of the additional functionality they provide. Shells like sh, jsh, ash and the standard posix are more limited and don't come with a lot of bells and whistles.

Of course, working in a limited shell does have its advantages. For instance, if you write a script in sh, it'll probably work on most any system you port it to. Write in on Solaris and you can most likely run it on Solaris, HP-UX, SCO, Linux, etc.

One of the bonuses the more advanced shells come with is the ability to perform arithmetic simply. However, simply because the functionality exists overtly in the more advanced shells doesn't mean that, without a little practice, you can't perform arithmetic just as easily in "lesser" shells.

In sh (which we'll use as our example "simple" shell from this point forward) you can do arithmetic pretty easily if you know how to use the "expr" command.

Let's say you want to add 2 + 2 and assign that result to a variable. Simple enough, right? In ksh you could simply do:

a=2
let result=$a+$a
echo $result


And you're all set. Of course, it's not that much more difficult in sh. As shown below:

a=2
result=`expr $a + $a`
echo $result


Doesn't seem all that much more difficult, huh?

Basically, in sh you have to use expr. That's okay, because its on virtually every flavor of Unix available, as is sh. You get your result from expr (which simply evaluates expressions; arithmetic and otherwise) by calling it from between the backticks (``). Backticks in Unix are just a simple way of saying "replace what's within these backticks with the result of the expression, calculation, function or assignment executed within these backticks."

So, to break it down, sh simply does this:

a=2
result=`expr 2 + 2`
expr 2 + 2 returns 4
result=4


There are lots of things you can do like this in lesser shells. Ultimately, it's a user preference, but, if you're looking for maximum portability, taking advantage of the lesser shells is a great way to go. Virtually everything that can be done easily in more user-friendly shells can be done in lesser shells.

Sometimes it's simple, like this, and sometimes its beyond the point of confusing. For instance, faking arrays in sh isn't very complicated, but it can be very easy to lose track of. But that's for another day!

, Mike