For loop and running 2 commands at once?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For loop and running 2 commands at once?
# 1  
Old 02-25-2010
For loop and running 2 commands at once?

HI.

I am trying to run 2 commands, using the "for x in a b c d" loop but i am having a hard time coding it...

Here is what i have so far:

Code:
for SERVER in SERVERA SERVERB SERVERC SERVERD SERVERE 
do

###############################################################################
# Launching the first backup...
###############################################################################

        echo ${LOGIN} "GENERATE BACKUPSET ${SERVER}

###############################################################################
# Launching the second backup
###############################################################################

        echo ${LOGIN} "GENERATE BACKUPSET ${SERVER}

###############################################################################
# Waiting for the first pair to complete...
###############################################################################

  while (( $( ${LOGIN} q proc | grep "GENERATE BACKUPSET" | wc -l) != 0 ))
  do
     echo ".\c"
     sleep 60
  done
done


What i am trying to do, is get the loop to run the GENERATE BACKUPSET SERVERA and SERVERB at the same time. Then wait using the while loop at the bottom. Once those 2 are completed (while loop would exit) it should keep going to GENERATE BACKUPSET SERVERC and SERVERD and so on...I've inserted "echo" in the front for debug purposes...

What i am getting though, is a GENERATE BACKUPSET SERVERA twice instead...

I tried to issue a "continue" but that did not work either...

Ideas?

Thanks!

Last edited by Scott; 02-25-2010 at 12:13 PM.. Reason: Please use code tags
# 2  
Old 02-25-2010
Start backup in background and wait process exit. Something like:
Code:
#!/ksh93 or bash or zsh ...
backup1=( SA SC SE )
backup2=( SB SD SF )
round=0
roundmax=${#backup1[*]}
while (( round < roundmax ))
do
       backup "${backup1[$round]}" &
       pid1=$!
       backup "${backup2[$round]}" &
       pid2=$!
       wait $pid1
       wait $pid2
       echo done 
       ((round+=1))
done


Last edited by kshji; 02-26-2010 at 02:06 AM..
# 3  
Old 02-25-2010
Quote:
Originally Posted by kshji
Start backup in background and wait process exit. Something like:
Code:
#!/someposixsh
backup1=( SA SC SE )
backup2=( SB SD SF )
round=0
roundmax=${#backup1[*]}
while (( round < roundmax ))
do
       backup "${backup1[$round]}" &
       pid1=$!
       backup "${backup2[$round]}" &
       pid2=$!
       wait $pid1
       wait $pid2
       echo done 
       ((round+=1))
done

Thanks for the input.

I am getting some problems with this though...

Variables backup1 and backup2 with the ( ) does not work. I tried to use " but not good either...

I understand what your scripts tries to do and it seems logical, but can't figure out how to get it to work though...

Thanks.
# 4  
Old 02-25-2010
Hi stephen,

Script posted by kshji itself working fine.If you face problems with assigning values to an array.Then,assign the values to an array using the following way.

i=0

for x in SA SC SE
do
backup1[$i]=$x
let i=i+1
done
i=0
for x in SB SD SF
do
backup2[$i]=$x
let i=i+1
done
for((x=0;x<$i;x++)) #This is for checking and printing the array values
do
echo "Back Up1 Values:${backup1[$x]}"
echo "Back Up2 Values:${backup2[$x]}"
done
# 5  
Old 02-26-2010
Sorry, my term "posixsh" is not good in this case. This kind of array init is not in posix defination.
Many of current sh support it. Like ksh93, bash, zsh, ... but not in dash which is posix-sh without extension.
# 6  
Old 02-26-2010
Thank you both for the explanation. Also, thanks for the example, i think i can get that to work.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Running two commands in background

Hi I want to run two commands in background, i am using below way, but it is not working, i m calling these two commands from one script to another server where below commands are running, in my script ssh is working fine, but command is not going in background, please advise what should i do. ... (10 Replies)
Discussion started by: learnbash
10 Replies

2. UNIX for Dummies Questions & Answers

Echo out running commands

Is there any way in a script to print out the commands being ran? In DOS script, there is the "@echo on" and "@echo off". so I have a script like this: #!/bin/ksh echo "hello there. moving files." <turn on echoing here> cp thisfile.txt thatfile.txt cp whatfile.prop whyfile.prop <turn... (2 Replies)
Discussion started by: ronron5477
2 Replies

3. Shell Programming and Scripting

Code for running commands one after other

I need an if code in shell script where it should continue to further commands after succesfully installing the executable file. i.e. /run installer is continuing but in the middle it executes further commands like "cp /root/user which were given after /run installer. I want /runinstaller... (16 Replies)
Discussion started by: sriki32
16 Replies

4. Shell Programming and Scripting

Running sed commands

Hello I need to run some sed commands but it involves "/" in the substitute or delete, any ideas how I get round the problem. Example: cat file1.txt | sed -e '/</Header>/d' > file2.txt This errors due to the forward slash before the Header text. Thanks (3 Replies)
Discussion started by: Dolph
3 Replies

5. UNIX for Dummies Questions & Answers

Running two commands in one line

hi! how do i run 2 command from the same line e.g: 'which screen' and then ls -la 'which screen' (4 Replies)
Discussion started by: rdns
4 Replies

6. Programming

Running shell commands from C/C++

Hi guys, I know using system() we can run unix commands but the problem is, I can't get any returns with the system(). I am returning stuff from my shell scripts that I need to be able to read from my C code. Anybody has cure to this problem? :)) Thanks (2 Replies)
Discussion started by: alirezan
2 Replies

7. Shell Programming and Scripting

running commands from outside of bash

Hello all! I have two consoles.I know the PID of these two.Now I want to execute a command in console#1 but from console#2. How can I achieve this? Anyone can do something for my query? Click here if you can't understand my query or to know the need for this query. (3 Replies)
Discussion started by: rakabarp
3 Replies

8. Shell Programming and Scripting

How to disable running commands from vi

Hello, We have a requirement to disable running shell commands via vi using "!". Can anybody please suggest how to disable this option. The requirement arises because we open up a xterm window with a config file in vi mode for the customer to edit. After the customer edits the config... (1 Reply)
Discussion started by: Umesh_Sharoff
1 Replies

9. Shell Programming and Scripting

running commands from script

I'm new to unix and I have a fairly simple problem: Lets say I am in a specific directory and I run the command: "dirs" , I get an output of all the folders that i pushed into the stack (as expected), buut, when when I create a script (called test): #! /bin/csh dirs and then i run:... (2 Replies)
Discussion started by: owijust
2 Replies

10. UNIX for Dummies Questions & Answers

running start up commands

Hi all How can I setup my shell so that I run a set of commmands or a script every time I login. I am using kshell. I tried putting a line in ./.bash_profile (does not work) Ex.: I want to use vi as the commandline editor so, I want to run the command: set -o vi (3 Replies)
Discussion started by: skotapal
3 Replies
Login or Register to Ask a Question