« August 2010 »
S M T W T F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        

This is hot

Upgrade VIA Epia-Board to current 2.6.28.7
601 times viewed
23.02.2009 18:41
t3blog Update to version 0.8.1 (Security, Bugfixes)
157 times viewed
03.02.2010 14:33
LaTeX Font Selection
83 times viewed
29.01.2010 17:17
linux.bigga.de now with RSS Feed
82 times viewed
25.11.2009 09:36

[blog...]

Currently the posts are filtered by: bash
Reset this filter to see all posts.

29.10.2009
19:43

Replace Bash Indirect Variable Expansion Feature

replace bash syntax with POSIX conform equivalent

Debian and Ubuntu changed the default shell /bin/sh from bash (Bourne Again Shell) to dash (Debian Almquist Shell).

The advantage of dash is its size, speed and POSIX compliance. The disadvantage is that many scripts written for bash won't work anymore because bash has specific features - so called "bashisms".

 

One of this bashisms, I used frequently in the xxsvideo build system is the " indirect variable expansion". Which makes it possible to execute the following script:

#!/bin/bash                                                                                                                                        
                                                                                                                                                  
CONFIG_BUSYBOX=y

PACKAGENAME="BUSYBOX"
PACKAGECONFIG=CONFIG\_$PACKAGENAME

if [ "${!PACKAGECONFIG}" = "y" ]; then
        echo $PACKAGENAME "is selected"
else
        echo $PACKAGENAME "is NOT selected"
fi

If you call this script with bash you receive as expected "BUSYBOX is selected".

With dash you see:

./test.sh: 13: Bad substitution

I was looking a long time for the right way to replace this {!VAR} statement. But it's quite simple and suddenly I found a thread describing it:

#!/bin/dash

CONFIG_BUSYBOX=y

PACKAGENAME="BUSYBOX"

PACKAGECONFIG=CONFIG\_$PACKAGENAME

if [ "$(eval echo '$'$PACKAGECONFIG)" = "y" ]; then
    echo $PACKAGENAME "is selected"
else
    echo $PACKAGENAME "is NOT selected"
fi

 

In short: With dash you write:

eval echo '$'$VARIABLE

which is equivalent to bash

echo ${!VARIABLE}

 

... I think ;-)

 

back

[ 22.07.2010 ]