(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version



Bash typecasting



A recent post here mentioned typecasting in the shell. I responded by advising the poster to use some other language for his scripting needs. My feeling is that while I do appreciate that Bash can be used for complicated tasks, I just don't see the point: use a language that matches your expectations for features. For me, that's Perl, but others might choose Python or Php or Java or who knows - but if you bring the right tool (or a tool you know well), jobs go much easier.

But back to bash and its typecasting. Bash does let you declare how you intend to use a variable. Let's do it "wrong" first:


#!/bin/bash
ralph="randols"
goo=$ralph
if [ $goo -lt 7 ]
then
echo $goo
fi

That script will fail with this message: "line 5: [: randols: integer expression expected". That's because line five ends up looking like this:

if [ "randols" -lt 7 ]

The "-lt" operator expects integer expressions on both sides, so it doesn't like its lunch and says so.

You can fix that:

#!/bin/bash
ralph="randols"
declare -i goo
goo=$ralph
if [ $goo -lt 7 ]
then
echo $goo
fi

At line two, we state our intentions for "goo" ("typeset -i goo" would do exactly the same thing). That works: $goo ends up with a value of 0, and is an integer, so the "-lt" is happy and the script runs. Everybody is happy, right? Naaw. Let's throw a monkey wrench in:

#!/bin/bash
ralph="10 randols"
declare -i goo
goo=$ralph
if [ $goo -lt 7 ]
then
echo $goo
fi

All we've done is change the initial value of "ralph", but our script has a fit:


./t: line 4: 10 randols: syntax error in expression (error token is "randols")
./t: line 5: [: -lt: unary operator expected





That's all it takes to break it. A similar Perl script doesn't even need all this nonsense (and doesn't confuse you by using text ("-lt") for arithmetic comparisons and arithmetic operators ("<") for string tests as Bash does):

#!/usr/bin/perl
$ralph="10 randols";

if ( $ralph < 7 ) {
print "$ralph\n";
}

There are, of course, ways around any problem. But why fight it? OK, if you just love Bash and are used to working around this kind of thing, fine: have at it. You can find problems with Perl, too, and anything else. My point is to use a language YOU are comfortable with, a language that meets your needs and expectations.

Don't struggle. Be happy. No silk purses from sows ears necessary.


Technorati tags:


Click here to add your comments





Tue Aug 1 12:58:10 2006: Subject:   TonyLawrence

gravatar
Another minor point about the Perl script: the result of evaluating "10 randols" in an integer context is "10". That's often helpful in your scripts.. and yes, I know you can get the same effect in bash.. but that's not the point. The original poster didn't know much about Bash, so why get into it? If you have to learn scripting, learn with a "better" scripting language - better in the sense that it meets you expectations and needs of course.





Thu Feb 19 18:05:08 2009: Subject:   JasonAntman
http://www.jasonantman.com
gravatar
What about the other way around? If I pass bash an IP address as an argument, it wants to evaluate it as an integer....





Thu Feb 19 18:07:03 2009: Subject:   TonyLawrence

gravatar
Can you give a short example of what you are trying to do?

Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar


ad

cartoon

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


book graphic unix and linux troubleshooting guide

My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!



 I sell and support
 Kerio Mail server




pavatar.jpg
More:
       - Basics
       - Shell
       - Programming


Unix/Linux Consultants

Skills Tests

Guest Post Here











My Favorites

Change Congress