簡體   English   中英

Git Bash 腳本回顯命令 output 反參數后

[英]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

我創建只是為了調試其中 output 是的 query.txt 文件:

';從 audit_trail.audit_trail 中選擇不同的操作,其中 gcid='XXX

代替

select 與 audit_trail.audit_trail 不同的操作,其中 gcid='XXX'

簡而言之,$gcid 參數后面的每個字符串都會寫在整個字符串的開頭。 如果我使用 unix 終端,則 echo output 可以。 為什么在 git bash 中,“echo”命令提到了錯誤的 output?

提前致謝

我認為您在包含CR的字符串的終端上看到 output (十進制的13或十六進制的0D ):最后一個'; 字符從行首開始寫入,從而覆蓋字符串的前兩個字符。

該字符串實際上由select dist... gcid='XXX\r'; ,並且只是在終端上笨拙地(對人類)打印。


有很多方法可以從輸入中刪除CR ,這里有兩種方法:

# 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM