Looking to minimize 'for' loops in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looking to minimize 'for' loops in script
# 1  
Old 01-23-2017
Looking to minimize 'for' loops in script

Hi, Below is the script that I came up with but looking to see if there is a more appropriate way to achieve this by reducing number of "for" loops or something.

Regards,
mbak

Code:
#!/usr/bin/ksh
status=missing
for disk in `lspv | awk '{print $1}'`
do
MISSPATH=`lspath -l ${disk}  -s ${status} -F "name parent path_id connection" | awk '{print $3}'`
	for path in ${MISSPATH}
	do
		rmpath -d -l ${disk} -i ${pathid}
	done
done

status=failed
for disk in `lspv | awk '{print $1}'`
do
FAILPATH=`lspath -l ${disk}  -s ${status} -F "name parent path_id connection" | awk '{print $3}'`
	for path in ${FAILPATH}
	do
		rmpath -d -l ${disk} -i ${pathid}
	done
done

# 2  
Old 01-23-2017
Like this?
Code:
#!/usr/bin/ksh
for status in missing failed
do
  for disk in `lspv | awk '{print $1}'`
  do
    for path in `lspath -l ${disk}  -s ${status} -F "name parent path_id connection" | awk '{print $3}'`
    do
      rmpath -d -l ${disk} -i ${pathid}
    done # end path
  done   # end disk
done     # end status

This User Gave Thanks to Ivo Breeden For This Post:
# 3  
Old 01-24-2017
Noticed that variable names do not match: for path vs. ${pathid}.
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 01-24-2017
In the previous suggestion the two outer loops can be swapped, so lspv is run once.
The following suggestion does not run lspv at all:
Code:
for status in missing failed
do
  lspath -s $status -F "name parent path_id connection" |
  while read dname dparent dpath_id dconnection
  do
    echo rmpath -d -l "$dname" -i "$dpath_id"
  done
done

Remove the echo when you think it works.
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

script for connecting database in loops

Hi experts. I have different database and schema and their passwords in one file. In Unix : I want to create the script that connect the sqlplus and execute the select query one by one schema with different database. Ex: password File Format databse schemaname password DB1 name1 ... (1 Reply)
Discussion started by: abhigrkist
1 Replies

2. Shell Programming and Scripting

Script loops again and again and again and ...

Hi, Linux newbie here with what I'm guessing is silly questions. My script below is working in that it correctly copies files from the backup IP (10.0.91.1) back down to the Linux server but trouble is it loops continuously. It correctly downloads 100 files from the the IP 10.0.91.1... (1 Reply)
Discussion started by: MOWS
1 Replies

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

4. UNIX for Advanced & Expert Users

Conky apps key binding so that no need to minimize the windows open to see desktop

Hi I would like to ask if someone try or is there any key binding about the conky apps..I would like to know if it possible to key bind the conky running in desktop so that everytime i want to see the running conkyrc on the desktop there is no need for me to minimize the open windows inorder... (0 Replies)
Discussion started by: jao_madn
0 Replies

5. Shell Programming and Scripting

Minimize command line

Hello, I am looking for help to minimize this commande line. The commande is working fine but I tried to make it shorter ... It's about to get rid of some characters. | sed '/NODE*/d' | cut -d "'" -f 2 | sed '/;;/d' | sed '/ /d' | sed 's///g' Thanks for your help (8 Replies)
Discussion started by: Aswex
8 Replies

6. Shell Programming and Scripting

Mailing script using loops

Hi all.. I'm not a scripter but I'm trying to set up a mail script that emails out once a file has been written to. Is there some way of writing a while read loop or something, so that it reads each line of the file and then ends when a specific number of lines have been written... We're... (2 Replies)
Discussion started by: Jazmania
2 Replies

7. Shell Programming and Scripting

Enhancing Script (using loops) to go through for each group

Currently I have written a shell script that will add departments for one group (displayed below with configuration file). I want to enhance the script in order to add departments to multiple groups by: 1. Making changes to allow the script to loop through and write each dept/group combo into... (0 Replies)
Discussion started by: dolo21taf
0 Replies

8. UNIX for Dummies Questions & Answers

Equivalent unix command to minimize/maximize xterm window

To all experts, I need some advice on how I can enter a unix command which is equivalent to the action of minimizing/maximizing an active xterm window. How can I do this (i) when control is in the active xterm window to be minimized/maximized , & (ii) when control is in a xterm window which runs... (0 Replies)
Discussion started by: icemocha75
0 Replies

9. UNIX for Dummies Questions & Answers

Minimize ksh

Dear, Have any command which can minimize the ksh windows? I use telnet to connect to server. Wilson (7 Replies)
Discussion started by: wilsonchan1000
7 Replies
Login or Register to Ask a Question