Connect With Us

Skip the Script and Try the Apply Command

The apply command helps you process parameters in a single line

Simple tools can save your day, as Anthony English explains in this article on the AIX apply command. Simplify your command line loops and scripts using this trick.

In this article, I’ll present a nifty little trick for running a command on a set of parameters. This trick uses the AIX apply command. Apply can be easier to use than more elaborate tools such as xargs, awk, or perl, so it's a helpful command to have up your sleeve. As you'll see, you can write a one-liner using apply and save yourself from creating a more elaborate loop or writing a script.

You can use apply to create simple reports; for example, displaying one of the attributes for a set of disks. First, I'll show how you do that using the more traditional for loop, then I’ll show you how it can be done using apply.

Looping the Loop

Suppose you have an I/O bottleneck and want to get the queue_depth attribute for all your physical volumes. You could run lsattr on each physical volume, with the disk name and the queue depth attribute indicated by the -a flag:

lsattr -El hdisk0 -a queue_depth
queue_depth 3 Queue DEPTH True

To do that for several disks (physical volumes), you can use a loop. To run the lsattr for hdisk0 through hdisk2:

for i in hdisk0 hdisk1 hdisk2
do
    lsattr -El $i -a queue_depth
done
queue_depth 3 Queue DEPTH True
queue_depth 20 Queue DEPTH True
queue_depth 20 Queue DEPTH True

You could make that a tad cleaner by placing just the disk numbers in the top of the for loop:

for i in 0 1 2
do
    lsattr -El hdisk${i} -a queue_depth
done
queue_depth 3 Queue DEPTH True
queue_depth 20 Queue DEPTH True
queue_depth 20 Queue DEPTH True

This requires you to type in the numbers of each physical volume (hdisk) you're checking. That gets a bit cumbersome if you have a large number of physical volumes, especially if they aren’t numbered sequentially. So, rather than hard-coding the physical volumes or storing their names in a file, it would be better to get a list of the physical volumes using the lsdev command. That way, you're working off an up-to-date list of physical volumes each time you run the command. Better still, the exact same command would work on a different AIX logical partition, even if that partition had a completely different set of hdisk numbers.

You can get the list of disks using the lsdev command:

lsdev -c disk -F name
hdisk0
hdisk1
hdisk2

You could then include that command in your original loop:

for i in $(lsdev -c disk -F name)
do
     lsattr -El $i -a queue_depth
done

Apply Now

You can do something similar using the apply command. The syntax for apply is surprisingly simple:

apply CommandString Parameter

Or, if you prefer, “apply this command to these parameters.”

Here's a simple example: Suppose you want to display a list of words using the echo command, with one word on each line. You can run the following:

apply "echo" one two three four five six seven eight nine
one
two
three
four
five
six
seven
eight
nine

This example had one command (echo) and nine parameters, and the echo command ran for each parameter. But suppose you want the apply command to run on every third parameter. You would do that by inserting %3 into your command string. Here, I'll echo every third parameter:

apply "echo %3" one two three four five six seven eight nine
three
six
nine

Apply to All Disks

Now that you've seen the apply command in action, it's very easy to get it working to display the queue_depth disk attribute for all disks. You can run the command lsattr on every disk that’s listed when you run lsdev. Here's how that looks:

apply "lsattr -El %1 -a queue_depth" $(lsdev -c disk -F name)
queue_depth 3 Queue DEPTH True
queue_depth 20 Queue DEPTH True
queue_depth 20 Queue DEPTH True

Let's enhance it a little. As you saw before, you can display every parameter by running the echo command:

apply "echo %1" $(lsdev -c disk -F name)
hdisk0
hdisk1
hdisk2

Putting it All Together

Here's how to combine the two commands. First, run the echo command to display the disk name. If the echo command is successful (indicated by a double ampersand, &&), run the lsattr command:

apply "echo %1 && lsattr -El %1 -a queue_depth" $(lsdev -c disk -F name)
hdisk0
queue_depth 3 Queue DEPTH True
hdisk1
queue_depth 20 Queue DEPTH True
hdisk2
queue_depth 20 Queue DEPTH True

Simple Math

How about some basic math? The following is the syntax for performing multiplication using the expr command. The multiplication is indicated by the asterisk (*) and needs to be escaped by a backslash.

expr 100 \* 2
200

So, suppose you had a set of numbers. You could multiply each number by two using apply and expr:

apply "expr %1 \* 2" 1 2 3 4 5 6
2
4
6
8
10
12

Wider Application

Once you think about it, I believe you'll find that there are many uses for apply. For example, instead of using apply to run a single command against a set of parameters, you could use it to run a script. Apply is both simple and powerful. If you're not familiar with apply, avoid using it to make any changes; instead, use it initially just to list details (e.g., list attributes of devices, volume groups, file systems, or users). I think you’ll find that it's a pretty good tool to have in your systems admin arsenal.

Discuss this Article 1

dxtans
on Mar 6, 2013

In all my years doing AIX, never knew about the apply command. Look's quite handy to me !

dxtans

Please or Register to post comments.

AIX User-Related Commands

Download AIX User-Related Commands (Infographic) today! This quick guide highlights differences between user administration commands on AIX and other UNIX-derived operating systems.


POWER IT Pro Blogs
LPI Exam Objective 101.3: Runlevels, Shutdown, and Reboot
May 13, 2013
blog

LPI Exam Objective 101.3: Runlevels, Shutdown, and Reboot

This entry in the LPI certification exam blog series discusses the third objective in the System Architecture topic of the exam: Linux runlevels, and shutdown and reboot procedures....More
May 13, 2013
blog

Feedback Loop

I had more feedback from readers to my article, "The Forgotten", than practically anything I have written before. If you missed it, it told the story of a conversation I had with the financial director a mid-sized British food retailer....More
May 13, 2013
blog

COMMON Europe Cancels Flagship Conference

COMMON Europe has cancelled its flagship annual conference, due to be held next month in Annecy, south-eastern France....More
eBooks & Training
Secrets of an AIX Administrator by Christian Pruett
Dec. 13, 2012
Datasheet

Secrets of an AIX Administrator  

Download this Tech Advisor from POWER IT Pro and learn practical, real-world information about AIX administration. Christian Pruett shares all the things he wishes he had known about AIX before becoming an admin in his ebook Secrets of an AIX Administrator....More
Dec. 3, 2012
Datasheet

AIX Security  

IBM i has extremely robust intrinsic security, and Linux, although much less secure than either AIX or IBM i, has many more eyes watching for problems. But AIX has some significant corner cases where security can be breached if you're not careful....More