Bash - 'While IFS= read' - do not read the last line of text

Need help with C, C++, perl, python, etc?

Bash - 'While IFS= read' - do not read the last line of text

Postby Allain » 2017-07-15 19:23

Hello everybody
I got a maybe stupid problem, here it is
My script uses two while loops
The first loop gives me the complete list from geturl2.bak and put the result in gvar
The second loop reads gvar and redirect to the file gvar2
but i lose the last line in gvar2, which is not read...
I have read a lot of comments about this : missing 'fi' or 'done' in a loop...
missing newline in my text file
But i can't solve this
Regards

I am reading this file, geturl2.bak :
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    21
    23
    24
    25
    26
    28
    29
    30
    31
    32
    33
    34
    36
    37
    38
    39
    40
    41
    45
    46
    47
    48
    51
    52
    53
    54
    55
    57
    58
    59
    60
    61
    62
    64
    67
    68
    70
    72
    73
    75
    76
    77
    79
    82
    83
    94
    95
    96
    97
    98
    99

... read by this script :
Code: Select all
#!/bin/bash
  mkdir -p json6
  if [ -f gvar ] ; then rm gvar ; fi
  if [ -f gvar2 ] ; then rm gvar2 ; fi
  input="geturl2.bak"
  var2="" ; var1=1
  while FS= read -r var2 ; do
    if [ $var2 -eq $((var1 + 1)) ]; then
      echo -n "$var2 " >> gvar
    else
      echo -ne "\n$var2 " >> gvar
      #echo >> gvar
    fi
    var1=$(( var2 ))
  done < "$input"
  less gvar
# The above displays well, not the following, last line of gvar is lost in gvar2
  input="gvar"
   while IFS= read -r var2 ; do
    read -a arr <<< $var2
    #echo "array : ${arr[0]} ${arr[1]} ${arr[2]} ${arr[3]} ${arr[4]}..."
    var1=$(( ${arr[0]} ))
    var2=$(( ${arr[-1]} ))
    echo -n ${arr[@]} >> gvar2
    if [ "$var1" -eq "$var2" ] ; then
      echo -ne " (curl ${arr[0]})\n" >> gvar2
    else
      echo -ne " (curl ${arr[0]} - ${arr[-1]})\n" >> gvar2
    fi
    echo >> gvar2
  done < "$input"
  less gvar2


Please check this
Allain
 
Posts: 21
Joined: 2016-12-06 13:55

Re: Bash - 'While IFS= read' - do not read the last line of

Postby Allain » 2017-07-15 19:42

After other testing, having two line feed at the end of geturl2.bak makes the trick
but...
I get one more '(curl )' in gvar2. I don't need this empty curl
Allain
 
Posts: 21
Joined: 2016-12-06 13:55

Re: Bash - 'While IFS= read' - do not read the last line of

Postby ralph.ronnquist » 2017-07-16 04:56

Have you tried
Code: Select all
echo >> gvar
before
Code: Select all
less gvar
User avatar
ralph.ronnquist
 
Posts: 299
Joined: 2015-12-19 01:07
Location: Melbourne, Australia

Re: Bash - 'While IFS= read' - do not read the last line of

Postby Allain » 2017-07-16 22:30

Yes i did, you can see it commented

I made a pause after each line in the second loop
and discovered that gvar got a new blank line at the top...
I don't find why
Is the possible cause the 'echo' commands in the while read loop
because i use \n once at the beginning and once at the end of the line ?

I got solutions but i want to understand what is happening in this case
Allain
 
Posts: 21
Joined: 2016-12-06 13:55


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

fashionable