Help with multiple for loops


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with multiple for loops
# 1  
Old 02-06-2014
Help with multiple for loops

All,
I have set up ssh trust between 3 machines. The aim is to connect from machine-A to B and C and clear the txt files in tmp on all 3 machines. And, I have 3 environments and each environment has 2 hosts. So I should be able to run this script on any environment.

Here is the logic:
I want to see if my current hostname is part of any of these 3 sections/environments, if yes, then go into that section only and do stuff. That is clear the txt only for that section and exit out.

But this isnt working as expected. It does the job but after clearing the txt, it moves onto next section/environment instead of exiting out.

I think nested for isnt working as expected or maybe I shouldnt use nested for in the first place! Please let me know if you have any better ideas to make this work.

Here is the script:

Code:
##This script will clear the txt in tmp in both local and remote locations
#!/bin/sh

EXTSSSERVERLIST=ext1.test.com,ext2.test.com
APPSSSERVERLIST=app1.test.com,app2.test.com
FAASERVERLIST=faa1.test.com,faa2.test.com

echo "Clearing txt for Test Domain..."
host=$(hostname)
echo $host;

for $(hostname) in $EXTSSSERVERLIST;
        do
                for f in $(echo $EXTSSSERVERLIST | tr "," " ");
                        do
                        ssh $f "rm -rf /tmp/*.txt"
                        done
        done

for $(hostname) in $APPSSSERVERLIST;
        do
                for f in $(echo $APPSSSERVERLIST | tr "," " ");
                        do
                        ssh $f "rm -rf /tmp/*.txt"
                        done
        done

for $(hostname) in $FAASSSERVERLIST;
        do
        for f in $(echo $FAASSSERVERLIST | tr "," " ");
                do
                ssh $f "rm -rf /tmp/*.txt"
                done
        done

Moderator's Comments:
Mod Comment It appears that several moderators have edited your posts to add CODE tags for you. Please add CODE tags when you submit posts rather than depending on moderators to clean things up for you.

Last edited by Don Cragun; 02-06-2014 at 08:35 PM.. Reason: Add CODE tags.
# 2  
Old 02-06-2014
To assist other posters:

Which Operating System and Version are you running?
Which Shell have you written the code for?
Is there a link on your computer from /bin/sh to another (non-Bourne) Shell?
# 3  
Old 02-06-2014
It is Redhat linux. and /bin/sh and there is no link to any other shell


Thanks
# 4  
Old 02-06-2014
To test if your hostname is in a list you should use something like this:

Code:
if echo $EXTSSSERVERLIST | grep -wq $host
then
                for f in ${EXTSSSERVERLIST//,/ }
                do
                        ssh $f "rm -rf /tmp/*.txt"
                done
fi

# 5  
Old 02-06-2014
Note that a line of the form:
Code:
#!/pathname/of/interpreter

has absolutely no effect on a script unless it is the first line in the script.
# 6  
Old 02-07-2014
Thanks to all

Special thanks to chubler. The code worked perfectly. I am trying it out on other hosts now and will also do some extensive testing before I make it more dynamic.

I am planning to make it more dynamic and use this script to do repeated things like clearing cache, files et al on remote hosts.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ssh to multiple hosts and then run multiple for loops under remote session

Hello, I am trying to login to multiple servers and i have to run multiple loops to gather some details..Could you please help me out. I am specifically facing issues while running for loops. I have to run multiple for loops in else condition. but the below code is giving errors in for... (2 Replies)
Discussion started by: mohit_vardhani
2 Replies

2. UNIX for Dummies Questions & Answers

Need help with for loops

Why wont my for statements work? Im trying to get this script to swich to a user an if you put in a start/stop/or restart paramater to do just that for each user. I commented out the actual start/stop actions to test it just by using echos and not do anything hasty in the environment but it... (0 Replies)
Discussion started by: LilyClaro
0 Replies

3. Shell Programming and Scripting

Using grep with multiple loops in reading files

I am trying to read a file line by line and then search that line in another file and get a particular column from the second file. I have written this code.but its not working properly #!/bin/sh while read keyword in duplicate.txt do echo $keyword while read line do ... (7 Replies)
Discussion started by: Prachi Gupta
7 Replies

4. UNIX for Dummies Questions & Answers

While Loops Multiple File

Guru, I try to make a loop of 2 files, Input File1.txt: 1 2 File2.txt: A B C A, B and C is a file name, inside A X Y Z Expected Output A.1 X (2 Replies)
Discussion started by: guns
2 Replies

5. Shell Programming and Scripting

help with multiple loops in shell script

Hi Guys- I'm trying to write a script which takes date as input (mm.yy.dd) and search in the current file. If pattern doesn't exist it will then look in a backup directory and so on. being a newb i'm unable to loop over to the backup directory. hoping for some ideas, i've highlighted the... (1 Reply)
Discussion started by: Irishboy24
1 Replies

6. Shell Programming and Scripting

multiple while loops in expect script

Hi, I am trying to incorporate multiple while loops into an expect script written in ksh shell. This is on a Solaris 10 system. Here is the code: #!/bin/ksh EXPECT=/usr/local/bin/expect exp_internal i=1 h=0 while ]; do $EXPECT << DONE set stty_init raw ... (1 Reply)
Discussion started by: cic
1 Replies

7. Shell Programming and Scripting

For loops with multiple variables

Hi script gurus. I have need to know how to use for loop with multiple variable. Basically lets take for example /etc/passwd file has following entries The above cat command will basically first greps the real users that have email addresses then converts ':' to '+' then using cut... (4 Replies)
Discussion started by: sparcguy
4 Replies

8. Shell Programming and Scripting

bash scripting: using multiple 'for loops'??

Hey guys, I'm kinda a noob at scripting. I am trying to create a script that uses multiple for loops with the lsiutility to monitor disk health on a system. The script runs, but it will continually echo an infinite number of LogVolumes when there are only 2 per virtual disk on my server. It's... (2 Replies)
Discussion started by: tank126
2 Replies

9. Shell Programming and Scripting

Multiple loops for Load test

Hi All, I am trying to write a bash script that will read a list of numbers from a file, then it needs to use netcat to create a socket connection and pass header/request. I need to limit it to 100 connections. So, after the first set of 100 are fineshed, I need to loop and do it again with... (0 Replies)
Discussion started by: willdev
0 Replies

10. UNIX for Dummies Questions & Answers

Multiple for loops within a Menu?

I have program that I want to be able to use I guess you would call them functions.... to run muliple little programs or loops with one menu script. How would I do this. Here is some code I am using. Sorry about the formatting....it doesn't paste well. echo "*****************************... (3 Replies)
Discussion started by: darthur
3 Replies
Login or Register to Ask a Question