The loop does not run as expected


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting The loop does not run as expected
# 1  
Old 07-01-2006
The loop does not run as expected

Hi folks,

I have the following configuration file:
Code:
DB_LAYER=NO
ADMIN_LAYER=NO
RTESUB_LAYER=NO
DB_HOST_NAME=tornado
ADMIN_HOST_NAME=tornado
RTESUB_HOST_NAME=tornado
RESPONSE_FILE_SR=/tmp/SR.rsp
INSTALL_SR_1=/home/Upgrade_4.7.1/Utilities/Install_SR:Y
INSTALL_SR_2=/home/Upgrade_4.7.2/Utilities/Install_SR:Y
INSTALL_SR_3=/home/Upgrade_4.8/Utilities/Install_SR:Y
INSTALL_SR_4=/home/Upgrade_4.8.1/Utilities/Install_SR:Y

I need to do the following:
1. Run in loop on this file
2. find the lines which end with ":Y"
3. cut the path from the line
4. Change directory to the path and run a script

I wrote the following function:


Code:
run_install_sr()
{
cat ${CONF_FILE} | grep INSTALL_SR_ | grep :Y | while read line
do
  INSTALL_SR_DIR=`echo $line | cut -d= -f2 | cut -d: -f1`

  rcp ${RESPONSE_FILE_SR} ${DB_HOST_NAME}:/tmp >> $installLog
  rsh ${DB_HOST_NAME} "su - root -c \"cd ${INSTALL_SR_DIR} && ./Install_SR.ksh -f /tmp/${RESPONSE_FILE_SR##*/} -l database\"" | tee -a $installLog

  rcp ${RESPONSE_FILE_SR} ${ADMIN_HOST_NAME}:/tmp >> $installLog
  rsh ${ADMIN_HOST_NAME} "su - root -c \"cd ${INSTALL_SR_DIR} && ./Install_SR.ksh -f /tmp/${RESPONSE_FILE_SR##*/} -l administration\"" | tee -a $installLog

  if [[ ${RTESUB_LAYER} = 'YES' ]] && [[ ${ADMIN_LAYER} = 'YES' ]] ; then
     rcp ${RESPONSE_FILE_SR} ${RTESUB_HOST_NAME}:/tmp >> $installLog
     rsh ${RTESUB_HOST_NAME} "su - root -c \"cd ${INSTALL_SR_DIR} && ./Install_SR.ksh -f /tmp/${RESPONSE_FILE_SR##*/} -l subscriber\"" | tee -a $installLog
  fi
done
}

The expected results : the loop suppose to run 4 times
The current results: the loop ran only once and issue the first line the loop found.
Does someone see something wrong?

Thanks in advance,
Nir
# 2  
Old 07-01-2006
Code:
run_install_sr()
{
nawk -F'[=:]' '$1 ~ "^INSTALL_SR_" && $NF = "Y" {print $2}' | while read INSTALL_SR_DIR
do
  echo "INSTALL_SR_DIR -> [${INSTALL_SR_DIR}]"
  .......... do whatever .........
done;

# 3  
Old 07-02-2006
Hi vgersh99,

Thanks for your reply.
I replaced the original loop with your "nawk" command.
There is a problem : the program is hang.

Any more suggestions?

Thanks in advance,
Nir
# 4  
Old 07-02-2006
I found the problem but I didn't find a solution.
The loop is OK. The problem is that the function exit from the operation after "rsh" command and does not continue to the next iteration.

I wrote the following testing program:
Code:
#! /bin/ksh

CONF_FILE=nir.conf
export $(< $CONF_FILE)
run_install_sr()
{
cat ${CONF_FILE} | grep INSTALL_SR_ | grep :Y | while read line
do
  INSTALL_SR_DIR=`echo $line | cut -d= -f2 | cut -d: -f1`

  echo Install SR directory is $INSTALL_SR_DIR
  rsh ${DB_HOST_NAME} hostname 
done
}

nir.conf content is:
Code:
DB_HOST_NAME=tornado
INSTALL_SR_1=/home/Upgrade_4.7.1/Utilities/Install_SR:Y
INSTALL_SR_2=/home/Upgrade_4.7.2/Utilities/Install_SR:Y
INSTALL_SR_3=/home/Upgrade_4.8/Utilities/Install_SR:Y
INSTALL_SR_4=/home/Upgrade_4.8.1/Utilities/Install_SR:Y

When I run the script I get the following output:
Code:
tornado:> ./nir.ksh 
Install SR directory is /home/Upgrade_4.7.1/Utilities/Install_SR
tornado

If I remark the rsh command in the script ,I get the following output:
Code:
tornado:> ./nir.ksh 
Install SR directory is /home/Upgrade_4.7.1/Utilities/Install_SR
Install SR directory is /home/Upgrade_4.7.2/Utilities/Install_SR
Install SR directory is /home/Upgrade_4.8/Utilities/Install_SR
Install SR directory is /home/Upgrade_4.8.1/Utilities/Install_SR

As you can see,the loop works fine and the problem is with the rsh command - the function is getting out from the loop after the first iteration instead of continuing to the next iteration.

Any ideas?

Thanks in advance,
Nir
# 5  
Old 07-02-2006
Quote:
Originally Posted by nir_s
Hi vgersh99,

Thanks for your reply.
I replaced the original loop with your "nawk" command.
There is a problem : the program is hang.

Any more suggestions?

Thanks in advance,
Nir
sorry, forgot to include the file to read:
Code:
#!/bin/ksh

CONF_FILE=nir.conf

run_install_sr()
{
nawk -F'[=:]' '$1 ~ "^INSTALL_SR_" && $NF = "Y" {print $2}' "${CONF_FILE}" | while read INSTALL_SR_DIR
do
  echo "INSTALL_SR_DIR -> [${INSTALL_SR_DIR}]"
  .......... do whatever .........
done;

# 6  
Old 07-02-2006
Hey vgersh99,

Thanks!
It works!!

Please explain what is the magic here?

Thanks in advance,
Nir
# 7  
Old 07-02-2006
The 'magic' is....
To have TWO FieldSeparators - ':' and '=' : -F'[:=]'
Checking the FIRST field starting with 'INSTALL_SR_'
And LAST field having a 'Y': $NF = "Y"

If the above two conditions are true, output the SECOND field: {print $2}

The output of the 'nawk' gets piped to a while/read .....
And the rest is your loop.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop not giving expected output

#cat /tmp/input old_array old_dev new_dev new_array 0577 008AB 01744 0125 0577 008AC 01745 0125 0577 008AD 005C8 0125 0577 008AE 005C9 0125 0577 008AF 005CA 0125 0577 008B0 005CB 0125 0577 008B1 005CC 0125 cat test.sh #!/bin/ksh... (4 Replies)
Discussion started by: mbak
4 Replies

2. Shell Programming and Scripting

Trying to run a basic for loop

OS : RHEL 6.1 Shell : Bash I had a similair post on this a few weeks back. But I didn't explain my requirements clearly then. Hence starting a new thread now. I have lots of files in /tmp/stage directory as show below. I want to loop through each files to run a command on each file. I... (8 Replies)
Discussion started by: kraljic
8 Replies

3. Shell Programming and Scripting

Run the for loop in parallel

I have the below code which runs on multiple databases , but this runs one-after-one. I will need this to run in parallel so that i could save a lot of time. Please help!!! Thanks in advance for Db in `cat /var/opt/oracle/oratab |egrep -v "ASM" |grep -v \# |cut -d\: -f1` do { export... (5 Replies)
Discussion started by: jjoy
5 Replies

4. Shell Programming and Scripting

How to run it in the loop??

I have this code awk -F, ' { C5+=$5 C6+=$6 C7+=$7 C8+=$8 R=$5+$6+$7+$8 T+=R } { print $0,R } END { print... (7 Replies)
Discussion started by: nikhil jain
7 Replies

5. Shell Programming and Scripting

Script for telnet and run one command kill it and run another command using while loop

( sleep 3 echo ${LOGIN} sleep 2 echo ${PSWD} sleep 2 while read line do echo "$line" PID=$? sleep 2 kill -9 $PID done < temp sleep 5 echo "exit" ) | telnet ${HOST} while is executing only command and exits. (5 Replies)
Discussion started by: sooda
5 Replies

6. Shell Programming and Scripting

How do I run a shell command in a while loop?

The command is: sic -h irc.freenode.net 2>&1 | tee -a irc.log Where sic is an IRC client, and I'm piping the output to tee in order to log my IRC sessions. I'm trying to handle reconnects by running it in a while loop in the shell process and cat the initial commands into sic's stdin. I... (1 Reply)
Discussion started by: guitarscn
1 Replies

7. Shell Programming and Scripting

For loop to run external program

Hi, I was hoping for help with a for loop to run a program (vina) repeatedly using all the files in a folder as input. Currently my code looks like this: #!/bin/bash FILES=/home/afalk/Desktop/battest/*.pdbqt for f in $FILES do vina --config /home/afalk/Desktop/A.txt --ligand "$f".pdbqt done... (5 Replies)
Discussion started by: oldmanwinter
5 Replies

8. Shell Programming and Scripting

How To Run A For Loop In A Remsh?

Hi all, I'm trying to remsh to another server and then execute a for loop command there but I'm getting unexpected errors and would appreciate any suggestions. Ideally what I want to do is this: for host in `cat host_file` do remsh $host -n " cd /home/ for DATABASE in `ls -d... (5 Replies)
Discussion started by: Korn0474
5 Replies

9. Shell Programming and Scripting

Is there a better way I could have run this loop. (For loop with two variables)

Sorry for such a dreadful title, but I'm not sure how to be more descriptive. I'm hoping some of the more gurutastic out there can take a look at a solution I came up with to a problem, and advice if there are better ways to have gone about it. To make a long story short around 20K pieces of... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

10. Shell Programming and Scripting

Script doesn't work as expected when run on cron

The script checks for free space stats on Oracle. If there are any tablespaces with more than 85% usage it prints the details of the tablespace. If all the tablespaces have more than 15% free space, then "All tablespaces have more than 15 pct free space" must be printed on the screen. When I run... (2 Replies)
Discussion started by: RoshniMehta
2 Replies
Login or Register to Ask a Question