Command Line Shortcuts

Edit: An oldie but a goodie.

Originally posted December 4, 2012 on AIXchange

What are your favorite scripting command line shortcuts? When I have a relatively small pile of repetitive things to do, I like to create a for loop, such as:

#for i in 0 1 2 3

>do

>lscfg -vl fcs$i | grep Net

>done

In this case I can easily get my WWPNs from my fibre cards.

If you’ve already run set –o vi and you recall your command history with esc-k, you might end up with something like this on your command line, ready for you to rerun:

#for i in 0 1 2 3^Jdo^Jlscfg -vl fcs$i | grep Net^Jdone

Though it’s certainly easy enough to go back in and edit that directly on the command line using normal vi keys, sometimes with the ^J characters and the lack of spacing — especially if it’s a long command that wraps around on the command line — it can be easier to enter v somewhere on that line and pull yourself into a vi editor session. That makes it easier to work on the command in question:

for i in 0 1 2 3

do

lscfg -vl fcs$i | grep Net

done

When you’re done with your edits, just save out of vi as you normally would, and the command that you put together will run as if it had been edited on the command line.

Here’s another loop I sometimes use:

while (true)

do

df

sleep 5

done

This basically runs the df command every 5 seconds. It will do so forever.

One way to easily remove all hdisks from a system is to run:

for x in `lsdev -Cc disk|grep hdisk|awk ‘{print $1}’`

do

rmdev -dl $x

done

Obviously, it will not rmdev disks that are in use, but I find that on systems with hundreds of hdisks that I want to manipulate for some reason, this can be a handy way to do some cleanup.

Otherwise, if you had some lists of values that you needed to loop on — say you need to delete hdisk12-25 — you could first run:

x=12 to set $x equal to 12, then you could run either

>while [ $x -le 25 ]

> do

> rmdev -dl $x

> ((x=$x+1))

> done

or

while (($x<=25)); do

> rmdev -dl $x

> let x=$x+1

> done

What other simple things do you run on the command line to make your job easier? Please share your tips in Comments.