aymanzone
aymanzone

Reputation: 135

Remove carriage return end of variable

I'm getting really strange output for this program. What is the "Carriage Return" doing, and how to remove it - missing single quote in the end? Why is the letter "T" missing? How to write code to correct this?

code i'm using

#!/bin/bash

export DATABASE_LIST="/opt/halogen/crontab/etc/db_stat_list.cfg"
export v3=""

while read -r USERID ORACLE_SID2
do
 v3="This is '${ORACLE_SID2}' "
 echo $v3
done < <(tac $DATABASE_LIST)

output

'his is 'OT1SL80
'his is 'OT1SL010

The file I'm reading from is not corrupt and is small one with two lines

[oracle@ot1sldbm001v test2]$ cat /opt/halogen/crontab/etc/db_stat_list.cfg
asp_dba/dba OT1SL010
asp_dba/dba OT1SL80

Thank you

Upvotes: 0

Views: 1336

Answers (1)

Gordon Davisson
Gordon Davisson

Reputation: 125708

Your DATABASE_LIST file is in DOS/Windows format, with carriage return + linefeed at the end of each line. Unix uses just linefeed as a line terminator, so unix tools treat the carriage return as part of the content of the line. You can keep this from being a problem by telling the read command to treat the carriage return as whitespace (like spaces, tabs, etc), since read automatically removes whitespace from the beginning and end of lines:

...
while IFS="$IFS"$'\r' read -r USERID ORACLE_SID2
    ...

Note that since this assignment to IFS (which basically lists the whitespace characters) is a prefix to the read command, it only applies to that one command and doesn't have to be set back to normal afterward.

Upvotes: 2

Related Questions