Showing posts with label shell scripts. Show all posts
Showing posts with label shell scripts. Show all posts

Wednesday, December 18, 2013

shell code and useless use of "ls *.tar"

Consider this code (I came across such Korn shell script code recently):
TARFILE=$(ls -1 /tmp/*.tar)
According to http://partmaps.org/era/unix/award.html#ls you ought to replace the ls with a glob, e.g. like this:
[[ -s /tmp/*.tar ]] && set -A tarfiles /tmp/*.tar
for TARFILE in "${tarfiles[@]}"
do
  : # ...
done
Instead you may want to enquire ${#tarfiles[@]}, i.e. the number of files globbed.

Please forgive me the naming here! I was happy to take this note anyway.

Tuesday, November 5, 2013

the bash's "nullglob" is rather nice to use

http://www.gnu.org/software/bash/manual/html_node/Filename-Expansion.html

w/o this command line:
$ shopt -s nullglob
a glob pattern, that does not successfully evaluate to existing file names, gets literally resolved to a file name identical to the glob pattern.
With the above command line executed already, this command line will print file infos of /etc/passwd and ignore the unsuccessful other two glob patterns:
$ ll /etc/passwd /etc/passwd? /etc/passwd??
In interactive mode you may want to see the error messages, within a shell script supplying all three parameters to a "for f in …" loop, you will want the unresolving ones to get ignored.
$ for f in /etc/passwd /etc/passwd? /etc/passwd??; do echo $f; done
Well, at least in my script today I found this option rather useful.

I tried to find something comparable in the Korn Shell, but I wasn't successful.

Sunday, August 12, 2012

O'Reilly Media book: Classic Shell Scripting




nice scripts shown there and available in the sample code:
  • pathfind.sh – rather similar to my own find_file_on_PATH.sh
  • show-identical-files.sh – rather similar to my own group_by_content.sh – but has a problem with "md5sum"
  • puser.sh



Wednesday, July 28, 2010

updating my xmlresume CV to mention Shell more often

I applied for a contracting position, the recruiter likes my CV, but he wants to see the term Shell mentioned far more often. Alright then…

Actually it's always quite funny as well (updating the CV), as it's yet another nice XML document to edit in emacs with nxml-mode: it's in xmlresume.
A little typing, a lot of copy+paste until back to 1980, a few ruby rake tasks executed, exported the RTF from Open Office as Word file, uploaded, messaged the recruiter – done.

Thursday, October 29, 2009

creating phone book and diary entries from incoming calls on my router

If you read this title, how mad do you think I am? Honest!

Alright, "as you know" (so of course this article only applies to some sort of computer nerds), my router is a FRITZ!Box 7270, it's also the base station for my (wireless) phones, and a lot more ... -- certainly one of my most important toys or gadgets. And I have 2 of them, one for at home, one for anywhere outside, where I can only connect to the Internet through UMTS.

So alright, back to the title of this article!

  • It's nice to record your incoming and outgoing calls in your diary, right? (my diary is emacs style)
  • It's nice to get something descriptive displayed on your phone, when somebody calls, right?
  • It's nice to extend your phone book through time as easily as possible, right?

Well, my router is running Linux as its operating system kernel, and BusyBox on top of it. On each incoming phone call it runs a shell script with a few parameters, of course the caller's phone# (if available) and also the callee's phone#. (I do have more than one phone#, and why not also record the called phone#, just for the record?) It tries to associate a name with the caller's phone#, if there is a matching entry in the phone book.

Quite a while ago I started implementing such a shell script, and obviously (as it is almost an ordinary (bash) shell script), I can develop and test it on any of my computers, that can run shell scripts, like any of my openSuSE computers, any WinXP computer running cygwin, a Mac running OS X, ... . Yes, I cannot create a full blown shell script, I will not pipe a lot through one-liner perl or ruby scripts on my router, but still: it's a handy and useful, not so tiny shell script.

So far it appears to me, as if no such executable gets called on the router for outgoing calls, but I can still mimic this behaviour by calling that script on my main development box.

Having said this, my script (fritz_box_calllog.sh) fulfills all the requirements, that I listed above in that bullet list.
For incoming calls without caller id and/or without a matching entry in the phone book it creates a piece of XML text, that I can paste into my FRITZ!Box XML phone book, and where I can fill in a phone# and a name or any descriptive text, so next time, I want to dial that phone# myself, I can select it from the phone book. And maybe another time that person will call you with caller id, and then you will be able to greet that person with her/his name. Wouldn't that be nice?

The implementation of this software also got inspired by Matthias Hühne's "Dial!Fritz", that I run on my iPhone. But of course his software is far completer and nicer, and it's well integrated.