Bash parameter expansion

Christopher Browne cbbrowne-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org
Mon Aug 23 19:18:42 UTC 2010


On Mon, Aug 23, 2010 at 3:14 PM, Lennart Sorensen
<lsorense-1wCw9BSqJbv44Nm34jS7GywD8/FfD2ys at public.gmane.org> wrote:
> On Sun, Aug 22, 2010 at 06:36:11PM -0400, 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:
>
> That is a kernel change, not a Debian change.
>
>> 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?   And: is there an easier way to do
>> a string of Bash parameter expansions than assigning to multiple temp
>> values like that?  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.
>
> Best option is look at /dev/disk/by-*
>
> Each disk will have a unique identifier (usually serial number based), so
> you can do things based on that.  Much better than what you used to have.
>
> For example:
>
> /dev/disk/by-id/ata-WDC_WD2500JD-22HBB0_WD-WCAL72953808 -> ../../sdb
>
> So simply using the consistent by-id names means not worriying about
> which /dev/sd* it happens to be today.  It isn't your problem.

Ah, slick.

Here's sample output of this sort of thing:

ls  -l /dev/disk/by*
/dev/disk/by-id:
total 0
lrwxrwxrwx 1 root root  9 Aug 19 11:19 ata-ST3320418AS_6VMF0G0D -> ../../sda
lrwxrwxrwx 1 root root 10 Aug 10 11:32 ata-ST3320418AS_6VMF0G0D-part1
-> ../../sda1
lrwxrwxrwx 1 root root 11 Aug 10 11:32 ata-ST3320418AS_6VMF0G0D-part10
-> ../../sda10
lrwxrwxrwx 1 root root 10 Aug 10 11:32 ata-ST3320418AS_6VMF0G0D-part2
-> ../../sda2
lrwxrwxrwx 1 root root 10 Aug 10 11:32 ata-ST3320418AS_6VMF0G0D-part3
-> ../../sda3
lrwxrwxrwx 1 root root 10 Aug 10 11:32 ata-ST3320418AS_6VMF0G0D-part4
-> ../../sda4
lrwxrwxrwx 1 root root 10 Aug 10 11:32 ata-ST3320418AS_6VMF0G0D-part5
-> ../../sda5
lrwxrwxrwx 1 root root 10 Aug 10 11:32 ata-ST3320418AS_6VMF0G0D-part6
-> ../../sda6
lrwxrwxrwx 1 root root 10 Aug 10 11:32 ata-ST3320418AS_6VMF0G0D-part7
-> ../../sda7
lrwxrwxrwx 1 root root 10 Aug 10 11:32 ata-ST3320418AS_6VMF0G0D-part8
-> ../../sda8
lrwxrwxrwx 1 root root 10 Aug 10 11:32 ata-ST3320418AS_6VMF0G0D-part9
-> ../../sda9
lrwxrwxrwx 1 root root  9 Aug 19 11:19 scsi-SATA_ST3320418AS_6VMF0G0D
-> ../../sda
lrwxrwxrwx 1 root root 10 Aug 10 11:32
scsi-SATA_ST3320418AS_6VMF0G0D-part1 -> ../../sda1
lrwxrwxrwx 1 root root 11 Aug 10 11:32
scsi-SATA_ST3320418AS_6VMF0G0D-part10 -> ../../sda10
lrwxrwxrwx 1 root root 10 Aug 10 11:32
scsi-SATA_ST3320418AS_6VMF0G0D-part2 -> ../../sda2
lrwxrwxrwx 1 root root 10 Aug 10 11:32
scsi-SATA_ST3320418AS_6VMF0G0D-part3 -> ../../sda3
lrwxrwxrwx 1 root root 10 Aug 10 11:32
scsi-SATA_ST3320418AS_6VMF0G0D-part4 -> ../../sda4
lrwxrwxrwx 1 root root 10 Aug 10 11:32
scsi-SATA_ST3320418AS_6VMF0G0D-part5 -> ../../sda5
lrwxrwxrwx 1 root root 10 Aug 10 11:32
scsi-SATA_ST3320418AS_6VMF0G0D-part6 -> ../../sda6
lrwxrwxrwx 1 root root 10 Aug 10 11:32
scsi-SATA_ST3320418AS_6VMF0G0D-part7 -> ../../sda7
lrwxrwxrwx 1 root root 10 Aug 10 11:32
scsi-SATA_ST3320418AS_6VMF0G0D-part8 -> ../../sda8
lrwxrwxrwx 1 root root 10 Aug 10 11:32
scsi-SATA_ST3320418AS_6VMF0G0D-part9 -> ../../sda9
lrwxrwxrwx 1 root root  9 Aug 19 11:19 wwn-0x5000c50027931ff1 -> ../../sda
lrwxrwxrwx 1 root root 10 Aug 10 11:32 wwn-0x5000c50027931ff1-part1 ->
../../sda1
lrwxrwxrwx 1 root root 11 Aug 10 11:32 wwn-0x5000c50027931ff1-part10
-> ../../sda10
lrwxrwxrwx 1 root root 10 Aug 10 11:32 wwn-0x5000c50027931ff1-part2 ->
../../sda2
lrwxrwxrwx 1 root root 10 Aug 10 11:32 wwn-0x5000c50027931ff1-part3 ->
../../sda3
lrwxrwxrwx 1 root root 10 Aug 10 11:32 wwn-0x5000c50027931ff1-part4 ->
../../sda4
lrwxrwxrwx 1 root root 10 Aug 10 11:32 wwn-0x5000c50027931ff1-part5 ->
../../sda5
lrwxrwxrwx 1 root root 10 Aug 10 11:32 wwn-0x5000c50027931ff1-part6 ->
../../sda6
lrwxrwxrwx 1 root root 10 Aug 10 11:32 wwn-0x5000c50027931ff1-part7 ->
../../sda7
lrwxrwxrwx 1 root root 10 Aug 10 11:32 wwn-0x5000c50027931ff1-part8 ->
../../sda8
lrwxrwxrwx 1 root root 10 Aug 10 11:32 wwn-0x5000c50027931ff1-part9 ->
../../sda9

/dev/disk/by-path:
total 0
lrwxrwxrwx 1 root root  9 Aug 19 11:19 pci-0000:00:1f.2-scsi-0:0:0:0
-> ../../sda
lrwxrwxrwx 1 root root 10 Aug 10 11:32
pci-0000:00:1f.2-scsi-0:0:0:0-part1 -> ../../sda1
lrwxrwxrwx 1 root root 11 Aug 10 11:32
pci-0000:00:1f.2-scsi-0:0:0:0-part10 -> ../../sda10
lrwxrwxrwx 1 root root 10 Aug 10 11:32
pci-0000:00:1f.2-scsi-0:0:0:0-part2 -> ../../sda2
lrwxrwxrwx 1 root root 10 Aug 10 11:32
pci-0000:00:1f.2-scsi-0:0:0:0-part3 -> ../../sda3
lrwxrwxrwx 1 root root 10 Aug 10 11:32
pci-0000:00:1f.2-scsi-0:0:0:0-part4 -> ../../sda4
lrwxrwxrwx 1 root root 10 Aug 10 11:32
pci-0000:00:1f.2-scsi-0:0:0:0-part5 -> ../../sda5
lrwxrwxrwx 1 root root 10 Aug 10 11:32
pci-0000:00:1f.2-scsi-0:0:0:0-part6 -> ../../sda6
lrwxrwxrwx 1 root root 10 Aug 10 11:32
pci-0000:00:1f.2-scsi-0:0:0:0-part7 -> ../../sda7
lrwxrwxrwx 1 root root 10 Aug 10 11:32
pci-0000:00:1f.2-scsi-0:0:0:0-part8 -> ../../sda8
lrwxrwxrwx 1 root root 10 Aug 10 11:32
pci-0000:00:1f.2-scsi-0:0:0:0-part9 -> ../../sda9
lrwxrwxrwx 1 root root  9 Aug 10 11:32 pci-0000:00:1f.2-scsi-1:0:0:0
-> ../../sr0

/dev/disk/by-uuid:
total 0
lrwxrwxrwx 1 root root 10 Aug 10 11:32
64daee94-a903-4bbc-8d0a-86649e255bbc -> ../../sda5
lrwxrwxrwx 1 root root 10 Aug 10 11:32
6c3211c2-9222-4a29-b56f-907c9ea55661 -> ../../sda6
lrwxrwxrwx 1 root root 10 Aug 10 11:32
7480f326-b80b-4c22-ba3c-f5179d9d6d8b -> ../../sda9
lrwxrwxrwx 1 root root 10 Aug 10 11:32
79c3ffa9-4b98-42b4-ab1a-a86582132ace -> ../../sda7
lrwxrwxrwx 1 root root 11 Aug 10 11:32
935b1f6c-346b-4d8c-a074-a92306656935 -> ../../sda10
lrwxrwxrwx 1 root root 10 Aug 10 11:32
9c6322f8-4ceb-42fb-b95f-9e34b834f4e8 -> ../../sda3
lrwxrwxrwx 1 root root 10 Aug 10 11:32
aea3961d-6f97-4cbe-ad27-b6e863cef67c -> ../../sda1
lrwxrwxrwx 1 root root 10 Aug 10 11:32
b38eb8c7-1f5f-4c9f-bfe8-963dcfe9a069 -> ../../sda8
lrwxrwxrwx 1 root root 10 Aug 10 11:32
c960ec6c-2bbc-4838-9099-1d986bbbbb77 -> ../../sda2
-- 
http://linuxfinances.info/info/linuxdistributions.html
--
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