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.





I think the lscfg command can accept this:
lscfg -vl fcs* | grep Net
I skip spaces before and after the pipe key, unless I'm showing someone else or putting it into documentation. So you could make it shorter using:
lscfg -vlfcs*|grep Net
and the lsdev command can be shortened using xargs. BTW, with lsdev you don't need the upper case C, although everyone uses it. Once again, give your space key a rest:
lsdev -cdisk|awk '{print $1}'|xargs -n1 rmdev -dl
The other one I use is when I am in a long command line that I want to cancel with ctrl-c, but may come back to, I turn it into a comment with two keys: Esc and #
(Okay. I admit, the # key requires you to press two keys). Then it's in the shell history for easy recall with Esc k.
Loved the tip to press v to turn it into a vi session. I've done it thousands of times by mistake and wondered which key did it.
Posted by: Anthony English | December 04, 2012 at 07:16 AM
Hello.
I use
for D in $(lsdev -c disk -F name)
do
S=$(bootinfo -s $D)
printf "$D\t$S\n"
done
... all the time
Posted by: Ramon Barrios Lascar | December 04, 2012 at 07:39 AM
Hello.
I use
lsdev -c adapter -l fcs* -F name | xargs -n1 chdev -a reserve_policy=no_reserve -l
... all the time too
Posted by: Ramon Barrios Lascar | December 04, 2012 at 07:40 AM
The part about looping on a set of values reminded me of "seq". I missed it from my Linux days, and so had written an imitation in perl before realizing that the "AIX Toolbox for Linux Applications" page at http://www-03.ibm.com/systems/power/software/aix/linux/toolbox/alpha.html has it packaged in the coreutils RPM.
Posted by: Jeff Schaller | December 04, 2012 at 08:19 AM
i'm using this one all the time (small variation of the above):
#lsdev -c adapter -l fcs*|awk '{print "chdev -l" $1" -a reserve_policy=no_reserve}'|ksh
...which gives me the output of the commands if i take out the "|ksh" part, useful to check before executing the commands.
Posted by: nixysug | December 04, 2012 at 04:59 PM
My favorite trick for when I'm doing things with devices names is to change into the /dev directory and then use a 'for' loop to loop through the devices I need:
# cd /dev
# for D in hdisk*
> do
> printf "%s: " $D
> bootinfo -s $D
> done
hdisk0: 140013
hdisk1: 140013
hdisk10: 25600
hdisk11: 25600
hdisk12: 25600
hdisk13: 107550
hdisk14: 107550
hdisk15: 107550
hdisk16: 107550
hdisk2: 140013
hdisk3: 140013
hdisk4: 140013
hdisk5: 20480
hdisk6: 20480
hdisk7: 20480
hdisk8: 20480
hdisk9: 25600
hdiskpower0: 20480
hdiskpower1: 25600
hdiskpower2: 107550
R=0 root@hostname /dev
Posted by: Anker Lerret | December 05, 2012 at 12:32 PM
When using the v key to pull the command line into vi, it is important to remember that you are committed to running something at that point. If you try to :q!, it will run what you started with. So once in vi, if you decide to abort, you have to either comment out the command or delete everything and then :wq
Posted by: Bill | December 06, 2012 at 10:30 AM
This works wonders for getting the fcs name, location, and wwpn.
lscfg -vl fcs*|egrep 'fcs|Network'|awk '{print $1,$2}'
Posted by: Jon K | December 06, 2012 at 12:58 PM
If you're running ksh93 for your shell, to get the 12 thru 25 increment, you can use the more "C-like" structure:
# x=11;while (( (x++) %3C 25 )); do
> rmdev -dl hdisk$x
> done
I also like combining output from different commands into a more column-oriented "report." Then you can pipe the output from the while loop to awk to pick out exactly what output you like to see:
# x=11;while (( (x++) %3C 25 ))
> do
> print -n "hdisk$x:\t"
> print -n "$(lsdev -l hdisk$x) "
> print "$(lspv hdisk$x|grep 'TOTAL PPs:')"
> done | awk '{print $1, $3, $4, $11, $12, $13, $14"MB"}'
Posted by: Bill Harvey | December 07, 2012 at 10:50 AM
Occassionally we have a volume group with several filesystems, usually with nested mounts (/a, /a/b, /a/b/c). Here is a quick way to mount all the filesystems in the volume group in the proper order. This comes in very handy when dealing with clusters
lsvgfs some_vg|sort|while read fs; do mount $fs; done
With minor modifications this can be used to umount the filesystems in the proper order. Simply change 'sort' to 'sort -r', and 'mount' to 'umount'.
Posted by: Mike Discenza | December 11, 2012 at 10:05 PM
I'm a fan of the least known command "apply", so for you first command.
apply "lscfg -vl fcs%1" 0 1 2 4 | grep Net
For your for loop, why not use "while :" or even shorter form for all the script.
while df
do
sleep 5
done
(either way around)
For the command listed as;
lsdev -cdisk|awk '{print $1}'|xargs -n1 rmdev -dl
I always find the awk not required, just select the correct field (-r name)
apply "rmdev -dl %1" $( lsdev -cdisk -rname )
Posted by: Steve Maher | December 12, 2012 at 03:44 PM
I have found that using ctrl c and :q! successfully gets me out of the v key command pulled into vi.
Posted by: Larry DeWall | January 14, 2013 at 09:12 AM