Variable within a function does not accept its new value!Why is my variable local in one 'while read' loop,...
Why did UK NHS pay for homeopathic treatments?
Is this a Sherman, and if so what model?
Strange Sticky Substance on Digital Camera
How do you use the interjection for snorting?
Why does this image of Jupiter look so strange?
Hilbert's hotel: why can't I repeat it infinitely many times?
Can Northern Ireland's border issue be solved by repartition?
Do we have any particular tonal center in mind when we are NOT listening music?
Can a broken/split chain be reassembled?
Order of ingredients when making Pizza dough
Does Sitecore have support for Sitecore products in containers?
word frequency from file using partial match
Can an integer optimization problem be convex?
Comma Code - Automate the Boring Stuff with Python
Is it more effective to add yeast before or after kneading?
Lettrine + string manipulation + some fonts = errors and weird issues
Going to France with limited French for a day
What do you do if you have developments on your paper during the long peer review process?
Can a DC brushless motor produce the same torque at different power levels?
Can I take NEW (still in their boxes) PC PARTS in my checked in luggage?
Why does (inf + 0j)*1 evaluate to inf + nanj?
Is it impolite to ask for an in-flight catalogue with no intention of buying?
Is it possible to encode a message in such a way that can only be read by someone or something capable of seeing into the very near future?
On the meaning of 'anyways' in "What Exactly Is a Quartz Crystal, Anyways?"
Variable within a function does not accept its new value!
Why is my variable local in one 'while read' loop, but not in another seemingly similar loop?creating variable using variable value as part of new variable nameTest variable if its string or notValue assigned inside a function variable is always emptyAssign variable identifier in loop and echo its valueBASH Variable checker functionDifference between set variable “value” and set variable value?How to print the variable name along with its value?Declaring a variable that does not have inputSetting variable output from timeout
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I wrote the following function to sequentially add sum calculation into a variable.
RMS_Cal ()
{
num=`cat synt_tdiffs.dat | wc -l`
Sum=0
cat synt_tdiffs.dat | while read info; do
sta=`echo $info | cut -f 2 -d " "`
tds=`echo $info | cut -f 3 -d " "`
tdr=`grep -w "$sta" real_tdiffs.dat | cut -f 3 -d " "`
sum=`echo "($tds - $tdr) ^ 2" | bc -l`
Sum=`echo "$sum + $Sum" | bc -l`
done
echo $Sum
}
The problem is the "Sum" variable does not accept its new value after the while loop ends.
RMS_Cal
++ cat synt_tdiffs.dat
++ wc -l
+ num=1
+ local Sum=0
+ cat synt_tdiffs.dat
+ read info
++ echo AHWZ ILBA 25.631
++ cut -f 2 -d ' '
+ sta=ILBA
++ echo AHWZ ILBA 25.631
++ cut -f 3 -d ' '
+ tds=25.631
++ grep -w ILBA real_tdiffs.dat
++ cut -f 3 -d ' '
+ tdr=527
++ echo '(25.631 - 527) ^ 2'
++ bc -l
+ sum=251370.874161
++ echo '251370.874161 + 0'
++ bc -l
+ Sum=251370.874161
+ read info
+ echo 0
0
How can I handle this problem?
variable
add a comment
|
I wrote the following function to sequentially add sum calculation into a variable.
RMS_Cal ()
{
num=`cat synt_tdiffs.dat | wc -l`
Sum=0
cat synt_tdiffs.dat | while read info; do
sta=`echo $info | cut -f 2 -d " "`
tds=`echo $info | cut -f 3 -d " "`
tdr=`grep -w "$sta" real_tdiffs.dat | cut -f 3 -d " "`
sum=`echo "($tds - $tdr) ^ 2" | bc -l`
Sum=`echo "$sum + $Sum" | bc -l`
done
echo $Sum
}
The problem is the "Sum" variable does not accept its new value after the while loop ends.
RMS_Cal
++ cat synt_tdiffs.dat
++ wc -l
+ num=1
+ local Sum=0
+ cat synt_tdiffs.dat
+ read info
++ echo AHWZ ILBA 25.631
++ cut -f 2 -d ' '
+ sta=ILBA
++ echo AHWZ ILBA 25.631
++ cut -f 3 -d ' '
+ tds=25.631
++ grep -w ILBA real_tdiffs.dat
++ cut -f 3 -d ' '
+ tdr=527
++ echo '(25.631 - 527) ^ 2'
++ bc -l
+ sum=251370.874161
++ echo '251370.874161 + 0'
++ bc -l
+ Sum=251370.874161
+ read info
+ echo 0
0
How can I handle this problem?
variable
Possible duplicate of Why is my variable local in one 'while read' loop, but not in another seemingly similar loop?
– muru
3 mins ago
add a comment
|
I wrote the following function to sequentially add sum calculation into a variable.
RMS_Cal ()
{
num=`cat synt_tdiffs.dat | wc -l`
Sum=0
cat synt_tdiffs.dat | while read info; do
sta=`echo $info | cut -f 2 -d " "`
tds=`echo $info | cut -f 3 -d " "`
tdr=`grep -w "$sta" real_tdiffs.dat | cut -f 3 -d " "`
sum=`echo "($tds - $tdr) ^ 2" | bc -l`
Sum=`echo "$sum + $Sum" | bc -l`
done
echo $Sum
}
The problem is the "Sum" variable does not accept its new value after the while loop ends.
RMS_Cal
++ cat synt_tdiffs.dat
++ wc -l
+ num=1
+ local Sum=0
+ cat synt_tdiffs.dat
+ read info
++ echo AHWZ ILBA 25.631
++ cut -f 2 -d ' '
+ sta=ILBA
++ echo AHWZ ILBA 25.631
++ cut -f 3 -d ' '
+ tds=25.631
++ grep -w ILBA real_tdiffs.dat
++ cut -f 3 -d ' '
+ tdr=527
++ echo '(25.631 - 527) ^ 2'
++ bc -l
+ sum=251370.874161
++ echo '251370.874161 + 0'
++ bc -l
+ Sum=251370.874161
+ read info
+ echo 0
0
How can I handle this problem?
variable
I wrote the following function to sequentially add sum calculation into a variable.
RMS_Cal ()
{
num=`cat synt_tdiffs.dat | wc -l`
Sum=0
cat synt_tdiffs.dat | while read info; do
sta=`echo $info | cut -f 2 -d " "`
tds=`echo $info | cut -f 3 -d " "`
tdr=`grep -w "$sta" real_tdiffs.dat | cut -f 3 -d " "`
sum=`echo "($tds - $tdr) ^ 2" | bc -l`
Sum=`echo "$sum + $Sum" | bc -l`
done
echo $Sum
}
The problem is the "Sum" variable does not accept its new value after the while loop ends.
RMS_Cal
++ cat synt_tdiffs.dat
++ wc -l
+ num=1
+ local Sum=0
+ cat synt_tdiffs.dat
+ read info
++ echo AHWZ ILBA 25.631
++ cut -f 2 -d ' '
+ sta=ILBA
++ echo AHWZ ILBA 25.631
++ cut -f 3 -d ' '
+ tds=25.631
++ grep -w ILBA real_tdiffs.dat
++ cut -f 3 -d ' '
+ tdr=527
++ echo '(25.631 - 527) ^ 2'
++ bc -l
+ sum=251370.874161
++ echo '251370.874161 + 0'
++ bc -l
+ Sum=251370.874161
+ read info
+ echo 0
0
How can I handle this problem?
variable
variable
asked 5 mins ago
alireza niksejelalireza niksejel
83 bronze badges
83 bronze badges
Possible duplicate of Why is my variable local in one 'while read' loop, but not in another seemingly similar loop?
– muru
3 mins ago
add a comment
|
Possible duplicate of Why is my variable local in one 'while read' loop, but not in another seemingly similar loop?
– muru
3 mins ago
Possible duplicate of Why is my variable local in one 'while read' loop, but not in another seemingly similar loop?
– muru
3 mins ago
Possible duplicate of Why is my variable local in one 'while read' loop, but not in another seemingly similar loop?
– muru
3 mins ago
add a comment
|
0
active
oldest
votes
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/4.0/"u003ecc by-sa 4.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%2funix.stackexchange.com%2fquestions%2f542947%2fvariable-within-a-function-does-not-accept-its-new-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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%2funix.stackexchange.com%2fquestions%2f542947%2fvariable-within-a-function-does-not-accept-its-new-value%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
Possible duplicate of Why is my variable local in one 'while read' loop, but not in another seemingly similar loop?
– muru
3 mins ago