MEMO: Issue with a for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting MEMO: Issue with a for loop
# 1  
Old 05-11-2011
Network MEMO: Issue with a for loop

Can someone help me what i am doing wrong with this:
Code:
apsrvrport1=28548
stcmsport1=28507
apsrvrport2=38548
stcmsport2=38507
apsrvrport3=48548
stcmsport3=48507
apsrvrport4=58548
stcmsport4=58507
apsrvrport5=38048
stcmsport5=38007
apsrvrport6=27548
stcmsport6=27507
apsrvrport7=37548
stcmsport7=37507
apsrvrport8=18548
stcmsport8=18507
emanager1=15500
emanager2=15600
echo " ..................Collecting lsof command information..............."
for i in 1 2 3 4 5 6 7 8
do
serverlsof$i=`lsof |grep *:$apsrvrport$i | awk '{print $2}'`
echo "PID = $appserver$i"
done

when I run it:
Code:
mytest.sh[40]: serverlsof1=: not found.
PID = 1

I have two issues: 1) I am getting an error on the serverlsof$i
2) Why the PID is capturing the counter !! hopw can I pass the actual value of the lsof command in this case?

Thank you

Last edited by vbe; 05-11-2011 at 10:36 AM.. Reason: code tags are to be used...
# 2  
Old 05-11-2011
From a syntax point of view the script needs to use "eval" wherever a changeable variable name occurs to stop the variables being evaluated on the first pass through the Shell.
My interpretation of what you intended (untested):

Code:
for i in 1 2 3 4 5 6 7 8
do
  eval serverlsof$i=`lsof |grep *:\$apsrvrport$i | awk '{print $2}'`
  eval echo "PID = \$appserver$i"
done


Personally I would have used a case statement to set unsuffixed environment variables. Other scripters might use an array. Other scripters would "source" parameter files (one per suffix).
# 3  
Old 05-11-2011
Fixed the first issue, But the echo PID line is not displaying the result of the lsof command: it is display this line 8 times:
PID = spaces
# 4  
Old 05-11-2011
You never assigned any values to the variable "\$appserver$i" and hence it is printing nothing. Revisit your logic once more.

regards,
Ahamed
# 5  
Old 05-12-2011
Code:
for i in 1 2 3 4 5 6 7 8
    do
       eval serverlsof$i=`lsof |grep *:\$apsrvrport$i | awk '{print $2}'`
       eval echo "PID = \$serverlsof$i"
    done

Still the same issue.
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!


---------- Post updated at 11:06 PM ---------- Previous update was at 02:39 PM ----------

Need someone who knows about this to respond.

Last edited by vgersh99; 05-11-2011 at 04:41 PM.. Reason: code tags, please!
# 6  
Old 05-23-2011
any takerrrrrrs?
# 7  
Old 05-23-2011
That eval trick is a nightmare with a complex command like that. You'll need a lot more backslashes to get it working. Use arrays instead.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Issue when doing a loop

Hi, I just have started learning shell scripting (sh). Why do i only get the date? while read dt do echo "Date : ${dt} sed -n '/${dt}/,/${dt}/p' file1.log | grep -w ERROR done < date1.dat INPUT - date1.dat 2019-04-05 04:58:25 2019-04-05 04:58:26 2019-04-05 05:00:56... (3 Replies)
Discussion started by: margel
3 Replies

2. Shell Programming and Scripting

Issue with for loop

Hi Team, I have for loop in my shell script. Which basically loop through all files in the directory, When some files are in the directory it works just fine. But if there are no files at all..still the for loop try to execute. Please help. Below is the code. #!/bin/ksh echo "Program... (5 Replies)
Discussion started by: bharath561989
5 Replies

3. Shell Programming and Scripting

Issue with while loop?

Hi, I have prepared a script to search for backup file information on the Linux server. Script works fine for the most part except the echo statement inside an IF conditional block displays the message ''snapshot directory not found on xxxxx" even though the .snapshot directory is found a... (11 Replies)
Discussion started by: svajhala
11 Replies

4. Linux

HOW CAN I PRINT OUT THE MEMO ONLY?

I tried to print out external memo in LINUX system, however, each time in the print out also print out M--. M--. in front of the external memo, my question is HOW CAN I PRINT OUT THE MEMO ONLY? THANKS (0 Replies)
Discussion started by: scottblee
0 Replies

5. Homework & Coursework Questions

Airport using semaphores and shared memo

Hi just doin' this here for the naval school, back here in Pportugal, and needed some help, especially with the shared memo i want to use for the 10 airport gate, and the maximum of 4 planes preparing to leave; canīt figure out how the gate can be id by the same PID. WELL, if someone wants to... (2 Replies)
Discussion started by: Turbo
2 Replies

6. Programming

Airport using semaphores and shared memo

Hi just doin' this here for the naval school, back here in Pportugal, and needed some help, especially with the shared memo i want to use for the 10 airport gate, and the maximum of 4 planes preparing to leave; canīt figure out how the gate can be id by the same PID. WELL, if someone wants to... (1 Reply)
Discussion started by: Turbo
1 Replies

7. Shell Programming and Scripting

issue with for loop

Hi All, Please help me with for loop!!!! I have to put the limit with for loop like For I in 1..30 Do Echo $i Done I want my output to be like 1 2 3 4 5 6 7 8 9 till 30 (6 Replies)
Discussion started by: kumarmani
6 Replies

8. Shell Programming and Scripting

loop issue

I have 2 files one of them has all the all mac addresses and the other one has all the ip addresses. Basically, I want to loop thru those 2 files and generate a configuration like below: host www184.domain.com { hardware ethernet 00:13:72:3B:B4:3A; fixed-address 192.168.0.184; }... (4 Replies)
Discussion started by: kkkk
4 Replies
Login or Register to Ask a Question