Nixers Book Club - Book #1: The UNIX Programming Environment - Community & Forums Related Discussions
Users browsing this thread: 5 Guest(s)
|
|||
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 ;; 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) 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 A couple of drawbacks:
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. :) |
|||