There is a weird issue where the values of a "Global Variable" is updated in a function but the updated value is lost when i check it in other function. Any help would be appreciated.
#!/bin/bash
FINAL_RESULT="";
COMMAND_RESULT="";
function init() {
USERNAME="root";
DF_THRESHOLD="20";
DF_COMMAND="df -Pkh";
}
function executeCommand() {
local RESULT;
SERVER=$(hostname);
RESULT=$($1);
FINAL_RESULT="$FINAL_RESULT -------------------------- Executing command : \"$1\" on \"$SERVER\" --------------------------"
echo "Updating FINAL_RESULT to $FINAL_RESULT" # Updated value is present
echo "$RESULT"
COMMAND_RESULT="$RESULT"
}
function getCommandResult() {
executeCommand "$1";
echo "$COMMAND_RESULT" | while read eachLine
do
if [ "$eachLine" != "" ]; then
echo "----------- eachLine ----------- $eachLine"
fi
done
echo "Found FINAL_RESULT as $FINAL_RESULT" # Updated values is lost
}
function main() {
init
getCommandResult "$MMGETSTATE_COMMAND" "MMGETSTATE";
}
main
echo "*** $FINAL_RESULT" # Even now the updated values are not found