Date: Sun, 24 Aug 2008 15:42:28 -0500 From: Derek Ragona <derek@computinginnovations.com> To: David Banning <david+dated+1219918782.39167f@skytracker.ca>, questions@freebsd.org Subject: Re: space char shell script problem Message-ID: <6.0.0.22.2.20080824153456.025ac978@mail.computinginnovations.com> In-Reply-To: <20080823101941.GA42601@skytracker.ca> References: <20080823101941.GA42601@skytracker.ca>
next in thread | previous in thread | raw e-mail | index | archive | help
At 05:19 AM 8/23/2008, David Banning wrote: >I am running into a problem with the space character in filenames. >For instance, If I want to run the script; > >for x in `ls` >do > echo $x >done > >then filenames that have a space in them ie: "john smith.jpg" >are processed by my script as two names, "john" and "smith.jpg". > >What is the best way to deal with this type of space problem in the shell? > >I know that file names in quotes solves some problems but I can't tranfer >that to my script. Depending on what your script is doing, I would use an intermediate file and awk. Something like: ls >/tmp/mytempfile cat /tmp/mytempfile | awk '{ print $0 }' if you are looking for something special add grep to the mix: cat /tmp/mytempfile | awk '{ print $0 }'|grep -i [some name pattern] rm /tmp/mytempfile You can save the results to another temporary file for more processing, or use awk more to create commandlines to execute in another script file such as: cat /tmp/mytempfile | awk '{ print $0 }'|grep -i [some name pattern] | awk '{printf"cp %s /backup/backupdir\n", $0)}' >/tmp/mycopyscript chmod +x /tmp/mycopyscript /tmp/mycopyscript So depending on what your original script was doing, this method may work for you. -Derek -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?6.0.0.22.2.20080824153456.025ac978>