Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Space in file name problem

Status
Not open for further replies.

Mthales

Technical User
Feb 27, 2004
1,181
GB
Hi,

I've got an array that contains a list of object file names called OBJCUCUBLE. I'm trying use this to create symbolic links to the source files that correspond to these object files.

The script I have works fine except when there is a space in the file name. Could you please have a look at what I have and point me in the direction of where I'm going wrong?

Code:
hold=;
for obj in ${OBJCUCIBLE}; do
   # if the listed item doesn't end ".o"
   if [ ! -n "$( echo ${obj} | grep '[.]o')" ]; then
       [ ${ioptv} -gt 1 ] && echo "ERROR - The file name <$obj> is incomplete."
       hold=$obj
   else
        # if we have a value in hold stick it together with
        # the new value with an escaped space
          if [ -n "$hold" ]; then
          obj="${hold}\ ${obj}"
       hold=;
    fi
#
# Now work out which source file matches the object name
       cd ${CD}
       fn=`echo $obj | sed 's/[.]o[ ]*$//' | sed 's:^.*[/]::'`
       # check for all recognised source file extensions
       src=`ls $fn.[cCfsly] 2>/dev/null | awk ' { print $1 } ' `
       if [ -n "${src}" ] ; then
          cd ${TARGET}
          rm -f ./${src}
          ln -s ../${src} .
      else
       echo "An error has occurred because the file <${obj}> can't be found"
      fi
   fi
done

Thanks for your help
M

Trainee Chocolate Management Executive.
 
How is OBJCUCIBLE populated ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks for your quick responses guys.

Feherke - Unfortunately the IFS didn't work because they are separated by spaces - which I guess is more than part of the problem.

PHV - The array OBJCUCIBLE is populated by reading the values out of a simple text file. The definition of this file allows for #ed comment lines and says that the names can be separated by a space, newline or other white space character.
The line of code that does this reading in is as follows where item is the name of the text file.
Code:
OBJCUCIBLE=$( cat ${item} | grep -v '^#' | pr -t -e | sort | uniq )

Thanks
M

Trainee Chocolate Management Executive.
 
Generally speaking, unices don't play nicely with spaces in filenames, so the best plan is to avoid them where possible. If doing so is unavoidable quotes around the filenames usually works. But I'm probably missing the point here anyway!!
 
And how is the $item file populated ?
Anyway, if you really have space(s) embedded in filenames, your script should be deeply rewritten.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV - the $item file is hand made by the "user".
This leads me to think that you and ken are right when you say avoiding spaces in file names would be the only sensible course. I think I'll take that up with the design authority before I start rewriting it all.
Thank you all very much for saving me wasting hours struggling with this.

M

Trainee Chocolate Management Executive.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top