Showing posts with label bash. Show all posts
Showing posts with label bash. 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.

Wednesday, November 13, 2013

migrated a Korn shell script using "Extended Pattern Matching" to bash

https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html

I had to "shopt -s extglob" for that.

I inserted the shopt line rather close to the case statement, where the Extended Pattern Matching gets employed. That was inside a loop. That had no effect, but it didn't say so. I rather threw some weird error messages. I reduced the code around the causing line to its minimum, occasionally moved the shopt to outside the loop, and it worked. If it had only been that straigt …

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.

Wednesday, February 20, 2013

O'Reilly Media book: bash Pocket Reference

bash Pocket Reference:
You need to know how to work with the bash shell if you want to get to the heart of Unix systems, including Linux and Mac OS X. Now covering the most recent version of bash, this concise little book puts all of the essential information about bash at your fingertips. You'll quickly find answers to annoying questions that always come up when you're writing shell scripts -- What characters do you need to quote? How do you get variable substitution to do exactly what you want? How do you use arrays? -- and much more.

Thursday, March 10, 2011

today I needed a stack class in bash

It's certainly not hard to do, but then it takes a while, until it's done properly.

Tuesday, December 15, 2009

bash version 4 and associative arrays

The new builtin array variable BASH_ALIASES is a nice example for an associative array.
If you do an ''echo ${BASH_ALIASES[@]}" you see all the values, if you do a "set | fgrep BASH_ALIASES" you see, to what the indexes map.

Update:

In December, when I got aware of the new feature, I thought, there is no way to get the list of indexes.
But there is:  ${!name[@]} and ${!name[*]} return the list of array keys. This gets explained in the section Parameter Expansion. I am sorry, if my former statements caused confusion.


Tuesday, February 24, 2009

bash version 4.0 released

After all these years bash finally got associative arrays implemented: bash version 4.0 got announced.

Needless to mention, that bash is my favourite shell, has been for almost 20 years now. I once had a period, when I used buggy zsh for scripts, where I was tempted to use associative arrays, because zsh introduced them long before I came across zsh.