Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 15 Nov 2007 21:18:47 -0800
From:      Garrett Cooper <youshi10@u.washington.edu>
To:        freebsd-questions@freebsd.org
Subject:   Re: bash and strings
Message-ID:  <473D2837.6000301@u.washington.edu>
In-Reply-To: <20071116044331.GA21372@saraswathy.susmita.org>
References:  <52275.12.170.206.13.1195184604.squirrel@admintool.trueband.net> <20071116044331.GA21372@saraswathy.susmita.org>

next in thread | previous in thread | raw e-mail | index | archive | help
Girish Venkatachalam wrote:
> On 03:43:24 Nov 16, jhall@vandaliamo.net wrote:
>   
>> Everyone,
>>
>> I'm sure this is easy, and I am making it harder than it is.
>>
>> I am being supplied a list of files, and need to create the files and
>> directories to hold them, but I cannot figure out how to take the string
>> apart.
>>
>> For example, I am given
>>
>> /usr/local/scripts/firewall.sh
>>
>> I need to create the /usr/local/scripts directory and then create
>> firewall.sh.
>>
>> Any suggestions would be greatly appreciated.
>>     
>
> There is always more than one way to skin a cat. :)
>
> Perhaps you will like mine.
>
> DIR=`dirname $path`
> FILE=`basename $path`
> /bin/mkdir -p $DIR
> cd
> touch $FILE
>
> You can put this in a loop with path as loop variable.
>
> Best of luck!
>
> regards,
> Girish
A better way would be to quote the string variables, i.e.:

DIR=`/usr/bin/dirname "$path"`
FILE=`/usr/bin/basename "$path"`
/bin/mkdir -p "$DIR"
touch "$FILE"

Otherwise dirname and basename will choke on non-escaped characters 
(i.e. spaces), mkdir/touch will make funky directories / files, 
respectively.

Just watch out for '$' chars in $path...

Welcome to the wonderful world of [in]secure shell scripting :).

-Garrett



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?473D2837.6000301>