On 2016-09-05, Rakesh Sharma <
shar...@hotmail.com> wrote:
> On Sunday, 4 September 2016 14:34:44 UTC+5:30,
wke...@gmail.com wrote:
>
> > What I mean is that on some machines we will have
> > blkid /dev/sda1
> > /dev/sda1: LABEL="F24" UUID="69611ffb-a322-43b1-80fd-008a66b1a04e" TYPE="ext4"
> > and on some there is no LABEL set
> > /dev/sda1: UUID="0822921e-61d7-439a-a361-eb40116a2a74" TYPE="swap"
> >
> > I want to have a script which will return, for the first case,
> > 69611ffb-a322-43b1-80fd-008a66b1a04e, and
> > 0822921e-61d7-439a-a361-eb40116a2a74 for the second case.
> >
> > Any ideas ?
> > Kevin
> >
>
>
> # sed-based solution
> sed -ne 's/UUID="/\n/;s/.*\n//;s/"/\n/;P'
The wide old adage "it's all over but the shouting" was
crafted for these situations.
It was already shown that the the program can dump
the values of selected tags (-s <tag> option), and the format
can be altered (-o <format-keyword> option).
# blkid /dev/sda1
/dev/sda1: UUID="ffe2c614-30e3-4b97-9c00-1b25fef2934f" TYPE="ext4"
# blkid -o value -s UUID /dev/sda1
ffe2c614-30e3-4b97-9c00-1b25fef2934f
If you still feel like doing some clever shell thing,
you can use "-o export" and then eval the result:
# blkid -o export /dev/sda1
UUID=ffe2c614-30e3-4b97-9c00-1b25fef2934f
TYPE=ext4
If we
# eval $(blkid -o export /dev/sda1)
we then we now have UUID and TYPE shell variables.
The man page says that export makes for "easy import into the
environment", from which we might guess that either does shell
escaping for eval (easy import in shell scripts), or else dumps
the strings raw (easy for a C program that reads lines with fgets,
removes the newline from each one, and passes it to putenv).