space char shell script problem

Derek Ragona derek at computinginnovations.com
Sun Aug 24 20:42:48 UTC 2008


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.



More information about the freebsd-questions mailing list