Read variable line by line in shell (dash included)Check if a directory exists in a shell scriptHow do I prompt for Yes/No/Cancel input in a Linux shell script?Shell command to sum integers, one per line?In the shell, what does “ 2>&1 ” mean?How to count all the lines of code in a directory recursively?How to declare and use boolean variables in shell script?How to concatenate string variables in BashDelete lines in a text file that contain a specific stringCheck existence of input argument in a Bash shell scriptHow to read a file into a variable in shell?

What does $!# mean in Shell scripting?

Can I connect my older mathematica front-end to the free wolfram engine?

Ethical issue - how can I better document what is happening?

Can a person survive on blood in place of water?

Can a British citizen living in France vote in both France and Britain in the European Elections?

Is it possible to remotely hack the GPS system and disable GPS service worldwide?

NIntegrate doesn't evaluate

What is a fully qualified name?

Is Jon Snow the last of his House?

Ingress filtering on edge routers and performance concerns

How to ignore kerning of underbrace in math mode

Why did Jon Snow do this immoral act if he is so honorable?

Can I summon an otherworldly creature with the Gate spell without knowing its true name?

Apt - strange requests to d16r8ew072anqo.cloudfront.net:80

Why does Mjolnir fall down in Age of Ultron but not in Endgame?

Does this strict reading of the rules allow both Extra Attack and the Thirsting Blade warlock invocation to be used together?

Can my floppy disk still work without a shutter spring?

Is it truly impossible to tell what a CPU is doing?

Did this character show any indication of wanting to rule before S8E6?

Have 1.5% of all nuclear reactors ever built melted down?

How to let other coworkers know that I don't share my coworker's political views?

Where's this lookout in Nova Scotia?

Count Even Digits In Number

Why do Russians almost not use verbs of possession akin to "have"?



Read variable line by line in shell (dash included)


Check if a directory exists in a shell scriptHow do I prompt for Yes/No/Cancel input in a Linux shell script?Shell command to sum integers, one per line?In the shell, what does “ 2>&1 ” mean?How to count all the lines of code in a directory recursively?How to declare and use boolean variables in shell script?How to concatenate string variables in BashDelete lines in a text file that contain a specific stringCheck existence of input argument in a Bash shell scriptHow to read a file into a variable in shell?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















i have a variable with stored logs. What i want is to read every line of the variable containing logs and keep that line or remove base on some filtering stuffs.



The problem is that my code is working on bash, but not working on dash.



this is my code:



filtered_logs=""
while IFS= read -r line
do
...(store line to $filterered_logs if it comes throught filter )
done <<< "$logs"
logs="$filtered_logs"


this code is working in bash. But ' done <<< "$logs" ' is not working in dash (since is default sh in ubuntu). It's a homework and i need, that it works on every shell possible.



What i tried was:



filtered_logs=""
echo "$logs" |
while IFS= read -r line
do
...(store line to $filterered_logs if it comes throught filter )
done
logs="$filtered_logs"


But if i store something in from while cycle to $filtered_logs, it's not working. And i can't access this while cycle with my debugger too. (I think that the whole while cycle is whole new process since i run it with |.



My question is how to make it works, please. Thank you










share|improve this question






















  • man echo ; man cat ; google "shell input redirection"

    – jhnc
    Mar 24 at 2:34











  • Every shell possible seems a to an unlimited assignment. Do you need to use the while-construction or can you switch to awk, sed or another tool avoiding while (which has more problems like performance and skipping the last line when it is not ending with a newline) ?

    – Walter A
    Mar 24 at 11:16











  • yes, i can use both awk and sed. And it seems that other classmates used that. Sadly i didn't learned how to work with that, so i ended with this.

    – Ruuza
    Mar 24 at 12:16

















0















i have a variable with stored logs. What i want is to read every line of the variable containing logs and keep that line or remove base on some filtering stuffs.



The problem is that my code is working on bash, but not working on dash.



this is my code:



filtered_logs=""
while IFS= read -r line
do
...(store line to $filterered_logs if it comes throught filter )
done <<< "$logs"
logs="$filtered_logs"


this code is working in bash. But ' done <<< "$logs" ' is not working in dash (since is default sh in ubuntu). It's a homework and i need, that it works on every shell possible.



What i tried was:



filtered_logs=""
echo "$logs" |
while IFS= read -r line
do
...(store line to $filterered_logs if it comes throught filter )
done
logs="$filtered_logs"


But if i store something in from while cycle to $filtered_logs, it's not working. And i can't access this while cycle with my debugger too. (I think that the whole while cycle is whole new process since i run it with |.



My question is how to make it works, please. Thank you










share|improve this question






















  • man echo ; man cat ; google "shell input redirection"

    – jhnc
    Mar 24 at 2:34











  • Every shell possible seems a to an unlimited assignment. Do you need to use the while-construction or can you switch to awk, sed or another tool avoiding while (which has more problems like performance and skipping the last line when it is not ending with a newline) ?

    – Walter A
    Mar 24 at 11:16











  • yes, i can use both awk and sed. And it seems that other classmates used that. Sadly i didn't learned how to work with that, so i ended with this.

    – Ruuza
    Mar 24 at 12:16













0












0








0








i have a variable with stored logs. What i want is to read every line of the variable containing logs and keep that line or remove base on some filtering stuffs.



The problem is that my code is working on bash, but not working on dash.



this is my code:



filtered_logs=""
while IFS= read -r line
do
...(store line to $filterered_logs if it comes throught filter )
done <<< "$logs"
logs="$filtered_logs"


this code is working in bash. But ' done <<< "$logs" ' is not working in dash (since is default sh in ubuntu). It's a homework and i need, that it works on every shell possible.



What i tried was:



filtered_logs=""
echo "$logs" |
while IFS= read -r line
do
...(store line to $filterered_logs if it comes throught filter )
done
logs="$filtered_logs"


But if i store something in from while cycle to $filtered_logs, it's not working. And i can't access this while cycle with my debugger too. (I think that the whole while cycle is whole new process since i run it with |.



My question is how to make it works, please. Thank you










share|improve this question














i have a variable with stored logs. What i want is to read every line of the variable containing logs and keep that line or remove base on some filtering stuffs.



The problem is that my code is working on bash, but not working on dash.



this is my code:



filtered_logs=""
while IFS= read -r line
do
...(store line to $filterered_logs if it comes throught filter )
done <<< "$logs"
logs="$filtered_logs"


this code is working in bash. But ' done <<< "$logs" ' is not working in dash (since is default sh in ubuntu). It's a homework and i need, that it works on every shell possible.



What i tried was:



filtered_logs=""
echo "$logs" |
while IFS= read -r line
do
...(store line to $filterered_logs if it comes throught filter )
done
logs="$filtered_logs"


But if i store something in from while cycle to $filtered_logs, it's not working. And i can't access this while cycle with my debugger too. (I think that the whole while cycle is whole new process since i run it with |.



My question is how to make it works, please. Thank you







shell sh






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 24 at 2:07









RuuzaRuuza

32




32












  • man echo ; man cat ; google "shell input redirection"

    – jhnc
    Mar 24 at 2:34











  • Every shell possible seems a to an unlimited assignment. Do you need to use the while-construction or can you switch to awk, sed or another tool avoiding while (which has more problems like performance and skipping the last line when it is not ending with a newline) ?

    – Walter A
    Mar 24 at 11:16











  • yes, i can use both awk and sed. And it seems that other classmates used that. Sadly i didn't learned how to work with that, so i ended with this.

    – Ruuza
    Mar 24 at 12:16

















  • man echo ; man cat ; google "shell input redirection"

    – jhnc
    Mar 24 at 2:34











  • Every shell possible seems a to an unlimited assignment. Do you need to use the while-construction or can you switch to awk, sed or another tool avoiding while (which has more problems like performance and skipping the last line when it is not ending with a newline) ?

    – Walter A
    Mar 24 at 11:16











  • yes, i can use both awk and sed. And it seems that other classmates used that. Sadly i didn't learned how to work with that, so i ended with this.

    – Ruuza
    Mar 24 at 12:16
















man echo ; man cat ; google "shell input redirection"

– jhnc
Mar 24 at 2:34





man echo ; man cat ; google "shell input redirection"

– jhnc
Mar 24 at 2:34













Every shell possible seems a to an unlimited assignment. Do you need to use the while-construction or can you switch to awk, sed or another tool avoiding while (which has more problems like performance and skipping the last line when it is not ending with a newline) ?

– Walter A
Mar 24 at 11:16





Every shell possible seems a to an unlimited assignment. Do you need to use the while-construction or can you switch to awk, sed or another tool avoiding while (which has more problems like performance and skipping the last line when it is not ending with a newline) ?

– Walter A
Mar 24 at 11:16













yes, i can use both awk and sed. And it seems that other classmates used that. Sadly i didn't learned how to work with that, so i ended with this.

– Ruuza
Mar 24 at 12:16





yes, i can use both awk and sed. And it seems that other classmates used that. Sadly i didn't learned how to work with that, so i ended with this.

– Ruuza
Mar 24 at 12:16












1 Answer
1






active

oldest

votes


















0














It's easy to use clean posix api in your case:



logs=$(
printf "%s" "$logs" |
while IFS= read -r line
do
echo "store this to logs"
done
)





share|improve this answer























  • printf "%s" "$(echo "$logs" | tail -3)" will show that the last newline is missing.

    – Walter A
    Mar 24 at 10:24











  • Thank you so much, it works!. Yes, the last line is missing. I append a newline to variable as workaround. Do you know about better solution?

    – Ruuza
    Mar 24 at 14:08











  • Just printf "%sn" "$logs" or while ....; do ... done <<<"$logs".

    – Kamil Cuk
    Mar 24 at 20:42











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f55320122%2fread-variable-line-by-line-in-shell-dash-included%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









0














It's easy to use clean posix api in your case:



logs=$(
printf "%s" "$logs" |
while IFS= read -r line
do
echo "store this to logs"
done
)





share|improve this answer























  • printf "%s" "$(echo "$logs" | tail -3)" will show that the last newline is missing.

    – Walter A
    Mar 24 at 10:24











  • Thank you so much, it works!. Yes, the last line is missing. I append a newline to variable as workaround. Do you know about better solution?

    – Ruuza
    Mar 24 at 14:08











  • Just printf "%sn" "$logs" or while ....; do ... done <<<"$logs".

    – Kamil Cuk
    Mar 24 at 20:42















0














It's easy to use clean posix api in your case:



logs=$(
printf "%s" "$logs" |
while IFS= read -r line
do
echo "store this to logs"
done
)





share|improve this answer























  • printf "%s" "$(echo "$logs" | tail -3)" will show that the last newline is missing.

    – Walter A
    Mar 24 at 10:24











  • Thank you so much, it works!. Yes, the last line is missing. I append a newline to variable as workaround. Do you know about better solution?

    – Ruuza
    Mar 24 at 14:08











  • Just printf "%sn" "$logs" or while ....; do ... done <<<"$logs".

    – Kamil Cuk
    Mar 24 at 20:42













0












0








0







It's easy to use clean posix api in your case:



logs=$(
printf "%s" "$logs" |
while IFS= read -r line
do
echo "store this to logs"
done
)





share|improve this answer













It's easy to use clean posix api in your case:



logs=$(
printf "%s" "$logs" |
while IFS= read -r line
do
echo "store this to logs"
done
)






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 24 at 3:23









Kamil CukKamil Cuk

15.3k2633




15.3k2633












  • printf "%s" "$(echo "$logs" | tail -3)" will show that the last newline is missing.

    – Walter A
    Mar 24 at 10:24











  • Thank you so much, it works!. Yes, the last line is missing. I append a newline to variable as workaround. Do you know about better solution?

    – Ruuza
    Mar 24 at 14:08











  • Just printf "%sn" "$logs" or while ....; do ... done <<<"$logs".

    – Kamil Cuk
    Mar 24 at 20:42

















  • printf "%s" "$(echo "$logs" | tail -3)" will show that the last newline is missing.

    – Walter A
    Mar 24 at 10:24











  • Thank you so much, it works!. Yes, the last line is missing. I append a newline to variable as workaround. Do you know about better solution?

    – Ruuza
    Mar 24 at 14:08











  • Just printf "%sn" "$logs" or while ....; do ... done <<<"$logs".

    – Kamil Cuk
    Mar 24 at 20:42
















printf "%s" "$(echo "$logs" | tail -3)" will show that the last newline is missing.

– Walter A
Mar 24 at 10:24





printf "%s" "$(echo "$logs" | tail -3)" will show that the last newline is missing.

– Walter A
Mar 24 at 10:24













Thank you so much, it works!. Yes, the last line is missing. I append a newline to variable as workaround. Do you know about better solution?

– Ruuza
Mar 24 at 14:08





Thank you so much, it works!. Yes, the last line is missing. I append a newline to variable as workaround. Do you know about better solution?

– Ruuza
Mar 24 at 14:08













Just printf "%sn" "$logs" or while ....; do ... done <<<"$logs".

– Kamil Cuk
Mar 24 at 20:42





Just printf "%sn" "$logs" or while ....; do ... done <<<"$logs".

– Kamil Cuk
Mar 24 at 20:42



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • 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%2fstackoverflow.com%2fquestions%2f55320122%2fread-variable-line-by-line-in-shell-dash-included%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

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?