From owner-freebsd-questions@FreeBSD.ORG Fri Nov 16 05:17:42 2007 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 5CF2616A41A for ; Fri, 16 Nov 2007 05:17:42 +0000 (UTC) (envelope-from youshi10@u.washington.edu) Received: from mxout3.cac.washington.edu (mxout3.cac.washington.edu [140.142.32.166]) by mx1.freebsd.org (Postfix) with ESMTP id 400F613C468 for ; Fri, 16 Nov 2007 05:17:42 +0000 (UTC) (envelope-from youshi10@u.washington.edu) Received: from smtp.washington.edu (smtp.washington.edu [140.142.32.141] (may be forged)) by mxout3.cac.washington.edu (8.13.7+UW06.06/8.13.7+UW07.09) with ESMTP id lAG5HZ8T019564 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 15 Nov 2007 21:17:36 -0800 X-Auth-Received: from [128.208.5.249] (lodovico.cs.washington.edu [128.208.5.249]) (authenticated authid=youshi10) by smtp.washington.edu (8.13.7+UW06.06/8.13.7+UW07.09) with ESMTP id lAG5HZG4022303 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT) for ; Thu, 15 Nov 2007 21:17:35 -0800 Message-ID: <473D2837.6000301@u.washington.edu> Date: Thu, 15 Nov 2007 21:18:47 -0800 From: Garrett Cooper User-Agent: Thunderbird 2.0.0.9 (Windows/20071031) MIME-Version: 1.0 To: freebsd-questions@freebsd.org References: <52275.12.170.206.13.1195184604.squirrel@admintool.trueband.net> <20071116044331.GA21372@saraswathy.susmita.org> In-Reply-To: <20071116044331.GA21372@saraswathy.susmita.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-PMX-Version: 5.3.3.310218, Antispam-Engine: 2.5.2.313940, Antispam-Data: 2007.11.15.205925 X-Uwash-Spam: Gauge=IIIIIII, Probability=7%, Report='__CT 0, __CTE 0, __CT_TEXT_PLAIN 0, __HAS_MSGID 0, __MIME_TEXT_ONLY 0, __MIME_VERSION 0, __SANE_MSGID 0, __USER_AGENT 0' Subject: Re: bash and strings X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 16 Nov 2007 05:17:42 -0000 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