Re: [Hampshire] Handling Volume Labels with Spaces

Top Page

Reply to this message
Author: Stuart Sears
Date:  
To: Hampshire LUG Discussion List
Subject: Re: [Hampshire] Handling Volume Labels with Spaces
Richard Brown wrote:
> On Fri, Feb 15, 2008 at 11:28 AM, Stephen Davies
> <stephen.davies@???> wrote:
>> I'm a bit stuck with a script I'm trying to write for a friend.
>> I can get the volume string using the following
>>
>> cat /proc/mounts | grep sdc1 | awk '{ print $2 }'

grrrr :)

why not awk '/sdc1/ {print $2}' /proc/mounts

:)

/me votes for quoting the vars you are using, as have others.


>> This gives "Test\40Volume"
>> However this is unusable in commands a the space is translated to "\40"
>> eg
>> involume=`cat /proc/mounts | grep sdc1 | awk '{ print $2 }'`
>> cp -R $involume ${outvol1}/${copy_date}/.


when I relabel a disk to be called "disk with gaps", I can access the
whole name like this:

srcdisk="$(df /dev/sdb1 | grep -o '/media/.*')"

(although technically you can get away without the quotes here)
You just have to quote it in use to get around the gaps issue:

[stuart@behemoth2 ~]$ srcdisk="$(df /dev/sde1 | grep -o '/media/.*')"
[stuart@behemoth2 ~]$ echo ${srcdisk}
/media/disk with gaps
[stuart@behemoth2 ~]$ echo boo > /tmp/test
[stuart@behemoth2 ~]$ cp /tmp/test ${srcdisk}
cp: target `gaps' is not a directory
[stuart@behemoth2 ~]$ cp /tmp/test "${srcdisk}"
[stuart@behemoth2 ~]$ ls $srcdisk
ls: cannot access /media/disk: No such file or directory
ls: cannot access with: No such file or directory
ls: cannot access gaps: No such file or directory
[stuart@behemoth2 ~]$ ls "$srcdisk"
lost+found test




> As victor just said, you'll need to quote or escape $involume to use it with cp.
>
> If you're feeling very lazy just add
>
> | sed -e 's#\\40# #'


so now we get to use cat, grep, awk and sed in one command. Talk about
exploring the command line :)


Stuart