I’ve been doing some work on a scripting test environment which would automatically run a test suite while I’m editing, triggered when I save.
I’ve already completed a rather complex version of it.. well I’ve completed three versions of it.
I want to merge all this effort into one mega-script which can be easily adapted to any scripting/programming language. Therein lies the problem. There are some tricks which I want to invoke in a bash and zsh environment in order to do some extra debugging.
I’ll explain that another time, let me give some examples.
example one ∞
vanilla shell:
time=`ls -gG --full-time "$1" 2> /dev/null`
zsh:
echo "${time[12,30]}"
bash:
echo "${time:15:29}"
Perhaps there is some other tool I could use which will do this string manipulation, like cut. But I strongly prefer to use builtins rather than rely on external programs which may or may not be present/functional in every environment.
Mind you, a complex scripting language like bash may also not be present/functional in every environment. This is a good argument, and programming in pure POSIX sh is something which I will have to look into later.
example two ∞
This is the debugging stuff I was talking about. Some other time I’ll describe why I want to do this stuff.
zsh:
setopt xtrace
bash:
set -x
This one is definitely not doable with an external program. So this problem of mine is still salient.
I spent some time thinking, and I couldn’t come up with a solution to my problem with my limited experience in this field. I googled for answers for quite some time, and then I checked the zsh manual for an environment variable or builtin or something which might give a hint, but no luck.
This is the kind of thing which would have been handy to have in a simple environment variable, but this being Linux.. well maybe there’s some of that incomprehensible GNU influence mucking the usability up.
I finally broke down and asked for help on the LinuxQuestions.org forum. I searched before posting, of course, but this is the kind of problem that requires too much English to ask/explain, and so it’s very difficult to search for it.
Well, I got a number of excellent suggestions to work with.
checking the process directly from Linux ∞
This is my solution of choice. It reaches right into the guts of Linux to get the info I want.
"basename" `"readlink" -f /proc/$$/exe`
I do this:
MYSHELL="$("basename" $("readlink" -f /proc/$$/exe))"
checking the process using ps ∞
This is another excellent solution.
echo `"ps" -o cmd --no-heading $$|"cut" -d' ' -f1`
checking for builtins ∞
The problem with the above solutions is that they require external programs. This solution is really cool and hackish. I’d use it if it were for my own internal scripting, but because I’m going to be giving away my final script, I do want something which is going to be happy on other peoples computers.
But thinking about that for a moment, this may be a really good way for this scripting to work on some funky BSD or other environment that has bash/zsh but does not have any of those other external programs – or maybe those external programs are simply named differently or they take different parameters to work the way I want.
help >/dev/null 2>&1 || echo "not bash?"
Sneaky. I like it.
