Never heard of it? Well, welcome to the club. Two colleagues and I had over 60 years’ combined experience on AIX and, between us, we had never once come across “apply”.
So, what does it do and why should you learn it? Very simply, “apply” will run a command for a set of parameters. It is useful, for example, if you want to change a tuning parameter for a whole set of disks or other devices. Or maybe you need to list some information from a number of files. Your friend is “apply”.
If you are already familiar with some basic scripting, you will probably see some similarities to running a “for” loop.
Here i s a basic use of the apply command. Let’s apply the command “echo” to a list of words.
apply “echo” all cows eat grass all cows eat grass
Not terribly exciting, I suppose, but the syntax is a good deal easier than using a “for” loop. Here’s how it would look:
for i in all cows eat grass do echo $i done
With “apply” you only need to specify “apply” and the command (“echo”, in this example).
How about replacing the words about bovine culinary habits with something a little more technical? I am going to list the attributes for a set of physical volumes.
The command to list the attributes would be:
lsattr –El hdiskN
So, to use the “apply” command, it would be:
apply “lsattr –El” hdisk0
The chances are that you would like to do this for a number of disks; maybe even all the disks on your AIX system. To list the disks, you could use this command:
lsdev –c disk –F name
That would produce a list of disks, such as:
hdisk0 hdisk1 hdisk2
and so on.
In the apply command, instead of listing those disks one by one, you can specify the command to list them. It needs to be either in backticks:
`lsdev –c disk –F name`
or use the $( command ) syntax:
$(lsdev –c disk –F name)
Now it is time to put it all together. As the lsattr command which we’re applying to the list of disks has spaces in it, you need to put it in quotes:
apply “lsattr –El” $(lsdev –c disk –F name)
This can be really handy if you want to make changes, too.
Here’s the command to change the queue depth for one disk:
chdev –l hdisk0 –a queue_depth=16 –P
To do that for all of the disks, you would substitute the disk name (hdisk0) with the %1. This is to indicate the first parameter generated in each of the lists of the apply command.
Here we go:
apply “chdev –l %1 –a queue_depth=16 –P” $(lsdev –c disk –F name)
Here’s how to do a tail command to look at the last 20 lines of several log files:
apply “tail -20 %1” $(ls *.log)
You might think you could just run this:
tail -20 *.log
but the tail command can only process one file at a time.
Here’s another example. Suppose you want to make a backup of a set of files before changing them. There are a lot of ways to do this, but the apply command would work very well:
apply “cp %1 %1.bak” $(ls *.cfg)
Once you get used to the slightly unusual syntax of the apply command, you will probably start to love it. I use it all the time, especially if I need to get a command running in a single line.
Having worked on IBM systems since 1991, Anthony English has seen how AIX and virtualisation can manage all kinds of business environments. He is a well-recognised author in the IT field and he writes about business systems improvement through his blog at https://anthonyenglish.com.au. Anthony is based in Sydney, Australia, with his wife and his seven children.