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;
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
add a comment |
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
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 toawk
,sed
or another tool avoidingwhile
(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
add a comment |
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
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
shell sh
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 toawk
,sed
or another tool avoidingwhile
(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
add a comment |
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 toawk
,sed
or another tool avoidingwhile
(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
add a comment |
1 Answer
1
active
oldest
votes
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
)
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
Justprintf "%sn" "$logs"
orwhile ....; do ... done <<<"$logs"
.
– Kamil Cuk
Mar 24 at 20:42
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
)
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
Justprintf "%sn" "$logs"
orwhile ....; do ... done <<<"$logs"
.
– Kamil Cuk
Mar 24 at 20:42
add a comment |
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
)
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
Justprintf "%sn" "$logs"
orwhile ....; do ... done <<<"$logs"
.
– Kamil Cuk
Mar 24 at 20:42
add a comment |
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
)
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
)
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
Justprintf "%sn" "$logs"
orwhile ....; do ... done <<<"$logs"
.
– Kamil Cuk
Mar 24 at 20:42
add a comment |
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
Justprintf "%sn" "$logs"
orwhile ....; 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 avoidingwhile
(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