Professor_Dodo
Professor_Dodo

Reputation: 99

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

Upvotes: 2

Views: 2302

Answers (2)

Bokili Production
Bokili Production

Reputation: 414

My example string trimming (like String.trim() in Java):

function trim(){ # Exaple: remoref=" da da\n da  da \r "; echo => "da da da da"
    trimed=$@;
    trimed=$(echo "$trimed" | tr -d '\n');
    trimed=$(echo "$trimed" | tr -d '\r');
    echo $trimed;
}

The function can be called like this:

remotef=$(trim "$remotef");

Upvotes: 0

Mr. Llama
Mr. Llama

Reputation: 20899

This doesn't do what you expect it to:

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

You probably meant:

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

Upvotes: 7

Related Questions