Archive
Posts in Shell
Indenting bourne shell here documents
The Bourne shell provides here documents to allow block of data to be passed to a process through STDIN. The typical format for a here document is something similar to this: This will send the data between the ARBITRARY_TAG statements to the standard input of the process. In order for this to work, you need to make sure that the data is not indented. If you indent it for readability, you will get a syntax error similar to the following: To allow your here documents to be indented, you can append a "-" to the end of the redirection strings like so: You will need to use tabs to indent the data, but that is a small price to pay for added readability…
$ read more →Exiting from a shell script when a failure occurs
While debugging an issue with one of my scripts, I wanted to abort execution and exit when a non-zero return code occurred. I recalled reading about a bash flag that provided this behaviour, and after a few minutes of reading through bash(1) I came across the following set option: "-e Exit immediately if a simple command (see SHELL GRAMMAR above) exits with a non-zero status. The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test in an if statement, part of a && or|| list, or if the command's return value is being inverted via !. A trap on ERR, if set, is executed before the shell exits." So given the following test script that calls a non-existent command /bin/bork: We can see that a normal run completes to the end: But an invocation with the "-e" option exits when a non-zero return value occurs: The bash manual page is full of super interesting stuff, and I just printed it out to learn more…
$ read more →Getting sed to substitute a newline on Linux and Solaris hosts
While crafting an install script a week or two ago, I came across an annoying issue with the Solaris sed utility. When I tried to substitute the string ', ' with a newline, I got this: But when I ran the same sustitution with GNU sed, everything worked as expected: It turns out that the Solaris sed is extremely dated, and requires an explicit newline instead of a ' ': 2.4 2.3 2.2 2.1 In the end I found it easier to write my installer in Python, which made life much much easier.
$ read more →Bash tips
I read through the bash tips on the hacktux website, which brought to light the fact that you can do basic integer math in your bash scripts. This is easily accomplished by using dual parenthesis similar to this: This is good stuff, and I need to replace some old and statements with this.
$ read more →Helpful shell shortcuts
So this may be a little basic, but I find myself using these two shortcuts quite a bit while at the shell. If you ever find yourself wanting to "reuse" the last argument in a command -- for example, here I move a file from one location into /var/tmp and I want to "cd" into /var/tmp without having to type it, use the shell variable !$... locutus:~ (svoboda)> dd if=/dev/zero of=/tmp/blah bs=1024000 count=1 1+0 records in 1+0 records out 1024000 bytes (1.0 MB) copied, 0.0109023 s, 93.9 MB/s locutus:~ (svoboda)> mv /tmp/blah /var/tmp locutus:~ (svoboda)> cd !$ cd /var/tmp locutus:/var/tmp (svoboda)> pwd /var/tmp If you wanted to "preface" your last command, you can throw anything you want into the shell followed by the !! shell shortcut…
$ read more →