Thursday Jan 27, 2005

fuser trickiness

I encountered this one a bit over a year ago while having a conversation with Ed in MPK (or is it a year and a half?). The fuser command is used to list the processes using a file system - fuser /home/bubba would list all the users of /home/bubba; and you can kill them too, using the -k option. So far so good.
What happens, though when you want to do something with the output?
haiiro[64i]~% fuser /home/bubba
/home/bubba:     6780c    6760c    6758c    6756c    6502c    6422c    6404c   6368c    6332c
6331c    6330c    6322c    6308c    6301c    6206c    6081c    5951c    5879c    5756c    5251c
5247c    5201c    5092c    5004c    4992c    4987c    4985c    4982c    4980c    4968c    4952c
4920c    4672c    4080c    4079c    3990c    3989c    3987c    3985c    3983c    3981c    3979c
3975c    3973c    3969c    3963c    3943c    3939c    3937c    3935c    3898c    3896c    3893c
3891c    3882c    3880c    3836c    3834c    3831c    3769c    3726c
it's a bit hard to parse? not so! What you can do is redirect stderr to /dev/null and magic happens
haiiro[64i]~% fuser /home/bubba 2>/dev/null
6781    6760    6758    6756    6502    6422    6404    6368    6332    6331    6330
6322    6308    6301    6206    6081    5951    5879    5756    5251    5247    5201
5092    5004    4992    4987    4985    4982    4980    4968    4952    4920    4672
4080    4079    3990    3989    3987    3985    3983    3981    3979    3975    3973
3969    3963    3943    3939    3937    3935    3898    3896    3893    3891    3882
3880    3836    3834    3831    3769
No parse issues there. Then you can get full process information on each of the processes using a small bit of shell:
haiiro[64i]~% ps -o pid,args -p "$(fuser /home/bubba 2>/dev/null)"
  PID COMMAND
 6206 /usr/openwin/bin/xterm -geom 80x25 -e /bin/zsh
 3943 /usr/lib/evolution/1.4/evolution-alarm-notify --sm-config-prefix /evolution-ala
 3939 nautilus --sm-config-prefix /nautilus-udaGgf/ --sm-client-id 11819cee2200010909
 3935 metacity --sm-save-file 1106677632-2610-3195804027.ms
 3880 /bin/ksh /usr/dt/config/Xsession2.jds
 3834 /usr/dt/bin/sdt_shell -c      unset DT;     DISPLAY=:0;       /usr/dt/bin/dt
:
:
:
Just what the doctor ordered for a sysadmin.

Wednesday Jan 26, 2005

Exit status from commands in a pipeline (ksh)

It's already answered in the comp.unix.shell FAQ, so stop asking me!
For the most part it's the slightly more compatible shells that require some work. As the ksh that ships in Solaris needs to be compatible with practically everything it's a bit tough to insist that people convert their scripts to languages that may not be present on all their machines.
The one I learned (moons ago) was to execute each of the elements in the background, and then wait on the exit status of each of the processes one by one.
mkfifo /tmp/fifo1
tar cf /tmp/fifo1 /blob&
pid1=$!
gzip -c /tmp/fifo1 >compressed.tar.gz&
pid2=$!
wait $pid1
tarstatus=$?
if (( tarstatus != 0 )); then
    echo "tar command exited with: $tarstatus"
fi
wait $pid2
gzipstatus=$?
if (( gzipstatus != 0 )); then
    echo "gzip command exited with: $gzipstatus"
fi
I'm never using this ever again!

Wednesday Jul 21, 2004

Korn shell arrays

Korn shell supports arrays. You can have about 4000 elements in a korn shell array, and the performance is not great, but they're quite handy for walking through sets of variables (like paths).
The syntax is:
set -A <array> [values...]
This will set the array named to the values passed, clearing the variable before setting. This is fine, but what if we're simply appending to an array? Lots of folks want to do that. In this case what you use is:
set +A <array> [values...]
All very fine and well, but how do we reference the values in the array? You use the variable[<index>] syntax.
array[<index>]=<value>
What about getting all the values from the array? simply use either @ or \* as the index.
Then there's dereferencing it. Dereferencing the entry involves using ${array[index]}, otherwise the $ would evaluate up to the left bracket, and not evaluate the content of the array, such is the grammar of ksh.
Putting this all together into something useful (like a gcc wrapper for gcc specific options)
set -A arguments -- "$@"
typeset -i index=0
while [[ -n ${arguments[$index]} ]]; do
	case ${arguments[$index]} in
		-W|-Wall)
			arguments[$index]=-v;;
		-Werror)
			arguments[$index]=-errwarn=%all;;
		-O?)
			arguments[$index]=-xO${arguments[$index]##-O};;
		
	esac
	index=$((index + 1))
done

cc -xCC "${arguments[@]}"
About

petesh

Search

Categories
Archives
« July 2014
MonTueWedThuFriSatSun
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
   
       
Today