Showing posts with label bourne. Show all posts
Showing posts with label bourne. Show all posts

Friday, June 5, 2009

The Bourne Shell War On Terror

Hey There,

I'm now, officially, a fan of The Thin Guy's Blog over at blogs.sun.com. At least, I'm officially a fan of the post I'm highlighting here today.

I don't really get too worked up over religion, politics and/or any other subject that most people hold very strong beliefs about (What's the point in having an argument that you can't possibly win? ;), but I thought this post, titled The War On Terror, as viewed from the Bourne shell was funny enough to share and absorb any backlash (that I'm positive I'm going to get) from family and friends.

I've included the basic post below, but you should check out the official page. A lot of the comments offer suggestions for improvement or addition that are just as entertaining :)

Cheers,



The War on Terror


As viewed from the Bourne shell.


$ cd /middle_east
$ ls
Afghanistan   Iraq          Libya         Saudi_Arabia  UAE
Algeria       Israel        Morrocco      Sudan         Yemen
Bahrain       Jordan        Oman          Syria
Egypt         Kuwait        Palestine     Tunisia
Iran          Lebanon       Qatar         Turkey

$ cd Afghanistan
$ ls
bin  Taliban    
$ rm Taliban
rm: Taliban is a directory
$ cd Taliban
$ ls
soldiers
$ rm soldiers
$ cd ..
$ rmdir Taliban
rmdir: directory "Taliban": Directory not empty
$ cd Taliban
$ ls -a
.            ..           .insurgents
$ chown -R USA .*
chown: .insurgents: Not owner
$ cd ..
$ su
Password: *******
# mv Taliban /tmp
# exit
$ ls
bin
$ cd bin
$ ls
laden
$ cd ..
$ rm -r bin/laden
bin/laden: No such file or directory
$ find / -name laden
$
$ su
Password: *******
# mv bin /tmp
# exit
$ pwd
/middle_east/Afghanistan
$ cd /opt/UN
$ ln -s /Bad_Guys/Al_Qaeda /middle_east/Iraq/.
ln: cannot create /middle_east/Iraq/Al_Qaeda: Permission denied
$ su
Password:*******
# ln -s /Bad_Guys/Al_Qaeda /middle_east/Iraq/.
# cd /middle_east/Iraq/Al_Qaeda
Al_Qaeda: does not exist
# rm /middle_east/Iraq/Al_Qaeda
# mkfile 100g /middle_east/Iraq/Al_Qaeda
mkfile: No space left on device
# rm /middle_east/Iraq/Al_Qaeda
# cd /opt/Coalition/Willing
# mkfile 1b /middle_east/Iraq/Al_Qaeda
# chown -R USA:Proof /middle_east/Iraq/Al_Qaeda
#exit
$ cd /middle_east/Iraq
$ ls
saddam
$ ls
saddam
$ ls
saddam
$ ls -a
.            ..           saddam
$ find / -name [Ww][Mm][Dd]
/Korea/North/wMd
$ wall Propaganda.txt
Broadcast Message from USA (pts/1) on USS_Abraham_Lincoln Th May 1st
Mission Accomplished!
$ rm saddam
saddam: No such file or directory
$ find / -name saddam
/var/opt/dictators/spiderhole/saddam
$ wall NewsWorthy.txt
Broadcast Message from USA (pts/1) on Time.Magazine Sat Dec 13
We Got Him!
$ mv /var/opt/dictators/spiderhole/saddam /opt/jail
$ cd /opt/USA
$ cp -Rp Democracy /middle_east/Iraq
$ cd /middle_east/Iraq/Democracy
$ ./install
Install Error: Install failed.  See install_log for details.
$ more install_log
Installed failed!
Prerequisite packages missing
Conflicting package Wahhabism found in /midde_east/Saudi_Arabia
Packages Church and State must be installed separately
File System /PeakOil nearing capacity
Please read the install guide to properly plan your installation.
$


Shirt Update:  New Shirts for sale over at Giant Robot Printing.  Please read here for details.  I will be closing down the CafePress shirt store, but will leave the poster up for sale.  After working with Ken from GRP, there is no way that all that text is going to look good on the CafePress Shirt.  I fully expect to have all the prior shirts returned.


Update:  If you don't get it, go here.





, 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.

Saturday, May 10, 2008

Finding the Number of Characters In A Variable Regardless Of Your Linux Or Unix Shell

Hey there,

Here's a simple enough task: Finding (or determining) the amount of characters in a string variable. In most Unix or Linux shells, this has become a trivial exercise. For instance, in bash or ksh, you can easily tell how many characters are in a particular string variable by either writing one line of code, or skipping right to the end and printing out the answer. By way of example:

host # x=thisIsMyVariable
host # echo ${#x}
16


And it's that simple. The variable x, which contains the string value "thisIsMyVariable", does, indeed, contain 16 elements (or characters). As you can see, in the more advanced shells, taking care of this is no problem.

However, in certain circumstances, you may be required to use a less-advanced, but more highly-portable, shell, like the original Bourne shell. In that case, this same sequence of commands would result in the following:

host # x=thisIsMyVariable
host # echo ${#x}
host #
<--- At best, you'll get zero here. Expect nothing. You might possibly get an error.

Our script attached today, uses the old-style expr (Well, it uses the new style expr with the old format and options, again, for maximum portability between systems and shells). It does the exact same thing that echo'ing ${#VARIABLE_NAME} does, but goes about it in a grueling and confusing manner. Remember back when you had to write scripts that took the shell's needs into consideration before your own? ;) Nowadays, things are much more intuitive.

Given a string, like "thisIsMyVariable," the following script will produce the exact same results. It will spit out how many characters are in that string, but take a longer, more confusing ,route to the answer. This is another good example of security through obfuscation as well. In general, having to use the expr command can be a hurdle for folks who've never used a shell that can't do arithmetic or string comparison on its own :)

Sample output (as boring as it is ;):

host # ./string.sh thisIsMyVariable
16


And now, you can get the string length no matter how limited your shell is!

Cheers,


Creative Commons License


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

#!/bin/sh

#
# string.sh - print out how many characters are in a string variable
#
# 2008 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

if [ $# -ne 1 ]
then
echo "Usage: $0 string"
exit 1
fi

string=$1
echo $string|while read x
do
chars=`expr "$x" : '.*'`
echo $chars
done


, 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