Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'd like to write a shell script (OSX) which copies files listed in a csv file to a particular directory. I need to strip quotes from each line in the csv file.

My attempt

for i in $(cat myfile.csv)
  temp="${$i%\"}"
  temp="${temp#\"}"

do
  cp foldername/$temp foldername/subfoldername
done

I get this error:

./mfcsv.sh: line 2: syntax error near unexpected token `temp="${$i%\"}"'
./mfcsv.sh: line 2: `  temp="${$i%\"}"'
share|improve this question
    
Can you show us an example of your csv file? –  terdon May 1 at 12:20

2 Answers 2

Put the do after the parameters expansions, not before. And leave out the $sign within the expansion and use double quotes around the expansions except when assigning to a variable. For example

while read file
do
  temp=${file%\"}
  temp=${temp#\"}
  cp "foldername/$temp" "foldername/subfoldername"
done < myfile.csv
share|improve this answer
    
thanks. In your example is 'file' received as a command line argument or should it be replaced with the filename? –  codecowboy May 1 at 11:23
    
@codecowboy You're welcome. file is a variable name, you could also use i and then temp=${i%\"} –  Scrutinizer May 1 at 11:30

You have 2 errors:

  1. Wrong loop syntax, do should be at 2nd line
  2. bad substitution syntax, no dollar sign before i needed

Try this:

#!/bin/bash

for i in $(cat myfile.csv)
do
  temp="${i%\"}"
  temp="${temp#\"}"

  cp foldername/$temp foldername/subfoldername
done

but, it will fail if you have spaces in filenames. Script below will work also with spaces:

#!/bin/bash

while read i;
do
  temp="${i%\"}"
  temp="${temp#\"}"

  cp "foldername/$temp" "foldername/subfoldername"
done < myfile.csv
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.