Sponsored Content
Full Discussion: Help with multiple for loops
Top Forums Shell Programming and Scripting Help with multiple for loops Post 302887305 by pnara2 on Thursday 6th of February 2014 05:54:05 PM
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 07:35 PM.. Reason: Add CODE tags.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
SSH-ARGV0(1)						    BSD General Commands Manual 					      SSH-ARGV0(1)

NAME
ssh-argv0 -- replaces the old ssh command-name as hostname handling SYNOPSIS
hostname | user@hostname [-l login_name] [command] hostname | user@hostname [-afgknqstvxACNTX1246] [-b bind_address] [-c cipher_spec] [-e escape_char] [-i identity_file] [-l login_name] [-m mac_spec] [-o option] [-p port] [-F configfile] [-L port:host:hostport] [-R port:host:hostport] [-D port] [command] DESCRIPTION
ssh-argv0 replaces the old ssh command-name as hostname handling. If you link to this script with a hostname then executing the link is equivalent to having executed ssh with that hostname as an argument. All other arguments are passed to ssh and will be processed normally. OPTIONS
See ssh(1). FILES
See ssh(1). AUTHORS
OpenSSH is a derivative of the original and free ssh 1.2.12 release by Tatu Ylonen. Aaron Campbell, Bob Beck, Markus Friedl, Niels Provos, Theo de Raadt and Dug Song removed many bugs, re-added newer features and created OpenSSH. Markus Friedl contributed the support for SSH protocol versions 1.5 and 2.0. Jonathan Amery wrote this ssh-argv0 script and the associated documentation. SEE ALSO
ssh(1) Debian Project September 7, 2001 Debian Project
All times are GMT -4. The time now is 03:30 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy