Linux Help: Creating 'For' and 'While' loops

 Introduction

 

Sometimes you need to run a command multiple times on a system or repeat a task over and over.  This is where a 'for' loop will come in handy.  In the article below I will site some examples of useful 'for' loops to make life a little easier.

 

BE CAREFUL: Make sure to test your command prior to running your loop.  Instead of making one mistake you could make hundreds.

 

 

Creating your 'For' loop

 

Example #1

 

Say you had a corrupt blob on a target system and wanted to check all the source systems to see if that blob was present.  The long method would be to login to each of the source systems and run the 'ispresent' command to see if it existed.  I will show you how to create a file with the source system IP address and then run a 'for' loop to check each system for that blob.

 

Create your file for source IPs

# vi /scratch/source_IPs

 

10.20.234.79

10.20.234.59

10.20.234.63

 

Test your loop

[root@SES6702DXi55 scratch]# /hurricane/blockpool/bin/blockpool ispresent CA47987E866C62D43AF5F2D1EB47C5E4 +Nlocal@localhost +Nremote/SHA-AES:password@10.20.234.79

 

Run your loop

[root@SES6702DXi55 scratch]# for i in `cat /scratch/source_IPs`; do /hurricane/blockpool/bin/blockpool ispresent CA47987E866C62D43AF5F2D1EB47C5E4 +Nlocal@localhost +Nremote/SHA-AES:password@$i; done

 

Output:

Blockpool Developer V3.8.7 DXi (29-Oct-2013) r48088

---------------------------------------------------

Copyright 1992-2013. Quantum Corporation. All Rights Reserved.

DXi, StorNext and Quantum are either trademarks or registered

trademarks of Quantum Corporation in the US and/or other countries.

Protected by US Patent #5,990,810.

Connecting to server "10.20.234.79"...

Successfully connected.

The BLOB is present.

BLOB Length (bytes): 1048576.

Reference Count:     1.

Flags:               Full hash|Support non-linear|New stats.

Unique Clusters:     1.

Lowest Cluster:      48.

Highest Cluster:     48.

Total memory malloced was 24298K.

 

Started at 31-Jan-2014 16:31.19

Ended   at 31-Jan-2014 16:31.19

Running time was 0.01 seconds.

Blockpool Developer V3.8.7 DXi (29-Oct-2013) r48088

---------------------------------------------------

Copyright 1992-2013. Quantum Corporation. All Rights Reserved.

DXi, StorNext and Quantum are either trademarks or registered

trademarks of Quantum Corporation in the US and/or other countries.

Protected by US Patent #5,990,810.

Connecting to server "10.20.234.59"...

Successfully connected.

The BLOB is NOT present.

Total memory malloced was 24298K.

 

Started at 31-Jan-2014 16:31.19

Ended   at 31-Jan-2014 16:31.19

Running time was 0.04 seconds.

 

 

Example #2


For some reason you want to delete a bunch of tags from blockpool.

Create your file for blockpool tags to delete

# vi /scratch/blockpooltags

 

B81A470FD30464716AFE97A12BB47059

B877868A3F2B30FCDD1AE89481333339

BBC7A50FB40E8F0C021DBBE8CCF4E0F3

BD55B4CB0C4425BD3014705E453B28C8

C03F78AEAF8AD0E2C6D927F8749EA3A6

C37D2EAA8EA172A5D6F54168BD3D3CC2

C66EB18637D1C73CB561DDBF3DF0196D

C73053C4214CAE9D24E6B0623927C773

CA47987E866C62D43AF5F2D1EB47C5E4

CA6F0A6EB27B97D3F69D12F8023A4042

CB13041374B2568AE5A9F69E14EF6E24

CC3C17AE636334199AB604E5B02BA726

CF2172D84CF922B83A5D36BFB4CE8024


# for i in `cat /scratch/blockpooltags`; do /hurricane/blockpool/bin/blockpool delete $i +Nlocal@localhost; done

 

Example #3


'For' loops can be run against files in the collect log too.  Say you want to find out how many replication trigger requests there are per day.  This is helpful is determining if the TBR queue is catching up or falling more behind on a daily basis.  While ssh'd into SRSCRATCH you can run the following command.  We will grep out  "Got Trigger Message for sharition" from the tsunami.log and we only care about days 1-24 in April.  The command would look like this: 

[emerritt@srscratch:~/active_cases/Wood-Group-1.3.14/scratch/collect/node1-collection]$ for x in {01..24};do echo -ne "Requests for 4/$x:  " && grep "Got Trigger Message for sharition" app-info/tsunami.log*|grep "4/$x"|wc -l;done
Requests for 4/1:  17929
Requests for 4/2:  0
Requests for 4/3:  0
Requests for 4/4:  0
Requests for 4/5:  0
Requests for 4/6:  0
Requests for 4/7:  0


'While' Loops

"while' loops are just a way to repeat a command and walk away.  These don't need much explaination so I will just list some examples.  The "sleep" statement is in seconds.


while true; do SMcli Qarray2a -S -c "show volume [TRAY_85_VOL_7] actionProgress;"; sleep 15; done

while true; do df -k; sleep 60; done

while true; do SMcli 10.17.21.31 -S -c "show volume [TRAY_85_VOL_1] actionProgress;"; SMcli 10.17.21.31 -S -c "show volume [TRAY_85_VOL_2] actionProgress;"; SMcli 10.17.21.31 -S -c "show volume [TRAY_85_VOL_3] actionProgress;"; sleep 15; done

 

 

Conclusion

 

Loops are a great shortcut in supporting the DXi product.  If you can think of other examples of 'for' loops that you want to run and can't figure out the proper syntax, email me and I will try to help out.  Eric.Merritt@quantum.com



This page was generated by the BrainKeeper Enterprise Wiki, © 2018