Dynamic Variable data problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Dynamic Variable data problem
# 1  
Old 05-21-2013
Wrench Dynamic Variable data problem

Hi,

I am trying to call this variable declared globally STATE=`/opt/Mic*/bin/mstrctl -s IntelligenceServer gs | grep "state" | cut -f2 -d'<' | cut -f2 -d'>'` which extracts the status of a microstrategy server inside the loop. But once the value is assigned initially in the begining, it is not changing as the status of the server changes. Any help on assigning dynamic values to a variable !

Code:
#################################################################################
#Fixing local variables and declaration part
STATE=`/opt/Mic*/bin/mstrctl -s IntelligenceServer gs | grep "state" | cut -f2 -d'<' | cut -f2 -d'>'`
#Fixing Font Colours#
END="\033[0m"
CYAN="\\033[40m\033[36m"
SERSTAT=`echo -e " The I-Server is in $CYAN $STATE $END state !"`
#################################################################################
#ServerStop
ServerStop()
{
        if [ $STATE = "running" ]
                then
						echo -e " $SERSTAT "
                        read STOP
                        if [ "$STOP" = "y" ]
                                then
                                        Emailalert
                                else
                                        ServerContinue
                        fi
        elif [ $STATE = "stopped" ]
                then
                        echo -e " The I-Server is in $CYAN $STATE $END state !"
                        ServerContinue
		elif [ $STATE = "stopping" ]
                then	
						echo -e " The I-Server is in $CYAN $STATE $END state !"
                        echo -e " $WRNEND "
                        ServerContinue
        else
                echo -e " $SERVMAINT "
                ServerContinue
        fi
}


Thanks,
Richard

Last edited by Franklin52; 05-21-2013 at 03:04 AM.. Reason: Please use code tags
# 2  
Old 05-21-2013
Let me see if I have this straight. You define the variables STATE, END, CYAN, and SERSTAT in a shell script. Then you define a function named ServerStop that uses the values of the above variables that were defined before the function is defined (and those values are never updated). The script never invokes the function it has defined.

What output did you expect to be produced by defining a function that you never call?

If you want the value of STATE to change, you need to redefine the value of the variable.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 05-21-2013
Hi,

I have only posted the part of the code that has the issue. Its a very lengthy annoying script that i wrote. The functions part is working fine with me and have got trouble issue with the Variable assigning. Let assume that the function is called successfully. Initially the variable STATE="Running" and first time when the variable is referenced . It fetches the values as running as expected.Once the loop goes into the second round and the command in the variable should fetch 'stopped' but its not doing so . If you need the full code . Please let me know

TIA
# 4  
Old 05-21-2013
Quote:
Originally Posted by kavinmjr
Hi,

I have only posted the part of the code that has the issue. Its a very lengthy annoying script that i wrote. The functions part is working fine with me and have got trouble issue with the Variable assigning. Let assume that the function is called successfully. Initially the variable STATE="Running" and first time when the variable is referenced . It fetches the values as running as expected.Once the loop goes into the second round and the command in the variable should fetch 'stopped' but its not doing so . If you need the full code . Please let me know

TIA
You only show that the variable STATE is set once. No matter how many times you expand $STATE, its value will not change unless you set STATE again. Show us the other lines in your script that set STATE to new a new value.
# 5  
Old 05-21-2013
Hi,

This is the full code. Can we substitute eval to get the results ?

Code:
#! /usr/bin/ksh
#################################################################################
# MSTR Server Restart/Start/Stop Utility
# Created by : Kavin Richard(krich4)
# Date: 20130515
#################################################################################
#Fixing local variables and declaration part
LOG_PATH=/home/itmstr/Autologs
LOG_FILE=$LOG_PATH/Status$(date +%Y-%m-%d""%H:%M:%S).log
EMAIL_REC="kavin.richard@aexp.com"
DT=`date +%Y-%m-%d" "%H:%M:%S`
STATE=`/opt/Mic*/bin/mstrctl -s IntelligenceServer gs | grep "state" | cut -f2 -d'<' | cut -f2 -d'>'`
START=`/opt/Mic*/bin/mstrctl -s IntelligenceServer start`
STOP=`/opt/Mic*/bin/mstrctl -s IntelligenceServer stop`
INIT="0"
CONT="n"
STOP="n"
SERVERIP=`hostname -i`
SERVERNAME=`uname -n`
ACTION="DEFAULT"
TEAM="GLMSTR"
#Fixing Font Colours#
END="\033[0m"
RED="\\033[40m\033[1;31m"
GREEN="\\033[40m\033[1;32m"
CYAN="\\033[40m\033[36m"
#Fixing system response messages#
WRNOPT="$RED ::WARNING:: Please enter the correct option number between [0-4] !!! $END"
WRNSTART="$RED ::WARNING::The I-Server is already RUNNING/STARTING state! Please try again !!! $END"
WRNSTARTCNT="$RED ::WARNING::Do you want to continue with Starting the I-Server (y/n) ? $END"
WRNEND="$RED ::WARNING::The I-Server is already in STOPPED/STOPPING state. Please check and try again !!!  $END"
WRNENDCNT="$RED ::WARNING::Do you want to continue with Stopping the I-Server (y/n) ? $END"
OPTCNT="$GREEN Would you like to Restart/Start/Stop I-Server ? (y/n) $END "
SERVMAINT="$RED STILL UNDER MAINTANANCE. Please select proper options ! $END"
SERSTAT=`echo -e " The I-Server is in $CYAN $STATE $END state !"`
#################################################################################
#ServerContinue
ServerContinue()
{
echo -e " $OPTCNT "
read CONT
echo -e "\n"
        if [ "$CONT" = "y" ]
            then
                Serverinit
        else
                exit
        fi
}
#################################################################################
#ServerStop
ServerStartStop()
{
        if [ "$ACTION" = "STATUS" ]
            then
                echo -e " The I-Server is in $CYAN $STATE $END state !"
                ServerContinue
        elif [ "$ACTION" = "STOPPING" ]
                then
                        ServerStop
        else
                echo -e "$WRNOPT"
                ServerContinue
        fi
}
#################################################################################
#ServerStop
ServerStop()
{
        if [ $STATE = "running" ]
                then
						echo -e " $SERSTAT "
                        echo -e " $WRNENDCNT"
                        read STOP
                        if [ "$STOP" = "y" ]
                                then
										$STOP
                                        ServerStopChk
                                else
                                        ServerContinue
                        fi
        elif [ $STATE = "stopped" ]
                then
                        echo -e " $WRNEND "
                        ServerContinue
		elif [ $STATE = "stopping" ]
                then	
						echo -e " The I-Server is in $CYAN $STATE $END state !"
                        echo -e " $WRNEND "
                        ServerContinue
        else
                echo -e " $SERVMAINT "
                ServerContinue
        fi
}
#################################################################################
#ServerStopChk
ServerStopChk()
{
        echo -e " The I-Server is in $CYAN $STATE $END state ! Please wait... "
        sleep 30 #sleeps for 5 seconds
        if [ $STATE = "running" ]
                then
                        echo -e " The I-Server is in $CYAN $STATE $END state ! Please wait... "
                        sleep 5 #sleeps for 5 seconds
                        ServerStopChk
		elif [ $STATE = "stopping" ]
                then
						echo -e " The I-Server is in $CYAN $STATE $END state ! Please wait... "
                        sleep 5 #sleeps for 5 seconds
                        ServerStopChk
		elif [ $STATE ="stopped" ]
			then
                echo -e " The I-Server has STOPPED."
                echo -e " The I-Server is in $CYAN $STATE $END state ! "
                ServerContinue
		else
			echo -e " $SERVMAINT "
            ServerContinue
        fi
        ActionCheck
}
#################################################################################
#ActionCheck
ActionCheck()
{
        if [ $ACTION = "STOPPING" ]
                then
                        ServerContinue
        else
                ServerContinue
        fi
}
#################################################################################
#ServerInput
ServerInput()
{
ACTION=""
        if [ "$INIT" = '1' ]
                then
                        ACTION="STATUS"
						echo $ACTION
                        ServerStartStop
						elif [ "$INIT" = '2' ]
                then
                        ACTION="STOPPING"
						echo $ACTION
                        ServerStartStop
                        exit
        elif [ "$INIT" = '0' ]
                then
                        exit
        else
                echo -e  " $WRNOPT "
                exit
        fi
ServerInput
}
#################################################################################
#Serverinit
Serverinit()
{
        echo -e "$GREEN /************ MSTSR Server Restart/Start/Stop Utility  ************/ $END"
    echo -e "   "Check status of the I-Server press : 1
        echo -e "   "Stop the I-Server"                 " : 2
        echo -e "   "To Exit"                           " : 0
    echo -e "$GREEN /******************************************************************/ $END "
        read INIT
ServerInput
}
#################################################################################

#Intiation of script
Serverinit


Last edited by Franklin52; 05-21-2013 at 03:05 AM.. Reason: Please use code tags
# 6  
Old 05-21-2013
Now that you have shown us your entire script, I repeat. You set STATE on the 11th line of your script. That sets the value it will have for the rest of your script until you use STATE-xomething or read ... STATE ... or something similar to assign it a new value. Nothing in your script changes the value of STATE after it is set the first time.

PS. In you 1st posting you said STATE was being set in a loop, but there is no loop in your program. (I didn't follow all of the function calls; you may have infinite recursion, but you do not have a loop. And nothing inside the possible recursion boundaries resets the value of STATE unless you're not showing us the entire script.)

Last edited by Don Cragun; 05-21-2013 at 01:51 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dynamic variable name in bash

Hello, I'm trying to build a small script to store a command into a dynamic variable, and I have trouble assigning the variable. #!/bin/bash declare -a var1array=("value1" "value2" "value3") var1arraylength=${#var1array} for (( i=1; i<${var1arraylength}+1; i++ )); do mkdir... (7 Replies)
Discussion started by: alex2005
7 Replies

2. UNIX for Dummies Questions & Answers

Dynamic Variable creation

I am trying to create some variables based on the input by the user, say if user entered 3 then 3 variables and if 5 then 5 variables. I am using a for loop for (( i=1; i <= $num; i++ )) do x="num" x+=$i done When i am using echo $x it will show num1 but now how to create variables... (3 Replies)
Discussion started by: Raj999
3 Replies

3. Shell Programming and Scripting

Passing dynamic variable within another variable.

I have a small program which needs to pass variable dynamically to form the name of a second variable whose value wil be passed on to a third variable. ***************** Program Start ****************** LOC1=/loc1 PAT1IN=/loc2 PAT2IN=/loc3 if ; then for fpattern in `cat... (5 Replies)
Discussion started by: Cyril Jos
5 Replies

4. Shell Programming and Scripting

Bash, Dynamic Variable Problem >.<

Hello, I have a problem with a bash script, I've been doing recherches but i can't make it work. It is my first time with a dynamic variable and i don't understand how to write it. #!/bin/bash USER=BARSPIN HOTKEYS_PATH="/home/$USER/Documents/bash/tibia/HOTKEYS" CFG1="A" CFG2="B"... (5 Replies)
Discussion started by: barspin
5 Replies

5. Shell Programming and Scripting

Dynamic file name in variable

Hi guys. i have a requirment as below. I have a scripts which perform for loop for i in /backup/logs -- it will give all the logs file SC_RIO_RWVM_20120413064217303.LOG SC_RIO_RWXM_20120413064225493.LOG SC_RIO_RXXM_20120413064233273.LOG ... do open script.sh ---- in this file... (3 Replies)
Discussion started by: guddu_12
3 Replies

6. Shell Programming and Scripting

dynamic variable value assignmnet

QUERY IN BRIEF Listing the query in short #! /bin/csh -f #say i have invoked the script with two arguments : a1 and 2 set arg = $1 # that means arg = a1 echo "$arg" #it prints a1 #now what i want is: echo "$a1" #it will give error message :a1 undefined. #however what i need is that the... (2 Replies)
Discussion started by: animesharma
2 Replies

7. Shell Programming and Scripting

Help with Dynamic variable

I need some variable help TEMP1=Jane TEMP2=Sue X=1 eval USER=TEMP${X} echo $USER This gives output USER1 I would like to get Jane I have tried eval USER='TEMP${X}' eval USER="TEMP${X}" eval USER=`TEMP${X}` (3 Replies)
Discussion started by: Jotne
3 Replies

8. Shell Programming and Scripting

dynamic variable name

I found one post in another site with a solution for my problem the below solution should explain what I want. #!/bin/sh first="one" second="two" third="three" myvar="first" echo ${!myvar} But this gives error 'bad substitution' System info SunOS sundev2 5.9... (3 Replies)
Discussion started by: johnbach
3 Replies

9. Shell Programming and Scripting

Dynamic variable assignment

Hi All, I have the below scenario: A file test.cfg with three fields>> DATA1 DATA2 DATA3 In the script I need to assign each of the fields to variables. The number of fields will not be constant (this case we have three). Im trying to do something like this: NUM=1 OUT_DAT_NO=3 ... (4 Replies)
Discussion started by: deepakgang
4 Replies

10. UNIX for Dummies Questions & Answers

Dynamic variable values

Bit of a newbie :D with regard to unix scripting and need some advice. Hopefully someone can help with the following: I have a predefined set of variables as follows: AAA_IP_ADD=1.1.1.1 BBB_IP_ADD=2.2.2.2 I have a funnction call which retrieves a value into $SUPPLIER which would be... (3 Replies)
Discussion started by: ronnie_uk
3 Replies
Login or Register to Ask a Question