Cp/Mkdir and spaces

I am having trouble using cp and mkdir commands on a small script. The problem is I have spaces what I am trying to mkdir and then copy. Any suggestions on how I can solve this problem? I've tried searching online.

while read linefile
do  
  echo mkdir -p $CPP$linefile | cut -d / -f 1-5,8-12
  echo cp "$linefile" $CPP$linefile | cut -d / -f 1-11,14-18
done < /tmp/itunes_50.txt

I've tried \" but that didn't help. Thanks in advance.

Try to quote all your variables and change the IFS temporary:

OIFS=$IFS
IFS=""

Do you stuff here..........

IFS=$OIFS

But.... you should avoid spaces in file and directory names.

Regards

Sorry, I don't quite follow. Still learning.

It should looks like:

#!/usr/bin/sh
.
.
.

OIFS=$IFS
IFS=""

while read linefile
do  
  echo mkdir -p "${CPP}""${linefile}" | cut -d / -f 1-5,8-12
  echo cp "${linefile}" "${CPP}""${linefile}" | cut -d / -f 1-11,14-18
done < /tmp/itunes_50.txt

IFS=$OIFS

.
.

Regards

You do learn something new everyday. The code kind of worked. It what you said it would do, but no exactly what I wanted. I did change the script, but again another error. I know, most of you with bash experience will probably say this is some ugly code. I'm learning, deal with it.

while read linefile
do  
  MKK=`echo "$CPP$linefile" | cut -d / -f 1-5,8-10`
  echo mkdir -p "\"$MKK"\"
  # echo $MKK
  CCP=`echo "$linefile""\" "\""$CPP""$linefile" | cut -d / -f 1-11,14-18`
  echo cp "\"$CCP"\"
done < /tmp/itunes_50.txt

Now it works fine with the echo command. But when I get rid of the echo and just use cp or mkdir, it creates no directories and tells me I am not using the copy command correctly. I have tried different `"' but nothing I do seems to solve this problem.

What is the output of these commands?

echo mkdir -p "\"$MKK"\"
echo cp "\"$CCP"\"

Regards

Here's what I get...

mkdir -p "/Users/diegoa/Desktop/itunesdir/Music/Smashing Pumpkins/Mellon Collie And The Infinite Sadness - Dawn To Dusk"
cp "/Volumes/Diego_External/Music/Smashing Pumpkins/Mellon Collie And The Infinite Sadness - Dawn To Dusk/02 Tonight, Tonight 1.mp3" "/Users/diegoa/Desktop/itunesdir/Music/Smashing Pumpkins/Mellon Collie And The Infinite Sadness - Dawn To Dusk/02 Tonight, Tonight 1.mp3"

Which is perfect if I copy it and run it on the shell.

Echo the output to sh like this:

while read linefile
do  
  MKK=`echo "$CPP$linefile" | cut -d / -f 1-5,8-10`
  echo mkdir -p "\"$MKK"\"
  # echo $MKK
  CCP=`echo "$linefile""\" "\""$CPP""$linefile" | cut -d / -f 1-11,14-18`
  echo cp "\"$CCP"\"
done < /tmp/itunes_50.txt | sh

Regards

That's it, it works! I don't get it, how exactly did | sh fix the problem?

If you pipe the line to sh the command is passed to a subshell. The subshell executes the string as a command typed on the command line.
If a program expects an input from the standard input you can also pipe the input to that program, thus

echo ls -l|sh

gives the same output as

ls -l

Hope this helps.

Ahhh. Interesting. Thanks for all your help Franklin52.