Visit Jeremy's Blog.
Go Back > Blogs
User Name



Pure Bash option handling

Posted 04-16-2012 at 02:44 AM by Nominal Animal

I like the standard getopt way of specifying script and/or program parameters, but I don't like its quirks, and it is pretty cryptic if you don't use it often enough.

Here is pure Bash skeleton to implement script option parsing. It is long, but it should be easier to read and understand than one implemented using getopt.
set +f                  # Enable filename globbing
set -B                  # Enable brace expansion
shopt -s dotglob
Senior Member
Posted in Uncategorized
Views 999 Comments 0 Nominal Animal is offline

Handling all file names safely in Bash

Posted 02-11-2012 at 01:09 PM by Nominal Animal
Updated 04-16-2012 at 03:41 AM by Nominal Animal (Fuller fixes.)
Tags bash, filename, nul

In Linux, each file or directory name (or more generally, pathname component) is just a string of bytes. It always ends with the C end-of-string mark, ASCII NUL: a zero. Value 47, ASCII /, is also reserved for use as a separator between pathnames.

Bash can read ASCII NUL separated data using read -d "" variable. It will, however, remove leading and trailing characters that match IFS, and return false (nonzero status) if the input does not have a final NUL. This applies to...
Senior Member
Posted in Uncategorized
Views 1377 Comments 0 Nominal Animal is offline

Bash scripts and keyboard input

Posted 01-29-2012 at 03:43 PM by Nominal Animal

You can use Bash to read individual keypresses. Here is an example:

# Reset terminal to current state when we exit.
trap "stty $(stty -g)" EXIT

# Disable echo and special characters, set input timeout to 0.2 seconds.
stty -echo -icanon time 2 || exit $?

# String containing all keypresses.

# Set field separator to BEL (should not occur in keypresses)
Senior Member
Posted in Uncategorized
Views 6105 Comments 0 Nominal Animal is offline


All times are GMT -5. The time now is 02:34 AM.

Main Menu

Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration