In my previous article, I showed you how to use WHILE READ and the FOR loop to process multiple items in the Storwize CLI. To refresh your memory, this is an example of how we can use a WHILE READ loop:
When the WHILE READ processes a stream of data – in this case, all the details for a vdisk – we need to split out the data by assigning our own field names. In the example above I have created three fields:
● ID – the first field in the stream containing the vdisk identifier
● NAME – the second field in the stream containing the vdisk name
● RESTOFREC – all remaining fields in the stream.
The stream of data returned by the lsvdisk command uses a space character as the field separator and the WHILE READ function uses the space character by default to split the fields.
WHILE READ into an array
If you run an lsvdisk command you will notice that there are many fields. If I wanted to access the value of the FC_name field, I would have to assign a field name to all of the fields that preceded it. Here’s an example of the lsvdisk output:
So I need to assign a name to all of the fields up to the one I’m interested in even though I may never refer to these fields. This is where the array processing comes in. I can amend the line above as follows:
This is much simpler. What we’ve done is to move the stream of data into an array by specifying -a after the read command. The READ WHILE operation assumes that each field is separated by a space character and places the data into the array variable VDISK. Now we simply use the array element number to retrieve the fields we’re interested in. Keep in mind that Linux arrays start from element number 0.
Retrieving data in CSV format
In the Storwizwe GUI many of the screens provide you with an Export button so that you can have the data selected exported to CSV format for import into your favourite spreadsheet application.
This is simple to do from the CLI too using the -delim parameter available on many of the ls* commands.
In the following example, I’ll display information on volumes mapped to a host on the Storwize system along with the SCSI ID of the mapped volumes. To do this, we use lshostvdiskmap and specify -delim “,” to replace the default field separator space character with a comma.
The use of -delim “,” is pretty pointless here but I wanted to highlight the use of the IFS= in the WHILE READ loop.
As previously mentioned, WHILE READ will expect a space character as the field separator. By placing IFS=”,” after the while command, we are overriding the default separator and instructing the WHILE READ processing to use to use the comma character as the field separator. Note this is only for the read operations. My echo command places the comma into the output stream after each array element I have specified.
Executing V7000 CLI commands from a remote host
In future articles I will demonstrate how to execute V7000 CLI commands from a remote host using SSH. Running a single command is simple but when you need to run multiple commands there are some considerations to be aware of.
I have been collecting some handy one-liners for the Storwize CLI. These can be viewed at developerWorks here.
Glenn Robinson has been PowerWire‘s resident tech tipster for over a decade. He has worked in the IBM computing arena since 1986 on System/34/36/38 and AS/400 through to Power Systems today.
As systems architect for IBM systems and storage at RSI Consulting, based in Northampton, UK, he works predominantly with IBM i clients as well as those using AIX and Linux. In the last five years, he has also had a great deal if experience working with Power customers using SVC and V7000 storage virtualisation.