Why does this 'while' loop not recognize the last line?












2














We use the following script:



more test.sh
#!/bin/bash

while read -r line
do

echo $line

done < /tmp/file


This is the file:



kafka-broker,log.retention.hours,12
kafka-broker,default.replication.factor,2
fefolp-defaults,fefolp.history.fs.cleaner.interval,1d
fefolp-defaults,fefolp.history.fs.cleaner.maxAge,2d
fefolp-env,fefolp_daemon_memory,10000
blo-site,blo.nodemanager.localizer.cache.target-size-mb,10240
blo-site,blo.nodemanager.localizer.cache.cleanup.interval-ms,300000
ams-env,metrics_collector_heapsize,512
fefolp,hbase_master_heapsize,1408
fefolp,hbase_regionserver_heapsize,512
fefolp,hbase_master_xmn_size,192
core-site,blolp.proxyuser.ambari.hosts,*
core-site,Hadoop.proxyuser.root.groups,*
core-site,Hadoop.proxyuser.root.hosts,*
blo-site,blo.scheduler.minimum-allocation-mb,1024
blolp-env,fefolp_heapsize,4096


Remark - after the last line - there are no space!



But the script prints only these lines (except the last line):



./test.sh
kafka-broker,log.retention.hours,12
kafka-broker,default.replication.factor,2
fefolp-defaults,fefolp.history.fs.cleaner.interval,1d
fefolp-defaults,fefolp.history.fs.cleaner.maxAge,2d
fefolp-env,fefolp_daemon_memory,10000
blo-site,blo.nodemanager.localizer.cache.target-size-mb,140
blo-site,blo.nodemanager.localizer.cache.cleanup.interval-ms,300
ams-env,metrics_collector_heapsize,51
fefolp,hbase_master_heapsize,1408
fefolp,hbase_regionserver_heapsize,542
fefolp,hbase_master_xmn_size,19
core-site,blolp.proxyuser.ambari.hosts,*
core-site,Hadoop.proxyuser.root.groups,*
core-site,Hadoop.proxyuser.root.hosts,*
blo-site,blo.scheduler.minimum-allocation-mb,1024


Why does this happen?










share|improve this question
























  • because the last text line is not complete. you need a n there.
    – Rui F Ribeiro
    Nov 18 at 11:50












  • do you have suggestion how to add n on the last line in the file , so I will put thus in the scritp
    – yael
    Nov 18 at 12:02










  • echo >> file as a poor mans solution.
    – Rui F Ribeiro
    Nov 18 at 12:36










  • Note POSIX defines "line" as a sequence of zero or more non- <newline> characters plus a terminating <newline> character. Your last line isn't even a line in this context.
    – Kamil Maciorowski
    Nov 18 at 16:24
















2














We use the following script:



more test.sh
#!/bin/bash

while read -r line
do

echo $line

done < /tmp/file


This is the file:



kafka-broker,log.retention.hours,12
kafka-broker,default.replication.factor,2
fefolp-defaults,fefolp.history.fs.cleaner.interval,1d
fefolp-defaults,fefolp.history.fs.cleaner.maxAge,2d
fefolp-env,fefolp_daemon_memory,10000
blo-site,blo.nodemanager.localizer.cache.target-size-mb,10240
blo-site,blo.nodemanager.localizer.cache.cleanup.interval-ms,300000
ams-env,metrics_collector_heapsize,512
fefolp,hbase_master_heapsize,1408
fefolp,hbase_regionserver_heapsize,512
fefolp,hbase_master_xmn_size,192
core-site,blolp.proxyuser.ambari.hosts,*
core-site,Hadoop.proxyuser.root.groups,*
core-site,Hadoop.proxyuser.root.hosts,*
blo-site,blo.scheduler.minimum-allocation-mb,1024
blolp-env,fefolp_heapsize,4096


Remark - after the last line - there are no space!



But the script prints only these lines (except the last line):



./test.sh
kafka-broker,log.retention.hours,12
kafka-broker,default.replication.factor,2
fefolp-defaults,fefolp.history.fs.cleaner.interval,1d
fefolp-defaults,fefolp.history.fs.cleaner.maxAge,2d
fefolp-env,fefolp_daemon_memory,10000
blo-site,blo.nodemanager.localizer.cache.target-size-mb,140
blo-site,blo.nodemanager.localizer.cache.cleanup.interval-ms,300
ams-env,metrics_collector_heapsize,51
fefolp,hbase_master_heapsize,1408
fefolp,hbase_regionserver_heapsize,542
fefolp,hbase_master_xmn_size,19
core-site,blolp.proxyuser.ambari.hosts,*
core-site,Hadoop.proxyuser.root.groups,*
core-site,Hadoop.proxyuser.root.hosts,*
blo-site,blo.scheduler.minimum-allocation-mb,1024


Why does this happen?










share|improve this question
























  • because the last text line is not complete. you need a n there.
    – Rui F Ribeiro
    Nov 18 at 11:50












  • do you have suggestion how to add n on the last line in the file , so I will put thus in the scritp
    – yael
    Nov 18 at 12:02










  • echo >> file as a poor mans solution.
    – Rui F Ribeiro
    Nov 18 at 12:36










  • Note POSIX defines "line" as a sequence of zero or more non- <newline> characters plus a terminating <newline> character. Your last line isn't even a line in this context.
    – Kamil Maciorowski
    Nov 18 at 16:24














2












2








2


1





We use the following script:



more test.sh
#!/bin/bash

while read -r line
do

echo $line

done < /tmp/file


This is the file:



kafka-broker,log.retention.hours,12
kafka-broker,default.replication.factor,2
fefolp-defaults,fefolp.history.fs.cleaner.interval,1d
fefolp-defaults,fefolp.history.fs.cleaner.maxAge,2d
fefolp-env,fefolp_daemon_memory,10000
blo-site,blo.nodemanager.localizer.cache.target-size-mb,10240
blo-site,blo.nodemanager.localizer.cache.cleanup.interval-ms,300000
ams-env,metrics_collector_heapsize,512
fefolp,hbase_master_heapsize,1408
fefolp,hbase_regionserver_heapsize,512
fefolp,hbase_master_xmn_size,192
core-site,blolp.proxyuser.ambari.hosts,*
core-site,Hadoop.proxyuser.root.groups,*
core-site,Hadoop.proxyuser.root.hosts,*
blo-site,blo.scheduler.minimum-allocation-mb,1024
blolp-env,fefolp_heapsize,4096


Remark - after the last line - there are no space!



But the script prints only these lines (except the last line):



./test.sh
kafka-broker,log.retention.hours,12
kafka-broker,default.replication.factor,2
fefolp-defaults,fefolp.history.fs.cleaner.interval,1d
fefolp-defaults,fefolp.history.fs.cleaner.maxAge,2d
fefolp-env,fefolp_daemon_memory,10000
blo-site,blo.nodemanager.localizer.cache.target-size-mb,140
blo-site,blo.nodemanager.localizer.cache.cleanup.interval-ms,300
ams-env,metrics_collector_heapsize,51
fefolp,hbase_master_heapsize,1408
fefolp,hbase_regionserver_heapsize,542
fefolp,hbase_master_xmn_size,19
core-site,blolp.proxyuser.ambari.hosts,*
core-site,Hadoop.proxyuser.root.groups,*
core-site,Hadoop.proxyuser.root.hosts,*
blo-site,blo.scheduler.minimum-allocation-mb,1024


Why does this happen?










share|improve this question















We use the following script:



more test.sh
#!/bin/bash

while read -r line
do

echo $line

done < /tmp/file


This is the file:



kafka-broker,log.retention.hours,12
kafka-broker,default.replication.factor,2
fefolp-defaults,fefolp.history.fs.cleaner.interval,1d
fefolp-defaults,fefolp.history.fs.cleaner.maxAge,2d
fefolp-env,fefolp_daemon_memory,10000
blo-site,blo.nodemanager.localizer.cache.target-size-mb,10240
blo-site,blo.nodemanager.localizer.cache.cleanup.interval-ms,300000
ams-env,metrics_collector_heapsize,512
fefolp,hbase_master_heapsize,1408
fefolp,hbase_regionserver_heapsize,512
fefolp,hbase_master_xmn_size,192
core-site,blolp.proxyuser.ambari.hosts,*
core-site,Hadoop.proxyuser.root.groups,*
core-site,Hadoop.proxyuser.root.hosts,*
blo-site,blo.scheduler.minimum-allocation-mb,1024
blolp-env,fefolp_heapsize,4096


Remark - after the last line - there are no space!



But the script prints only these lines (except the last line):



./test.sh
kafka-broker,log.retention.hours,12
kafka-broker,default.replication.factor,2
fefolp-defaults,fefolp.history.fs.cleaner.interval,1d
fefolp-defaults,fefolp.history.fs.cleaner.maxAge,2d
fefolp-env,fefolp_daemon_memory,10000
blo-site,blo.nodemanager.localizer.cache.target-size-mb,140
blo-site,blo.nodemanager.localizer.cache.cleanup.interval-ms,300
ams-env,metrics_collector_heapsize,51
fefolp,hbase_master_heapsize,1408
fefolp,hbase_regionserver_heapsize,542
fefolp,hbase_master_xmn_size,19
core-site,blolp.proxyuser.ambari.hosts,*
core-site,Hadoop.proxyuser.root.groups,*
core-site,Hadoop.proxyuser.root.hosts,*
blo-site,blo.scheduler.minimum-allocation-mb,1024


Why does this happen?







linux bash shell-script rhel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 at 23:09









Peter Mortensen

87758




87758










asked Nov 18 at 11:46









yael

2,42612159




2,42612159












  • because the last text line is not complete. you need a n there.
    – Rui F Ribeiro
    Nov 18 at 11:50












  • do you have suggestion how to add n on the last line in the file , so I will put thus in the scritp
    – yael
    Nov 18 at 12:02










  • echo >> file as a poor mans solution.
    – Rui F Ribeiro
    Nov 18 at 12:36










  • Note POSIX defines "line" as a sequence of zero or more non- <newline> characters plus a terminating <newline> character. Your last line isn't even a line in this context.
    – Kamil Maciorowski
    Nov 18 at 16:24


















  • because the last text line is not complete. you need a n there.
    – Rui F Ribeiro
    Nov 18 at 11:50












  • do you have suggestion how to add n on the last line in the file , so I will put thus in the scritp
    – yael
    Nov 18 at 12:02










  • echo >> file as a poor mans solution.
    – Rui F Ribeiro
    Nov 18 at 12:36










  • Note POSIX defines "line" as a sequence of zero or more non- <newline> characters plus a terminating <newline> character. Your last line isn't even a line in this context.
    – Kamil Maciorowski
    Nov 18 at 16:24
















because the last text line is not complete. you need a n there.
– Rui F Ribeiro
Nov 18 at 11:50






because the last text line is not complete. you need a n there.
– Rui F Ribeiro
Nov 18 at 11:50














do you have suggestion how to add n on the last line in the file , so I will put thus in the scritp
– yael
Nov 18 at 12:02




do you have suggestion how to add n on the last line in the file , so I will put thus in the scritp
– yael
Nov 18 at 12:02












echo >> file as a poor mans solution.
– Rui F Ribeiro
Nov 18 at 12:36




echo >> file as a poor mans solution.
– Rui F Ribeiro
Nov 18 at 12:36












Note POSIX defines "line" as a sequence of zero or more non- <newline> characters plus a terminating <newline> character. Your last line isn't even a line in this context.
– Kamil Maciorowski
Nov 18 at 16:24




Note POSIX defines "line" as a sequence of zero or more non- <newline> characters plus a terminating <newline> character. Your last line isn't even a line in this context.
– Kamil Maciorowski
Nov 18 at 16:24










1 Answer
1






active

oldest

votes


















6














Your input text contains an incomplete line as its last line. The last line is not terminated by a newline.



while IFS= read -r line || [ -n "$line" ]; do
printf '%sn' "$line"
done <file


The above loop will read unmodified lines (without stripping whitespaces or interpreting backslashed control sequences) from the file called file and print them to standard output.



When an incomplete line is read, read will fail, but $line will still contain data. The extra -n test will detect this so that the loop body is allowed to output the incomplete line. In the iteration after that, read will fail again and $line will be an empty string, thus terminating the loop.






share|improve this answer





















  • the script is only example of the problem , actually I set the echo in the script to show this issue , but what we need is how to set the last line with "n" with sed for example, since the the - file is already exists
    – yael
    Nov 18 at 12:26










  • so if need to add "n on the last line , then need sed/perl to add the "" for this solution
    – yael
    Nov 18 at 12:30












  • @yael You say in the question that you use a loop like the one you are showing. I gave a solution for how to fix that loop. It does not matter what the body of the loop looks like, $line will be each individual line of the file, including the last incomplete one. If you don't use a loop like this, what do you use?
    – Kusalananda
    Nov 18 at 12:37










  • ok , so maybe I will post another question for how to add "n" on the last line
    – yael
    Nov 18 at 12:38






  • 1




    @yael How to add a newline to the end of a file?
    – Kusalananda
    Nov 18 at 12:40











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482517%2fwhy-does-this-while-loop-not-recognize-the-last-line%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









6














Your input text contains an incomplete line as its last line. The last line is not terminated by a newline.



while IFS= read -r line || [ -n "$line" ]; do
printf '%sn' "$line"
done <file


The above loop will read unmodified lines (without stripping whitespaces or interpreting backslashed control sequences) from the file called file and print them to standard output.



When an incomplete line is read, read will fail, but $line will still contain data. The extra -n test will detect this so that the loop body is allowed to output the incomplete line. In the iteration after that, read will fail again and $line will be an empty string, thus terminating the loop.






share|improve this answer





















  • the script is only example of the problem , actually I set the echo in the script to show this issue , but what we need is how to set the last line with "n" with sed for example, since the the - file is already exists
    – yael
    Nov 18 at 12:26










  • so if need to add "n on the last line , then need sed/perl to add the "" for this solution
    – yael
    Nov 18 at 12:30












  • @yael You say in the question that you use a loop like the one you are showing. I gave a solution for how to fix that loop. It does not matter what the body of the loop looks like, $line will be each individual line of the file, including the last incomplete one. If you don't use a loop like this, what do you use?
    – Kusalananda
    Nov 18 at 12:37










  • ok , so maybe I will post another question for how to add "n" on the last line
    – yael
    Nov 18 at 12:38






  • 1




    @yael How to add a newline to the end of a file?
    – Kusalananda
    Nov 18 at 12:40
















6














Your input text contains an incomplete line as its last line. The last line is not terminated by a newline.



while IFS= read -r line || [ -n "$line" ]; do
printf '%sn' "$line"
done <file


The above loop will read unmodified lines (without stripping whitespaces or interpreting backslashed control sequences) from the file called file and print them to standard output.



When an incomplete line is read, read will fail, but $line will still contain data. The extra -n test will detect this so that the loop body is allowed to output the incomplete line. In the iteration after that, read will fail again and $line will be an empty string, thus terminating the loop.






share|improve this answer





















  • the script is only example of the problem , actually I set the echo in the script to show this issue , but what we need is how to set the last line with "n" with sed for example, since the the - file is already exists
    – yael
    Nov 18 at 12:26










  • so if need to add "n on the last line , then need sed/perl to add the "" for this solution
    – yael
    Nov 18 at 12:30












  • @yael You say in the question that you use a loop like the one you are showing. I gave a solution for how to fix that loop. It does not matter what the body of the loop looks like, $line will be each individual line of the file, including the last incomplete one. If you don't use a loop like this, what do you use?
    – Kusalananda
    Nov 18 at 12:37










  • ok , so maybe I will post another question for how to add "n" on the last line
    – yael
    Nov 18 at 12:38






  • 1




    @yael How to add a newline to the end of a file?
    – Kusalananda
    Nov 18 at 12:40














6












6








6






Your input text contains an incomplete line as its last line. The last line is not terminated by a newline.



while IFS= read -r line || [ -n "$line" ]; do
printf '%sn' "$line"
done <file


The above loop will read unmodified lines (without stripping whitespaces or interpreting backslashed control sequences) from the file called file and print them to standard output.



When an incomplete line is read, read will fail, but $line will still contain data. The extra -n test will detect this so that the loop body is allowed to output the incomplete line. In the iteration after that, read will fail again and $line will be an empty string, thus terminating the loop.






share|improve this answer












Your input text contains an incomplete line as its last line. The last line is not terminated by a newline.



while IFS= read -r line || [ -n "$line" ]; do
printf '%sn' "$line"
done <file


The above loop will read unmodified lines (without stripping whitespaces or interpreting backslashed control sequences) from the file called file and print them to standard output.



When an incomplete line is read, read will fail, but $line will still contain data. The extra -n test will detect this so that the loop body is allowed to output the incomplete line. In the iteration after that, read will fail again and $line will be an empty string, thus terminating the loop.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 18 at 12:17









Kusalananda

121k16229372




121k16229372












  • the script is only example of the problem , actually I set the echo in the script to show this issue , but what we need is how to set the last line with "n" with sed for example, since the the - file is already exists
    – yael
    Nov 18 at 12:26










  • so if need to add "n on the last line , then need sed/perl to add the "" for this solution
    – yael
    Nov 18 at 12:30












  • @yael You say in the question that you use a loop like the one you are showing. I gave a solution for how to fix that loop. It does not matter what the body of the loop looks like, $line will be each individual line of the file, including the last incomplete one. If you don't use a loop like this, what do you use?
    – Kusalananda
    Nov 18 at 12:37










  • ok , so maybe I will post another question for how to add "n" on the last line
    – yael
    Nov 18 at 12:38






  • 1




    @yael How to add a newline to the end of a file?
    – Kusalananda
    Nov 18 at 12:40


















  • the script is only example of the problem , actually I set the echo in the script to show this issue , but what we need is how to set the last line with "n" with sed for example, since the the - file is already exists
    – yael
    Nov 18 at 12:26










  • so if need to add "n on the last line , then need sed/perl to add the "" for this solution
    – yael
    Nov 18 at 12:30












  • @yael You say in the question that you use a loop like the one you are showing. I gave a solution for how to fix that loop. It does not matter what the body of the loop looks like, $line will be each individual line of the file, including the last incomplete one. If you don't use a loop like this, what do you use?
    – Kusalananda
    Nov 18 at 12:37










  • ok , so maybe I will post another question for how to add "n" on the last line
    – yael
    Nov 18 at 12:38






  • 1




    @yael How to add a newline to the end of a file?
    – Kusalananda
    Nov 18 at 12:40
















the script is only example of the problem , actually I set the echo in the script to show this issue , but what we need is how to set the last line with "n" with sed for example, since the the - file is already exists
– yael
Nov 18 at 12:26




the script is only example of the problem , actually I set the echo in the script to show this issue , but what we need is how to set the last line with "n" with sed for example, since the the - file is already exists
– yael
Nov 18 at 12:26












so if need to add "n on the last line , then need sed/perl to add the "" for this solution
– yael
Nov 18 at 12:30






so if need to add "n on the last line , then need sed/perl to add the "" for this solution
– yael
Nov 18 at 12:30














@yael You say in the question that you use a loop like the one you are showing. I gave a solution for how to fix that loop. It does not matter what the body of the loop looks like, $line will be each individual line of the file, including the last incomplete one. If you don't use a loop like this, what do you use?
– Kusalananda
Nov 18 at 12:37




@yael You say in the question that you use a loop like the one you are showing. I gave a solution for how to fix that loop. It does not matter what the body of the loop looks like, $line will be each individual line of the file, including the last incomplete one. If you don't use a loop like this, what do you use?
– Kusalananda
Nov 18 at 12:37












ok , so maybe I will post another question for how to add "n" on the last line
– yael
Nov 18 at 12:38




ok , so maybe I will post another question for how to add "n" on the last line
– yael
Nov 18 at 12:38




1




1




@yael How to add a newline to the end of a file?
– Kusalananda
Nov 18 at 12:40




@yael How to add a newline to the end of a file?
– Kusalananda
Nov 18 at 12:40


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482517%2fwhy-does-this-while-loop-not-recognize-the-last-line%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

AnyDesk - Fatal Program Failure

How to calibrate 16:9 built-in touch-screen to a 4:3 resolution?

QoS: MAC-Priority for clients behind a repeater