Maurizio Cacace
Maurizio Cacace

Reputation: 73

Git Bash script echo command output inverted after parameter

i'm using git bash for windows (git version 2.18.0.windows.1) to write a bash script like this one:

file=$1
if [[ ! -s "$file" ]] ; then #empty

           echo -e "${RED}Invalid argument. Pass the file with all GCIDs as INPUT!!!${NOCOLOR}"
else

           number=$(cat $file | wc -l )
           number=$(($number+1))
           echo -e "** ${number} GCID detected **"
           echo ""
           while read -r gcidRead                        

               do
                 gcid=${gcidRead}                     
                 echo -e "select distinct operation from audit_trail.audit_trail where gcid='$gcid';" >> query.txt
                                        

                 value=$(psql "host=XXXX port=62013 dbname=prodemeagcdm user=XXXX password=XXXX" <<-EOF
                         select distinct operation from audit_trail.audit_trail where gcid='$gcid';
                                                                       \q
                                                                       EOF
                                                                       ) 

                   echo -e "${value}" >> output.txt
                   if grep -q delete_bupa output.txt ; then
                      echo -e "${gcid}" >> gcidDeleted.txt       

                   fi                                    
           done < $file
 fi

I created just to debug the query.txt file in which the output is:

';lect distinct operation from audit_trail.audit_trail where gcid='XXX

instead of

select distinct operation from audit_trail.audit_trail where gcid='XXX'

In short, every string after $gcid parameter will be written at the beginning of the entire string. If I use a unix terminal the echo output is ok. Why in git bash the "echo" command has the wrong output mentioned?

Thanks in advance

Upvotes: 1

Views: 646

Answers (1)

LeGEC
LeGEC

Reputation: 51988

I think you see the output on terminal of a string which contains a CR (13 in dec or 0D in hex) : the last '; cahracters are written from the beginning of the line, thus overwriting the first two characters of the string.

The string actually consists of select dist... gcid='XXX\r';, and is just printed awkwardly (to a human) on the terminal.


There are many ways to drop CR from the input, here are two of them :

# remove all CR chars from the input :
cat $file | tr -d '\r' | while read -r gcdiRead; do
  ...
done

# remove all CR chars only at end of lines (e.g : when followed by LF) :
cat $file | sed -e 's/\r$//' | while read -r gcdiRead; do
  ...
done

Upvotes: 1

Related Questions