Search code examples
bashexpect

can't trim \r in variable


I get a list of files from a remote dir. Now I want to know if those files exist in a local dir. My problem is, that the \r in the string of each file name is untrimmable. I did it before in the 4th line and it worked.

numOfRemoteFiles=`expect countRemoteFiles.sh $user $remotedir $password $N | 
tail -1 | tr -d '\r'`
numOfRemoteFiles=$((numOfRemoteFiles-1))
for remotef in `expect forLocalDir.sh $user $remotedir $password $N | tail -n$numOfRemoteFiles`
do
    remotef=${remotef##*/}
    remotef=$remotef | tr -d '\r'
    if [ ! -f $localdir/$remotef ]; then
        expect receiveFile.sh $user $localdir $remotedir $password $N $remotef
    fi
done

I printed the file names in ascii and hex and this is the result:

test1.txt
0000000    74  65  73  74  31  2e  74  78  74  0d  0a                    
0000013
test2.txt
0000000    74  65  73  74  32  2e  74  78  74  0d  0a                    
0000013
test3.txt
0000000    74  65  73  74  33  2e  74  78  74  0d  0a                    
0000013

Solution

  • This doesn't do what you expect it to:

    remotef=$remotef | tr -d '\r'
    

    You probably meant:

    remotef=$(echo "$remotef" | tr -d '\r')