0

I have this CSV with 2 column: one contains names and the other file names with no extension.

When I parse it and store the values in 2 array I'd like to add a path and PDF extension to the file names but when I do it, I get something like this:

Giacomos-MBP:test Jack$ ./test.sh -p e.csv -a 2017
.pdf://www.test.com/2017/03cr14a076d
.pdf://www.test.com/2017/03bg15a365d
.pdf://www.test.com/2017/03bs15a451d
.pdf://www.test.com/2017/03bg15a440d
.pdf://www.test.com/2017/03cr16a570d
.pdf://www.test.com/2017/03bg15a794d

I expect

http://www.test.com/2017/03bg15a794d.pdf

instead

Anybody knows why? It looks like the extension goes in front of the path

This is my script

#!/bin/bash

while getopts p:a: arg
do
    case $arg in
        p) multi=$OPTARG 
            ;;
        a) anno=$OPTARG
            ;;
    esac
done

declare -a citta
declare -a codice
i=0

while IFS=";" read col1 col2 do

    citta[$i]=$col1;
    codice[$i]=$col2;
    ((i++))

done < $multi


cartella='http://www.test.com/'$anno'/'
total=${#citta[*]}


for (( i=0; i<=$(( $total -1 )); i++ )) do

    last="${codice[$i]}.pdf"
    echo $cartella$last

done

As @FredrikPihl has suggested this is the output I get

Giacomos-MBP:test Jack$ ./test.sh -p e.csv -a 2017
+ getopts p:a: arg
+ case $arg in
+ multi=e.csv
+ getopts p:a: arg
+ case $arg in
+ anno=2017
+ getopts p:a: arg
+ declare -a citta
+ declare -a codice
+ i=0
+ IFS=';'
+ read col1 col2
+ citta[$i]=Agnadello
+ codice[$i]=$'03cr14a076d\r'
+ (( i++ ))
+ IFS=';'
+ read col1 col2
+ citta[$i]=Arcene
+ codice[$i]=$'03bg15a365d\r'
+ (( i++ ))
+ IFS=';'
+ read col1 col2
+ citta[$i]=Artogne
+ codice[$i]=$'03bs15a451d\r'
+ (( i++ ))
+ IFS=';'
+ read col1 col2
+ citta[$i]=$'\r'
+ codice[$i]=
+ (( i++ ))
+ IFS=';'
+ read col1 col2
+ total=4
+ cartella=http://www.test.com/2017/
+ (( i=0 ))
+ (( i<=3 ))
+ last=$'03cr14a076d\r.pdf'
+ echo $'http://www.test.com/2017/03cr14a076d\r.pdf'
.pdf://www.test.com/2017/03cr14a076d
+ (( i++  ))
+ (( i<=3 ))
+ last=$'03bg15a365d\r.pdf'
+ echo $'http://www.test.com/2017/03bg15a365d\r.pdf'
.pdf://www.test.com/2017/03bg15a365d
+ (( i++  ))
+ (( i<=3 ))
+ last=$'03bs15a451d\r.pdf'
+ echo $'http://www.test.com/2017/03bs15a451d\r.pdf'
.pdf://www.test.com/2017/03bs15a451d
+ (( i++  ))
+ (( i<=3 ))
Giacomos-MBP:test Jack$ 
Giacomo Scarpino
  • 599
  • 3
  • 17
  • 1
    change shebang to `#!/bin/bash -x` and examine the output. There are many issues with the code, one is that parameter expansion doesn't work within single quotes, i.e. `'`, use `"`. Also, please show input and expected output. It makes everything so much simpler :-) – Fredrik Pihl May 19 '17 at 13:21
  • outch :-) please update your question instead... – Fredrik Pihl May 19 '17 at 13:26
  • 2
    `e.csv` has DOS line endings. Remove them. – chepner May 19 '17 at 13:28
  • `fromdos` is a great command – Fredrik Pihl May 19 '17 at 13:32
  • 1
    as @chepner told you, the `\r` screws everyting up. Remove the DOS line endings using `fromdos` or something listed in this answer http://stackoverflow.com/questions/6373888/converting-newline-formatting-from-mac-to-windows – Fredrik Pihl May 19 '17 at 13:59
  • You can pipe the output through dos2unix to get rid of those \r characters. – Jack May 19 '17 at 14:55

0 Answers0