1

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

1
  • blind guess : input file has cr lf line endings (windows format), and your bash script keeps the cr when calling read -r gcidRead. Confirm that by piping your output to a hexdump (bash myscript.sh | hd), and check if you see character 13 (dec) or 0D (hex) before the ;. Commented Jan 14, 2021 at 14:06

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

great! adding cat $file | tr -d '\r' | while read -r gcdiRead; do works many thanks
while IFS=$'\r' read -r gcdiRead; ... done < "$file" will also solve it. read trims whitespace at the beginning and end of the line, so if you tell it CR is whitespace...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.