Bash parameter expansion

Chris F.A. Johnson chris-E7bvbYbpR6jSUeElwK9/Pw at public.gmane.org
Mon Aug 23 00:12:43 UTC 2010


On Sun, 22 Aug 2010, Giles Orr wrote:

> With Debian's recent change to the way it handles device names at
> boot-up, I can't count on / being on sda anymore.  Today it's on sde,
> but what I've read suggests that that may change.  This messes up my
> automatic backup of the MBR, so I'm trying to figure out how to
> determine this value automatically.  What I've come up with so far is
> this:
>
> tmp="$(mount | grep [[:space:]]/[[:space:]])" ; tmp1=${tmp%% *};
> tmp2=${tmp1#/dev/} ; echo ${tmp2%%[0123456789]*}
>
> Two questions: is there an easier way to determine this value without
> pulling it out of mount like that?

## these use non-standard bashisms
{ read; read tmp junk; } < <(df /)
echo ${tmp:5:3}

>   And: is there an easier way to do
> a string of Bash parameter expansions than assigning to multiple temp
> values like that?

   You can use the same variable for each expansion, e.g.:

tmp=$(whatever)
tmp=${tmp#??}
tmp=${tmp%*[0-9]}

   However, each expansion is a separate operation.

> Even if there's an simple answer for the first
> question I'd love an answer to the second as I've had to use that
> messy method several times.  (I know I can do this in awk or perl more
> easily: I'd like to stick to Bash here.)  Thanks in advance.


-- 
    Chris F.A. Johnson, <http://cfajohnson.com>
    Author:
    Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
    Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list