Nixers Book Club - Book #1: The UNIX Programming Environment - Community & Forums Related Discussions

Users browsing this thread: 1 Guest(s)
movq
Long time nixers
Rest of Chapter 4
=================

Alright, a basic introduction to awk. Probably not a lot of new stuff to learn for must people around here.

A powerful tool. My usage has declined over time, interestingly. I often need more than just “process text”.


Chapter 5
=========

cal
---

The script uses “set `date`” on two occasions. IMHO, this is a very different style of programming than what we (I?) have today.

Code:
0) set `date`; m=$2; y=$6 ;;
1) m=$1; set `date`; y=$6 ;;

It makes for really short and effective code. If you know what the magical “set” does, it’s pretty good. Most modern scripts would probably use “sed” or parameter expansion to extract the current month and year:

Code:
now=$(date)
m=${now#* }
m=${m%% *}
y=${now##* }

Much more clunky. But then again, it’s more explicit (explicit is good, right, because it makes it easier for your successors to read your code) and it doesn’t clobber the original argument vector.

It’s probably hard to get this point across: To me, this is “typical” programming style of “those UNIX people”. Concise, “clever”. Maybe this will become more clear when we get to the C examples later on. (I also noticed it a lot when reading “The C Programming Language”.)

5.2: if
-------

“if” runs a command and checks its exit status. Such an important point to learn when writing shell scripts. Also highlights how shell scripts really only are the glue between other programs.

5.3: ls in a script
-------------------

Argh, they did it. It works in this case, but there are pitfalls.

5.5: overwrite
--------------

An academic example to teach more about the shell, isn’t it? Is it really worth it to have this as a command? Just redirect to a temporary file and do the “mv” manually, I’d say.

5.7: pick
---------

What always bothered me about pick is that there’s no undo. No way to go back. In 2011, I made a pick on top of a “vipe”:

https://www.uninformativ.de/blog/posting...NG-de.html

(German article, but you’ll be able to follow the code regardless. Be sure to open the screenshot.)

I never really used it, though. Doesn’t fit my workflow …

5.8: news
---------

I don’t know. This … this is just horrible. Using ~/.news_time as a terminator in a list? And then even ' not found'? Using “set” to process the output of “ls” …

Code:
#!/bin/sh

cd .  # /usr/news

for i in *
do
    if test "$i" -nt ~/.news_time
    then
        when=$(stat --format=%y "$i")
        who=$(stat --format=%U "$i")
        printf '%s: %s (%s)\n' "$when" "$i" "$who"
    fi
done | sort

touch ~/.news_time

A couple of drawbacks:
  • Uses GNU features of stat.
  • Way more forks. This could have been a problem back in the 1980ies.
  • Different output format, especially no cat. Was this really practical to print all the files? I’d rather expect a “news $filename” to show one posting.

But yeah, if you insist on including the cat, suddenly the original is clever again, because “ls -t” neatly sorts the files by time.

Still, I think it’s an example of a terrible shell script. I’d rather make the script longer instead of relying on hacks like “set X`foo`”.

5.9: get and put
----------------

Did they just implement a basic version of RCS? At least they mentioned the existence of SCCS at the very end of the chapter. :)


Messages In This Thread
RE: Nixers Book Club - Book #1: The UNIX Programming Environment - by movq - 05-12-2020, 11:19 AM