HAL: Part 4 Scripts

by Henry Grebler

This is the last part of a series of articles about how I operate.

Here is a list of the full set of articles:

In HAL: Part 1 Preliminaries, there was a link which would allow the reader to download my HAL. There you will find about 123 scripts.

Here are the most interesting ones.

Aliases Which Invoke Scripts

I’m going to start with scripts that I mentioned in ‘HAL – My Favourite Aliases’ in the section ‘Aliases Which Invoke Scripts’.

alias cleanup $HOME/scripts/cleanup.sh

This is only relevant to emacs users. By default, when you edit a file, emacs keeps a copy of the original file in a file whose name has a trailing tilde (~). They’re worth hanging on to while you are developing your program, script or article. But once you have finished, they become just clutter, especially if you maintain some sort of revision control (which I do).

This script finds all such emacs backup files in the tree rooted at the current working directory, lists them and offers to delete them.

alias diff $HOME/scripts/diff.sh

This script could be seen as related to the previous script. With more than one arg, it behaves like a regular diff. If given a single arg, it compares that file against its emacs backup. So, for example, ‘diff HAL.Scripts’ (the file I’m currently creating) would become ‘diff HAL.Scripts~ HAL.Scripts’.

Looking at the script now, I wince: it seems so badly written. And it seems it would be better cast as a function. But it has evolved over time, so perhaps I should be more forgiving.

alias lns '$HOME/scripts/lns.sh'

Like “ln -s”, but, if the target is a symbolic link, delete it first. If it’s a file, offer to delete it.

Ideal for replacing a symlink.

alias tcg='/home/henryg/scripts/tarcompressuuencode.sh +u -g'
alias tcu='/home/henryg/scripts/tarcompressuuencode.sh'
alias uut='/home/henryg/scripts/uudecodeuncompresstar.sh'

The name of the script provides clues to its origins. I’ve used email since at least 1993. Back in the early days, the typical way to send directory trees was tar-compress-uuencode. That’s what this script does. Over the years, gzip replaced compress and often I don’t uuencode (except when sending to myself).

‘tcu’ was what I used at first; ‘tcg’ is what I typically use now.

‘uut’ is for use in the other direction. I guess, these days, I would only use it on mail from myself. Other related capabilities are provided by functions ‘gttv’ and ‘gtx’ (see ‘HAL: Part 3 Functions’).

alias x='/home/henryg/scripts/expr.sh'

The ‘x’ alias (nice and short) invokes expr.sh, my Mickey Mouse calculator. For most of my calculations, this suffices. I can enter asterisk (‘*’) or lower-case ‘x’ for multiply. I can use parentheses for grouping as long as I quote the expression.

For example:

x '(2 + 3)/ 7'
0.714286

Under the covers, it invokes awk.

alias xr='/home/henryg/scripts/xrenamewindow.sh'
alias xt='/home/henryg/scripts/xterm.sh'

I’m currently running 34 xterms spread across 6 virtual desktops. By default, they’d all have the text “xterm” in the title bar. When iconised they’d all be identical and all have the text “xterm” near the bottom of the icon. How would I find the one I want?

When I start an xterm, I use the ‘xt’ alias followed by a string specifying the text I want to see in the title bar and on the icon. (I can choose to have different text for the icon, but usually I don’t.)

This text is meaningful to me and helps me to keep track of all my xterms. The one I’m working in at the moment has “WRITING” in the title bar. There’s another right near it with “WRITING2″. This is my main window for creating what you are now reading. The other one is a “helper” window; for instance, I use it to look up what I wrote in a previous article.

These two windows are here for the long haul. I opened “WRITING” last Christmas Day, soon after I rebooted 23 December 2010 (as I write, today is 28 February 2011). I’m not sure why it should be this way, but I only opened “WRITING2″ 3 Jan 2011.

Often I only work on something for a short time. When I’ve finished, if I want to recycle the xterm, I might decide to give it a different text string in the title bar. For that I use ‘xr’.

Scripts that stood the test of time

Some of these display my ignorance or naivete at the time that I wrote them – proving, yet again, that a script does not have to be brilliant or perfect to be effective and useful.

If someone provides me with a better way of achieving the same result, I’ll consider replacing the corresponding script.

The dates below refer to the first time I wrote the script. Many have been modified over time.

1991	expire.sh - move a file to the expire directory

Nowadays it would just be a function.

The basic idea is that some things (files, directories) look like you’ve finished with them, but you’re not quite sure. Move them to the expire directory. From time to time, when you visit the expire directory, if they are still there, delete them. I guess it’s a little like Trash on a Microsoft Windows system.

I use this infrequently.

1992	howWide - displays the number of chars in the widest line in a file

I cannot tell you how many times I’ve used this script. It often dictates whether I choose to print a file in landscape as opposed to portrait.

1993	rcsdiffp - do an rcsdiff on this file and the previous
                   revision in RCS

rcsdiff is usually used to compare the working copy with the last checked-in version. There are many options to perform other sorts of comparisons. Over the years, the second most common comparison for me has been with the penultimate revision because, after I have checked a file in, I may want to check again what I changed.

1995	add.sh

Adds numbers. Example usage:

ls -la | cutf 5 | add.sh

cutf, a function, is my response to some non-uniformity between awk and cut. cut only knows about single-char field separators; by default, awk treats white space as the field separator. cutf behaves more “intuitively” than cut on many sorts of (unstructured output) data.

The above pipeline lists the files, selects the 5th field (file size) and adds the sizes.

In many cases, even if there is text intermingled with numbers, add.sh produces the sum.

1995	ps_who_owns_it*.sh - provide a list of parent process ids for a pid

Solaris has ptree which does a better job than this script. Linux has pstree, but I’ve never found it satisfactory.

Give it a pid and this script will provide a full PS list entry for the given pid and all of its parents up to 1. I’ve never managed to get this script to work upwards and downwards. Each process has a single parent, but can have more than one child, so the full script would be more difficult.

I use this often.

1996	emacs-nw - interlude for commands in .mailrc

This is an interesting script. It has about 12 functional characters in the entire script. Any explanation will be bigger than the script itself. If you don’t run emacs, you won’t care.

emacs comes in 2 flavours. If you run emacs from the command line, it will try to run using its special interface to X in a separate emacs window. If you want it to run in an xterm you have to specify something special. I never want to run the GUI version.

There are times when applications offer to run your favourite editor. Sometimes you can add arguments, sometimes you can’t. This script is for those times when you can’t.

Note that, although it could be cast as a function for interactive use, a function may not be visible to the application.

This is one of the few scripts I have written that does not have .sh at the end of the name. I cannot recall what my thinking was at the time.

1996	chmod_g-o.sh - chmod copying rx bits from group to other

This is a script that I don’t use often, but every time I do I’m glad I wrote it. It’s such a simple function to state, and not hard to write, but not the sort of thing one can do easily on the command line. This script does not treewalk. Use a construct like chmod_g-o.sh `find …`

It’s particularly nice because, quite often, when you are working on a tree, you want to set read permission on files and rx on directories. This saves you dealing with that sort of detail. If the group permissions are right, then the other permissions will be right after the script is run.

1997	rename.sh - rename files

Ostensibly written in 1997, a comment near the top suggests that I had already written something like this previously. I thought I based it on a similar command from VMS, but I can find no confirmation for that proposition.

When I wrote it, I was concerned that I might get it wrong, so rather than having it do a rename, it simply displays the commands which it would have executed. I check that the commands look right and then copy and paste them to achieve the rename.

I guess because I use it infrequently, I left in the usage so that it is always displayed. Here’s an example:

rename.sh 'part*' part=.doc
Usage:  /home/henryg/scripts/rename.sh  <from> <to>

        where <from> is filename pattern to match

        <from> is of the form <string1>*<string2>
        <to>   is of the form <string3>=<string4>

either of <string1> or <string2> may be null
either or both of <string3> or <string4> may be null

 mv part1 part1.doc
 mv part2 part2.doc
 mv part3 part3.doc

Best of the Rest

dircmp - compare 2 directories

There may be other ways to achieve the same ends, but I’ve used dircmp since 1993 and still find it useful. It reports which files are different and which files in the first directory are absent from the second directory (it’s not symmetrical).

disp_args.sh - display args

I use this to determine what a shell script sees as its arguments. This can be helpful if there is a lot of quoting of different sorts going on.

du-forte.sh - industrial-strength du
du-root.sh - industrial-strength du for the root partition

The main purpose of these script is to avoid NFS-mounted filesystems. If I want to know where the space is being used on a disk, especially if it’s the root filesystem, I don’t want to wait while large remote disks are searched inefficiently. If the space is remote, I’ll go to the remote machine to do the analysis.

Lately, I’ve found that NFS-mounted filesystems are not the only pain. I also have a 1TB USB disk attached to my machine. It’s pretty slow. Usually I don’t want to search that. I’m not sure there’s an easy way to achieve this objective.

Until I started writing this, I’d forgotten that I had du-root.sh. Damn, it would have been very useful the other day!

I tried it just now:

du-root.sh
This script performs
        du -csk /*
for directories (not files) on the root partition only.
2       /.snap
Skipping non-root /dev
Skipping non-root /tmp
Skipping non-root /usr
Skipping non-root /var
1902    /etc
Skipping non-root /cdrom
Skipping non-root /cdrom1
2       /dist
1026    /bin
160510  /boot
6842    /lib
242     /libexec
2       /media
14      /mnt
2       /proc
4128    /rescue
66      /root
5398    /sbin
Skipping non-root /p6
1320    /History_dir
75398   /sw
2       /Big
Skipping non-root /WD5
Skipping non-root /DISKA
Skipping non-root /p7
Skipping non-root /p8
2       /p5
2       /p1
Total (excludes /proc) 256858

That was quick. That’s so clever.

openwin*

There’s a whole family of scripts that I use to fire up the same window environment on Solaris, Linux and FreeBSD. I use this family constantly.

treecopy.sh - like Prime's tree copy (trecpy)

This script uses back-to-back tar to copy a directory tree. I used this extensively until I discovered rsync.

That’s It

That’s all the scripts that I’m going to discuss in any detail. There are many more, but I don’t think they are of interest to a wider audience. If you’ve downloaded them, feel free to explore, ignore, adapt, reuse and recycle.

As I said at the start, these entities (aliases, functions, scripts) were intended to make life more comfortable for me. They suit the way I want to operate. I do not expect I have clones. Every reader will be different and will have different notions of comfort.

I anticipate that an individual reader will completely ignore perhaps 90% of these entities. Of the remaining 10%, perhaps 2 or 3 will be deemed worthy of further consideration.

On the other hand, I hope there are enough readers that at least one person finds some of these entities worthwhile, even if it’s only to act as a launching pad for an entirely different entity.

Enjoy.

About Henry Grebler

Henry has spent his days working with computers, mostly for computer manufacturers or software developers. His early computer experience includes relics such as punch cards, paper tape and mag tape. It is his darkest secret that he has been paid to do the sorts of things he would have paid money to be allowed to do. Just don't tell any of his employers. He has used Linux as his personal home desktop since the family got its first PC in 1996. Back then, when the family shared the one PC, it was a dual-boot Windows/Slackware setup. Now that each member has his/her own computer, Henry somehow survives in a purely Linux world. He lives in a suburb of Melbourne, Australia.
This entry was posted in July 2011. Bookmark the permalink.

Comments are closed.