Informing the IBM Community

Masterclass: Using the Storwize CLI for repetitive commands



In my last article I introduced you to running some basic commands on the Storwize CLI such as lsvdisk and lsmdisk. I also showed you how to use the -filtervalue command to display a subset of items.

Quite often, we want to execute a specific operation on a selection of items. So in this article we’ll look at two options for looping around a number of selected items so that we can run the same command against each one of these items.

The FOR loop

The FOR loop is most useful if you use a good naming convenient for volumes and mdisks. The Storwize CLI is a restricted BASH shell so you can use the normal FOR loop processing provided by BASH.

Let’s say you want to change the cache attribute for a set of volumes to none. We could use:


The loop will execute 20 times automatically incrementing X from 1 up to 20. The $X variable is replaced by the current value of the loop.

While this works, it is not compatible with my standard volume-naming convention which is LPARNAME_Lnn. The loop above would assume that my first nine volumes are named MYLPAR_L1/L2/L3 etc when, in fact, they are named MYLPAR_L01/L02/L03.

To make sure that the CLI uses leading zeros for my first nine volumes, I need to use the following command:


This takes the value of $X and pads it out with a leading zero if the number is between 0 and 9. So my modified command from above now looks like:


Note that the printf command is surrounded by backtick characters and not single quotes. On my UK keyboard the backtick is located to the left of the number 1 key. This instructs the CLI to execute the command within the backticks and return the results directly into your loop.

I’ve been using the printf method for a couple of years but I’ve recently discovered that the following also works and is easier to use:


If I wanted to execute my command against a specific set of volume numbers, I could also use:


This would run the chvdisk against the volume numbers listed.


This is a little more complicated but also more flexible. When we run an lsvdisk command, a stream of information is returned for each vdisk selected. Each attribute returned is separated by a space. The while read loop can interrogate each line of data returned and assign the attributes to field names you define.


When we run the lsvdisk command, we can see that the first field returned is always the volume’s ID, the second field is always the volume’s name etc. Knowing this allows us to use the a WHILE READ loop:


Let’s break this down:


This will return all the information for volumes where the name starts MYLPAR_L. The -nohdr parameter tells the CLI not to display the column headings, just the raw data.

Storwize 8

Firstly, notice the pipe character | at the beginning. This instructs the CLI to send the output from the lsvdisk command to the while read command instead of sending it to your screen. The pipe character on my UK keyboard is SHIFT+\

The while read loop processes each line of data from the lsvdisk command as if it is a record in a file. By default, while read assumes that each field in your data is separated by a space. In this example, I am only interested in field number 2, which is the name of the vdisk. I assign the following names to the data returned:

Field 1 – ID
Field 2 – NAME
Fields 3 to end of line – RESTOFREC

Notice that I even though I am not interested in the data following the NAME field, I still have to assign a name to it. Hence I have called this RESTOFREC.


Here we run the chvdisk -cache none command against the volume named within the $NAME field I assigned previously. The chvdisk command allows us to specify either a volume name or volume ID so I could have made the loop shorter using:


In this loop I have only assigned a field name to the ID value and ignored the remainder of the data.

Getting some feedback

You’ll find that you don’t get any confirmation back from the CLI when commands run successfully so it’s handy to put something in your loop to indicate what’s happening. I normally use the echo command to do this.

Storwize 11

I’ve simply added echo “Changing volume ” $NAME so that I can see on the screen which volume is being changed. Notice that I use the volume name in the echo command but the volume ID in the chvdisk command. I could have used ID or NAME on both commands.

Laying out your script for readability

You can enter your script text as a single line of commands separated by a semicolon as I’ve shown above. I would normally enter my commands into Notepad or another text editor to get the formatting looking more readable. For example:

Storwize 12

You can then copy and paste the above into the CLI to have these commands executed.

I also prefer to place an echo command in front of the CLI command I’m going to execute: chvdisk in this example. This allows me to run the script but simply display the commands I want to run so that I can check them first. Once I’m happy with the commands, I’ll remove the echo command so that the chvdisk can execute.

More on WHILE READ loops

There are more ways to use WHILE READ loops and we’ll continue to look at that in the next article where I’ll describe using arrays within our loops.

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.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.