-1

I have one file listed as below, which i am reading as input in while loop:

The command cat status_1.txt yields:

SQLLOADER|FALSE
TX3|TRUE

Below is my script:

#!/bin/bash
set -x
cat /iris/shrikant/scripts/status_1.txt;
echo
echo
IFS="|"
while read a b
do
date;
c=TRUE
        if [ "${b}" = "${c}" ]; then
        echo $a is freezed;

        else

        echo $a is not freezed;

        fi;

done < status_1.txt;

but I am not getting the desired output:

cat /iris/shrikant/scripts/status_1.txt
SQLLOADER|FALSE
TX3|TRUE

+ cat /iris/shrikant/scripts/status_1.txt
SQLLOADER|FALSE
TX3|TRUE
+ echo

+ echo

+ IFS='|'
+ read a b
+ date
Tue Nov  6 23:38:19 +08 2018
+ c=TRUE
+ '[' $'FALSE\r' = TRUE ']'
+ echo SQLLOADER is not freezed
SQLLOADER is not freezed
+ read a b
+ date
Tue Nov  6 23:38:19 +08 2018
+ c=TRUE
+ '[' $'TRUE\r' = TRUE ']'
+ echo TX3 is not freezed
TX3 is not freezed
+ read a b

When value is matching if statement is printing else statement instead of matched conditions.

  • Your variable $b has a linefeed character in it so it's not going to match a string without linefeed. You'll need to edit status_1.txt and remove the control character. – doneal24 Nov 6 '18 at 15:58
2

The file status_1.txt is a DOS text file. This is evident from the \r (carriage return) at the end of the value of $b in the trace output.

You have two options:

  1. Convert the text file to Unix text file format through e.g. the dos2unix tool, or

  2. Take the \r into account for $b (it will not be present for $a as its value is not taken from the end of the line) and test with

    if [ "$b" = "$c"$'\r' ]
    
  • if [ "$b" = "$c"$'\r' ] --this option is working now. thank you. – Shrikant Goud Nov 7 '18 at 7:14
  • Again My question for above is status_1.txt file is created in Unix server. There is no way DOS file is generated – Shrikant Goud Nov 7 '18 at 7:17
  • @ShrikantGoud Some web services may output an extra carriage return at the end of each line too. You never mentioned how you create the file, but it is a DOS formatted text file. – Kusalananda Nov 7 '18 at 7:29
  • we use an application which have configuration status stored in that file which is present on linux server. i am creating that file via cat file|grep FREEZE > status_1.txt – Shrikant Goud Nov 7 '18 at 10:28
  • SQLLOADER|FALSE|FREEZE TX3|TRUE||FREEZE i ignore FREEZE Column by awk again – Shrikant Goud Nov 7 '18 at 10:28
1

Your ipout file is in DOS format and includes Carriage Return characters before the Newline characters.

Make sure that your file is in the apropriate UNIX format.

  • Again My question for above is status_1.txt file is created in Linux server. There is no way DOS file is generated – Shrikant Goud Nov 7 '18 at 7:18
  • Some UNIX tools (like e.g. script) create files with DOS line ends. It is hard to comment your statement unless you explain how you created the file in question. – schily Nov 7 '18 at 10:21
  • we use an application which have configuration status stored in that file which is present on linux server. i am creating that file via cat file|grep FREEZE > status_1.txt – Shrikant Goud Nov 7 '18 at 10:28
  • SQLLOADER|FALSE|FREEZE TX3|TRUE||FREEZE i ignore FREEZE Column by awk again – Shrikant Goud Nov 7 '18 at 10:29
  • You should try to find where the cariage Return characters first appear and remove them e.g. via tr -d '\015' – schily Nov 7 '18 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.