Shell variable value passing to subshell?Pipe stdout to another process's file descriptorCalling shell commands from RubyCheck if a directory exists in a shell scriptHow do I prompt for Yes/No/Cancel input in a Linux shell script?How to use SSH to run a shell script on a remote machine?In the shell, what does “ 2>&1 ” mean?How to declare and use boolean variables in shell script?How to check if a variable is set in Bash?How to concatenate string variables in BashHow to set a variable to the output of a command in Bash?Check existence of input argument in a Bash shell script
What does "Scientists rise up against statistical significance" mean? (Comment in Nature)
Why did the EU agree to delay the Brexit deadline?
What should you do when eye contact makes your subordinate uncomfortable?
What are some good ways to treat frozen vegetables such that they behave like fresh vegetables when stir frying them?
What are the advantages of simplicial model categories over non-simplicial ones?
Yosemite Fire Rings - What to Expect?
Can a College of Swords bard use a Blade Flourish option on an opportunity attack provoked by their own Dissonant Whispers spell?
Why is so much work done on numerical verification of the Riemann Hypothesis?
Electoral considerations aside, what are potential benefits, for the US, of policy changes proposed by the tweet recognizing Golan annexation?
What is the evidence for the "tyranny of the majority problem" in a direct democracy context?
What is Cash Advance APR?
The probability of Bus A arriving before Bus B
Does an advisor owe his/her student anything? Will an advisor keep a PhD student only out of pity?
Temporarily disable WLAN internet access for children, but allow it for adults
How much character growth crosses the line into breaking the character
What exact color does ozone gas have?
The IT department bottlenecks progress. How should I handle this?
Can I visit Japan without a visa?
A social experiment. What is the worst that can happen?
Limits and Infinite Integration by Parts
Why Shazam when there is already Superman?
Can I still be respawned if I die by falling off the map?
Is there a way to get `mathscr' with lower case letters in pdfLaTeX?
Strong empirical falsification of quantum mechanics based on vacuum energy density
Shell variable value passing to subshell?
Pipe stdout to another process's file descriptorCalling shell commands from RubyCheck if a directory exists in a shell scriptHow do I prompt for Yes/No/Cancel input in a Linux shell script?How to use SSH to run a shell script on a remote machine?In the shell, what does “ 2>&1 ” mean?How to declare and use boolean variables in shell script?How to check if a variable is set in Bash?How to concatenate string variables in BashHow to set a variable to the output of a command in Bash?Check existence of input argument in a Bash shell script
I am writing script in shell and it's something like:
temp=0
while true; do
case "a" in
a) temp=5; ;;
esac
break
done | echo "$temp"
( temp value is condition for if in subshell and | (pipe) is needed as stdout redirect to stdin)
and I need in temp 5 but I got 0 in echo. Any way to preserve value inside case (posixly correct without tempfile)?
shell
New contributor
add a comment |
I am writing script in shell and it's something like:
temp=0
while true; do
case "a" in
a) temp=5; ;;
esac
break
done | echo "$temp"
( temp value is condition for if in subshell and | (pipe) is needed as stdout redirect to stdin)
and I need in temp 5 but I got 0 in echo. Any way to preserve value inside case (posixly correct without tempfile)?
shell
New contributor
1
Inwhile true; do case "a" in a) temp=5; ;; esac; break; done | echo "$temp"
, the$temp
on the right hand side of the pipe is evaluated before the while loop is executed.
– William Pursell
yesterday
1
Please show what you're actually trying to do in the right side of the pipe. Piping toecho
doesn't make sense sinceecho
doesn't read from stdin. We need to know what you're really doing because the answers will vary based on what you really need.
– John Kugelman
yesterday
add a comment |
I am writing script in shell and it's something like:
temp=0
while true; do
case "a" in
a) temp=5; ;;
esac
break
done | echo "$temp"
( temp value is condition for if in subshell and | (pipe) is needed as stdout redirect to stdin)
and I need in temp 5 but I got 0 in echo. Any way to preserve value inside case (posixly correct without tempfile)?
shell
New contributor
I am writing script in shell and it's something like:
temp=0
while true; do
case "a" in
a) temp=5; ;;
esac
break
done | echo "$temp"
( temp value is condition for if in subshell and | (pipe) is needed as stdout redirect to stdin)
and I need in temp 5 but I got 0 in echo. Any way to preserve value inside case (posixly correct without tempfile)?
shell
shell
New contributor
New contributor
edited yesterday
Darman41
New contributor
asked yesterday
Darman41Darman41
11
11
New contributor
New contributor
1
Inwhile true; do case "a" in a) temp=5; ;; esac; break; done | echo "$temp"
, the$temp
on the right hand side of the pipe is evaluated before the while loop is executed.
– William Pursell
yesterday
1
Please show what you're actually trying to do in the right side of the pipe. Piping toecho
doesn't make sense sinceecho
doesn't read from stdin. We need to know what you're really doing because the answers will vary based on what you really need.
– John Kugelman
yesterday
add a comment |
1
Inwhile true; do case "a" in a) temp=5; ;; esac; break; done | echo "$temp"
, the$temp
on the right hand side of the pipe is evaluated before the while loop is executed.
– William Pursell
yesterday
1
Please show what you're actually trying to do in the right side of the pipe. Piping toecho
doesn't make sense sinceecho
doesn't read from stdin. We need to know what you're really doing because the answers will vary based on what you really need.
– John Kugelman
yesterday
1
1
In
while true; do case "a" in a) temp=5; ;; esac; break; done | echo "$temp"
, the $temp
on the right hand side of the pipe is evaluated before the while loop is executed.– William Pursell
yesterday
In
while true; do case "a" in a) temp=5; ;; esac; break; done | echo "$temp"
, the $temp
on the right hand side of the pipe is evaluated before the while loop is executed.– William Pursell
yesterday
1
1
Please show what you're actually trying to do in the right side of the pipe. Piping to
echo
doesn't make sense since echo
doesn't read from stdin. We need to know what you're really doing because the answers will vary based on what you really need.– John Kugelman
yesterday
Please show what you're actually trying to do in the right side of the pipe. Piping to
echo
doesn't make sense since echo
doesn't read from stdin. We need to know what you're really doing because the answers will vary based on what you really need.– John Kugelman
yesterday
add a comment |
1 Answer
1
active
oldest
votes
Do you want it like this? Prints 5 and 0.
#!/bin/sh
temp=0
while true; do
case "a" in
a) temp=5; echo "$temp" ;;
esac
break
done | cat
echo "temp unchanged because of subshell $temp"
Would this help you, filtering out your status?
I still don't get why you need a pipe and how you expect the other command you pipe into to react on a status. It's just not the way it works. You evaluate conditions in your script and call commands accordingly.
#!/bin/sh
filter()
data=''
while read line; do
case "$line" in
temp=5) temp=5 ;;
*) data=$(printf '%sn' "$data"; printf '%sn' "$line") ;;
esac
done
if [ "$temp" -eq 5 ]; then
echo "do this 5 with data"
else
echo "do that other with data"
echo "$data"
fi
temp=0
while true; do
case "a" in
a) temp=5; printf 'temp=%sn' "$temp"; for i in $(seq 1 30); do echo "30 lines"; done ;;
esac
break
done | filter
echo "temp unchanged because of subshell $temp"
New contributor
Unfortunately not, I need to stdout and stdin unchanged. Point is to pass variable and stdout to subshell
– Darman41
yesterday
stdout and stdin unchanged? So no pipe? Because in your question you say you want to pipe?
– xenoson
yesterday
I need to pipe stdout (30 lines) to stdin (30 lines) and pass that variable, which decide, what should happen with those lines
– Darman41
yesterday
Maybe use another file descriptor.
– xenoson
yesterday
sadly I don't have those 20 lines in file and I am not allowed to create temporarily file
– Darman41
yesterday
|
show 2 more comments
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
);
);
Darman41 is a new contributor. Be nice, and check out our Code of Conduct.
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%2f55280880%2fshell-variable-value-passing-to-subshell%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
Do you want it like this? Prints 5 and 0.
#!/bin/sh
temp=0
while true; do
case "a" in
a) temp=5; echo "$temp" ;;
esac
break
done | cat
echo "temp unchanged because of subshell $temp"
Would this help you, filtering out your status?
I still don't get why you need a pipe and how you expect the other command you pipe into to react on a status. It's just not the way it works. You evaluate conditions in your script and call commands accordingly.
#!/bin/sh
filter()
data=''
while read line; do
case "$line" in
temp=5) temp=5 ;;
*) data=$(printf '%sn' "$data"; printf '%sn' "$line") ;;
esac
done
if [ "$temp" -eq 5 ]; then
echo "do this 5 with data"
else
echo "do that other with data"
echo "$data"
fi
temp=0
while true; do
case "a" in
a) temp=5; printf 'temp=%sn' "$temp"; for i in $(seq 1 30); do echo "30 lines"; done ;;
esac
break
done | filter
echo "temp unchanged because of subshell $temp"
New contributor
Unfortunately not, I need to stdout and stdin unchanged. Point is to pass variable and stdout to subshell
– Darman41
yesterday
stdout and stdin unchanged? So no pipe? Because in your question you say you want to pipe?
– xenoson
yesterday
I need to pipe stdout (30 lines) to stdin (30 lines) and pass that variable, which decide, what should happen with those lines
– Darman41
yesterday
Maybe use another file descriptor.
– xenoson
yesterday
sadly I don't have those 20 lines in file and I am not allowed to create temporarily file
– Darman41
yesterday
|
show 2 more comments
Do you want it like this? Prints 5 and 0.
#!/bin/sh
temp=0
while true; do
case "a" in
a) temp=5; echo "$temp" ;;
esac
break
done | cat
echo "temp unchanged because of subshell $temp"
Would this help you, filtering out your status?
I still don't get why you need a pipe and how you expect the other command you pipe into to react on a status. It's just not the way it works. You evaluate conditions in your script and call commands accordingly.
#!/bin/sh
filter()
data=''
while read line; do
case "$line" in
temp=5) temp=5 ;;
*) data=$(printf '%sn' "$data"; printf '%sn' "$line") ;;
esac
done
if [ "$temp" -eq 5 ]; then
echo "do this 5 with data"
else
echo "do that other with data"
echo "$data"
fi
temp=0
while true; do
case "a" in
a) temp=5; printf 'temp=%sn' "$temp"; for i in $(seq 1 30); do echo "30 lines"; done ;;
esac
break
done | filter
echo "temp unchanged because of subshell $temp"
New contributor
Unfortunately not, I need to stdout and stdin unchanged. Point is to pass variable and stdout to subshell
– Darman41
yesterday
stdout and stdin unchanged? So no pipe? Because in your question you say you want to pipe?
– xenoson
yesterday
I need to pipe stdout (30 lines) to stdin (30 lines) and pass that variable, which decide, what should happen with those lines
– Darman41
yesterday
Maybe use another file descriptor.
– xenoson
yesterday
sadly I don't have those 20 lines in file and I am not allowed to create temporarily file
– Darman41
yesterday
|
show 2 more comments
Do you want it like this? Prints 5 and 0.
#!/bin/sh
temp=0
while true; do
case "a" in
a) temp=5; echo "$temp" ;;
esac
break
done | cat
echo "temp unchanged because of subshell $temp"
Would this help you, filtering out your status?
I still don't get why you need a pipe and how you expect the other command you pipe into to react on a status. It's just not the way it works. You evaluate conditions in your script and call commands accordingly.
#!/bin/sh
filter()
data=''
while read line; do
case "$line" in
temp=5) temp=5 ;;
*) data=$(printf '%sn' "$data"; printf '%sn' "$line") ;;
esac
done
if [ "$temp" -eq 5 ]; then
echo "do this 5 with data"
else
echo "do that other with data"
echo "$data"
fi
temp=0
while true; do
case "a" in
a) temp=5; printf 'temp=%sn' "$temp"; for i in $(seq 1 30); do echo "30 lines"; done ;;
esac
break
done | filter
echo "temp unchanged because of subshell $temp"
New contributor
Do you want it like this? Prints 5 and 0.
#!/bin/sh
temp=0
while true; do
case "a" in
a) temp=5; echo "$temp" ;;
esac
break
done | cat
echo "temp unchanged because of subshell $temp"
Would this help you, filtering out your status?
I still don't get why you need a pipe and how you expect the other command you pipe into to react on a status. It's just not the way it works. You evaluate conditions in your script and call commands accordingly.
#!/bin/sh
filter()
data=''
while read line; do
case "$line" in
temp=5) temp=5 ;;
*) data=$(printf '%sn' "$data"; printf '%sn' "$line") ;;
esac
done
if [ "$temp" -eq 5 ]; then
echo "do this 5 with data"
else
echo "do that other with data"
echo "$data"
fi
temp=0
while true; do
case "a" in
a) temp=5; printf 'temp=%sn' "$temp"; for i in $(seq 1 30); do echo "30 lines"; done ;;
esac
break
done | filter
echo "temp unchanged because of subshell $temp"
New contributor
edited yesterday
New contributor
answered yesterday
xenosonxenoson
533
533
New contributor
New contributor
Unfortunately not, I need to stdout and stdin unchanged. Point is to pass variable and stdout to subshell
– Darman41
yesterday
stdout and stdin unchanged? So no pipe? Because in your question you say you want to pipe?
– xenoson
yesterday
I need to pipe stdout (30 lines) to stdin (30 lines) and pass that variable, which decide, what should happen with those lines
– Darman41
yesterday
Maybe use another file descriptor.
– xenoson
yesterday
sadly I don't have those 20 lines in file and I am not allowed to create temporarily file
– Darman41
yesterday
|
show 2 more comments
Unfortunately not, I need to stdout and stdin unchanged. Point is to pass variable and stdout to subshell
– Darman41
yesterday
stdout and stdin unchanged? So no pipe? Because in your question you say you want to pipe?
– xenoson
yesterday
I need to pipe stdout (30 lines) to stdin (30 lines) and pass that variable, which decide, what should happen with those lines
– Darman41
yesterday
Maybe use another file descriptor.
– xenoson
yesterday
sadly I don't have those 20 lines in file and I am not allowed to create temporarily file
– Darman41
yesterday
Unfortunately not, I need to stdout and stdin unchanged. Point is to pass variable and stdout to subshell
– Darman41
yesterday
Unfortunately not, I need to stdout and stdin unchanged. Point is to pass variable and stdout to subshell
– Darman41
yesterday
stdout and stdin unchanged? So no pipe? Because in your question you say you want to pipe?
– xenoson
yesterday
stdout and stdin unchanged? So no pipe? Because in your question you say you want to pipe?
– xenoson
yesterday
I need to pipe stdout (30 lines) to stdin (30 lines) and pass that variable, which decide, what should happen with those lines
– Darman41
yesterday
I need to pipe stdout (30 lines) to stdin (30 lines) and pass that variable, which decide, what should happen with those lines
– Darman41
yesterday
Maybe use another file descriptor.
– xenoson
yesterday
Maybe use another file descriptor.
– xenoson
yesterday
sadly I don't have those 20 lines in file and I am not allowed to create temporarily file
– Darman41
yesterday
sadly I don't have those 20 lines in file and I am not allowed to create temporarily file
– Darman41
yesterday
|
show 2 more comments
Darman41 is a new contributor. Be nice, and check out our Code of Conduct.
Darman41 is a new contributor. Be nice, and check out our Code of Conduct.
Darman41 is a new contributor. Be nice, and check out our Code of Conduct.
Darman41 is a new contributor. Be nice, and check out our Code of Conduct.
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%2f55280880%2fshell-variable-value-passing-to-subshell%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
1
In
while true; do case "a" in a) temp=5; ;; esac; break; done | echo "$temp"
, the$temp
on the right hand side of the pipe is evaluated before the while loop is executed.– William Pursell
yesterday
1
Please show what you're actually trying to do in the right side of the pipe. Piping to
echo
doesn't make sense sinceecho
doesn't read from stdin. We need to know what you're really doing because the answers will vary based on what you really need.– John Kugelman
yesterday