LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-13-2012, 01:05 PM   #1
shellscript_03
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Rep: Reputation: Disabled
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 !!
 
Old 11-13-2012, 01:12 PM   #2
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-19-live-usb/ aix
Posts: 3,562

Rep: Reputation: 522Reputation: 522Reputation: 522Reputation: 522Reputation: 522Reputation: 522
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 ]
02:  then "say glad to meet you."
03: else if [ your name is schneidz ]
04:  then "go to bed."
^ notice for you, you could stop reading at line 2.

Last edited by schneidz; 11-13-2012 at 01:21 PM.
 
1 members found this post helpful.
Old 11-13-2012, 01:17 PM   #3
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,424

Rep: Reputation: 890Reputation: 890Reputation: 890Reputation: 890Reputation: 890Reputation: 890Reputation: 890
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.
 
Old 11-13-2012, 02:35 PM   #4
shellscript_03
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
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 !!!
 
Old 11-13-2012, 04:20 PM   #5
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-19-live-usb/ aix
Posts: 3,562

Rep: Reputation: 522Reputation: 522Reputation: 522Reputation: 522Reputation: 522Reputation: 522
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.

Last edited by schneidz; 11-13-2012 at 04:22 PM.
 
Old 11-13-2012, 05:10 PM   #6
shellscript_03
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
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 !!
 
Old 11-14-2012, 08:23 AM   #7
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-19-live-usb/ aix
Posts: 3,562

Rep: Reputation: 522Reputation: 522Reputation: 522Reputation: 522Reputation: 522Reputation: 522
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.

Last edited by schneidz; 11-14-2012 at 08:43 AM.
 
Old 11-14-2012, 08:54 AM   #8
shellscript_03
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thank you schneidz !! I am able to finally fix this !!! Phew !!!! And I really appreciate your help !!

Last edited by shellscript_03; 11-14-2012 at 11:12 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
scipt help elif bilbonix Linux - Newbie 6 11-03-2012 03:47 PM
[SOLVED] Elif Statement help UnixNewbie91 Linux - Newbie 6 04-28-2012 10:35 AM
Python shell help (using elif) danred Linux - General 8 10-20-2009 03:55 AM
Bash if, elif, else not working Blackout_08 Programming 7 06-09-2006 10:28 AM
elif??????????? embsupafly Programming 1 11-27-2002 03:48 AM


All times are GMT -5. The time now is 02:38 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration