if elif issue
Hello,
I am trying to create a script that could execute a command in few different environments (with different number of servers in each environment) , but i am stuck .... I got a script working before , but someone deleted it accidentally :( , and trying to develop it again but it is just driving me crazy or am not thinking right to work on it !!! if [ "$ENV" = "TESTTC" -o "$ENV" = "TEST_ALL" ]; then SERVER_NUMS="201 202 " elif [ "$ENV" = "TESTSK" -o "$ENV" = "TEST_ALL"]; then SERVER_NUMS="203 204" elif [ "$ENV" = "PRODTC" ]; then SERVER_NUMS="201 202 203 204" elif [ "$ENV" = "PRODSK" ]; then SERVER_NUMS="201 202 203 204 205 " fi for i in $SERVER_NUMS do if [ "$ENV" = "TESTTC" -o "$ENV" = "TEST_ALL" ]; then APP="test_tc$1.com" elif [ "$ENV" = "TESTSK" -o "$ENV" = "TEST_ALL" ]; then APP="test_sk$1.com" elif [ "$ENV" = "PRODTC" ]; then APP="prod_tc$1.com" elif [ "$ENV" = "PRODSK" ]; then APP="prod_sk$1.com" fi when i use TEST_ALL option , this script needs to run both on TEST_TC and TEST_SK environments, but now it stops after running for TEST_TC environment. Please help !! Thanks a lot in advance !! |
the best way to debug is to echo the content of the variables inside of each function.
i think the problem with your logic is that you probably dont want to use elif (just ifs). logiacally: Code:
01: if [ your name is shellscript_03 ] |
Like schneidz said, the problem is your elif. elif stands for "else if", and is only run if the previous test was false. When you're in a TEST_ALL environment, the first if matches and runs, and since the first test was true, all of the subsequent "elif"s are skipped.
|
Thank you schneidz !! You suggestion worked !!But i ran into different issue this time...this script also runs on a specific server mentioned on the command line...which is not working now :( . I used server_num=$3 in the script and also specify the server number in the 3 field on the command line..
Had this highlighted before and used the same way...but it doesn't seem to be working... if [ "$SERVER_NUMS" = "" ]; then f [ "$ENV" = "TESTTC" -o "$ENV" = "TEST_ALL" ]; then SERVER_NUMS="201 202 " elif [ "$ENV" = "TESTSK" -o "$ENV" = "TEST_ALL"]; then SERVER_NUMS="203 204" elif [ "$ENV" = "PRODTC" ]; then SERVER_NUMS="201 202 203 204" elif [ "$ENV" = "PRODSK" ]; then SERVER_NUMS="201 202 203 204 205 " fi for i in $SERVER_NUMS do if [ "$ENV" = "TESTTC" -o "$ENV" = "TEST_ALL" ]; then APP="test_tc$1.com" elif [ "$ENV" = "TESTSK" -o "$ENV" = "TEST_ALL" ]; then APP="test_sk$1.com" elif [ "$ENV" = "PRODTC" ]; then APP="prod_tc$1.com" --> Blame myself for not backing up my scripts :( before someone had a chance to delete them !!! |
what is the f doing at the beginning of the second line (typo ?)
why dont you echo $SERVER_NUMS rite before the first if to make sure it is the expected result. |
Oops ...it is "if"
if [ "$SERVER_NUMS" = "" ]; then if [ "$ENV" = "TESTTC" -o "$ENV" = "TEST_ALL" ]; then ....etc... I did echo before the first if...the script runs two for 201...if i specify 201 on the command line The first time it does print ++ echo 201 201 ++ '[' 201 = '' ']' so it is taking 201 but the second time it is taking 201 again and running it ++ '[' 0 '!=' 0 ']' ++ '[' 201 = '' ']' I am running it with set -x in the script to see what the script is doing !! |
you arent explaining what you are trying to do or what the problem you are having very well.
when i try to run your script in post #4 i get various errors (typos/ missing lines ?); trying to predict what you ommitted i run the script no arguments and also with $SERVER_NUMS and both yeild no results which indicates you probably have some logic errors (e.g.- not sure what $ENV should be assigned to ?). not knowing what logic you are trying to program makes it is difficult to debug. please post the code that you are having problems with and what your expected outcome is. also, use code tags to preserve formatting when posting code. |
Thank you schneidz !! I am able to finally fix this !!! Phew !!!! And I really appreciate your help !!
|
All times are GMT -5. The time now is 12:41 AM. |