Continuing the drama of little snippets of shell

This little piece of shell script attempts to find the ‘lowest subprocess’ of a passed in process. It works well with a straight tree of processes, and if there are the occasional pipes in the command tree then it will miss them most of the time.
Maybe tomorrow I’ll talk about how to fix it.

function find_lowest_subprocess() {
local -i parent=$1
local pids
typeset -a pids
pids=$(pgrep -P $parent)
while [[ -n "$pids" ]]; do
if (( ${#pids[@]} > 1 )); then
local i=0
while (( i < ${#pids[@]} )); do
local sub=$(pgrep -P ${pids[$i]})
[[ -n $sub ]] && parent=$sub
((i=i + 1))
done
else
parent=$pids
fi
pids=$(pgrep -P $parent)
done
echo $parent
}

Don’t just install everything into /usr/local

If you use windows then this is probably going to be very, very boring.
Every now and again you find yourself needing to install some piece of software on your computer from a source package. You tar xjf the package and descend into the subdirectory and type ./configure.
At this point I would yell stop! Rather than putting it into the default location of /usr/local, consider putting it in /usr/local/<package-version>.
How does this help I hear you ask. Well, using a simple script (in the extended entry), you create a set of symbolic links in the /usr/local directories which reference the files in the /usr/local/<package-version> directories.
If you decide to remove the package then simply remove the /usr/local/<package-version> directory and all the symlinks become broken. By using symlinks -rd /usr/local you clean the file system up and everything is peachy. If you don’t have a copy of symlinks, it is available from the debian repository, where you should find the source package somewhere near the bottom.

#!/bin/bash -p
package=$1
destdir=${2:-/usr/local}
me=${0##*/}
[[ -z $package ]] && {
echo "Usage: $me <package> [destination = /usr/local]"
exit 2
}
cd $destdir/$package || {
echo "$me: package $package does not seem to be installed"
exit 1
}
# build the directory structure - this is a weakness
find . -type d | cpio -o | (cd $destdir; cpio -id)
find . -type f -exec $echo ln -s $destdir/$package/{} ../{} \;

Oh, and for solaris, as I’m using the file in various locations surrounded by symbols you will have to just pass it into a sub-program to execute the link command. Apparently solaris doesn’t just substitute the name of the target for the link; instead it will only substitute the name of the target when it is isolated (i.e. you would need to use the {} on their own without anything surrounding them – which explains the space between the closing brace and the backslashed semicolon – old habits). I supposed I could throw a bit of perl at this problem but… it works on my box so frell the rest of you :).
Meh, the entire problem is annoying; generally I would always have to create a program to process the {} operation anyway to prevent space characters from getting in the way but as we say in the trade 99% is better than 0%. If you want a 100% solution you need to add a script that performs the link – one per line produced from the find.

Differences between the busybox shell and the real bourne shell

Feature Bourne Shell Busybox Shell Bash
Subprocess Execution “ (the backtick) “ or $() As Busybox
Math Evaluation use expr (not builtin) $(( )) As Busybox; adds ((var=math))
Constants None None typeset -r
Integers None None typeset -i
Evaluation [ [ [
Extended Evaluation /bin/[[ (1) /bin/[[ (1) [[


(1)
The [[ operator is not the same as the program /bin/[[ as a program you need to still use double quotes around the variables to be expanded; thus defeating the reason for having them in the first place.

For Example:

# file="/tmp/let me go"
# touch "$file"
# [[ -e $file ]] && echo "There"

Yields [Busybox]:

[[: me: unknown operand

– as $file is word split before handing it to the command [[

Yields :

There

– due to $file not being split when passed to the test -e.

And another thing…

It’s the world’s most advanced operating system? I mean really, that’s an overstatement. Hello! that’s nothing more than a brag as there’s nothing to back up the statement. Advanced for what? File systems? zfs! observability? dtrace! pretties on the screen? XGL! The ability to perform more than one name service lookup at a time? [ok, that’s a cheap shot, I’m sure they fixed this]
Pants, complete pants I tell you!

Finally got XGL working properly

It started off not displaying on the screen correctly – it turns out that I had 16 bit visuals enabled. Changing to a default depth of 24 made that go away. It is really pretty. Prettier than vista. All I need to do is not waste time on it.

gedit character set interpretation

gedit being helpful gedit has this nice feature where it asks you what character set encoding a document is in if it can’t decide this for itself. I’m not familiar with the mechanism that is being used for this, but it probably has something to do with ninja badgers, character counting and a telepathic link to the borg collective. The problem is that if it has to give up on guessing what the file is, there’s no way to force the file to be opened as any file type at all. I encountered this when trying to access some old data that had trailing NULL characters at the end of the file. The problem is that there’s no ‘mangle it to this file type and show it to me‘ option.

Misleading Document titles

This one: Secure Java apps on Linux using MD5 crypt
Firstly, the encrypted string is: $<mechanism>$…, where a mechanism of 1 is MD5. My desktop has 2a, which indicates I’m using the Blowfish algorithm – I see no reason to compromise.
Well, guess what – this document won’t work for me because I use blowfish locally, and secondly, the only accounts in /etc/shadow (and passwd) are local accounts – if you’re using nis, nisplus, or ldap (solaris more so) for your name services, then you’re SOL with this mechanism.
This is another reason for not using the crypt mechanism is that this is trying to solve the problem from the wrong level. You should not be trying to compare the encryption strings, you should be using an alternative to the OS provided security mechanism. So in this case you should be using jaas, and a PAM plugin. The use of yet another ‘well it works on my box‘ mechanism is so cripplingly annoying that it angries up my blood.