Site Map - skip to main content - dyslexic font - mobile - text - print

Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes Monday through Friday.


hpr1757 :: Useful Bash functions

Some Bash functions that may be of use in your scripts

<< First, < Previous, Latest >>

Host Image
Hosted by Dave Morriss on 2015-04-28 is flagged as Explicit and is released under a CC-BY-SA license.
Listen in ogg, spx, or mp3 format. | Comments (4)

Part of the series: Bash Scripting

This is an open series in which Hacker Public Radio Listeners can share their Bash scripting knowledge and experience with the community. General programming topics and Bash commands are explored along with some tutorials for the complete novice.

Overview

I enjoy writing Bash scripts to solve various problems. In particular I have a number of scripts I use to manage the process of preparing a show for HPR, which I am developing at the moment.

My more complex Bash scripts use a lot of functions to perform the various tasks, and, in the nature of things, some of these functions can be of use in other scripts and are shared between them.

I thought I would share some of these functions with HPR listeners in the hopes that they might be useful. It would also be interesting to receive feedback on these functions and would be great if other Bash users contributed ideas of their own.

Full Notes

Since the notes explaining this subject are long, they have been placed here: http://hackerpublicradio.org/eps/hpr1757_full_shownotes.html, and an experimental ePub version is available here: http://hackerpublicradio.org/eps/hpr1757_full_shownotes.epub.

  1. Bash Support Vim plugin: http://www.vim.org/scripts/script.php?script_id=365
  2. HPR episode Bash parameter manipulation: http://hackerpublicradio.org/eps.php?id=1648
  3. How to write functions (from The Linux Documentation Project):
  4. Download the pad and yes_no functions: http://hackerpublicradio.org/eps/hpr1757_functions.sh

Comments

Subscribe to the comments RSS feed.

Comment #1 posted on 2015-04-28T15:02:44Z by Bill Ricker

epub

Nice podcast. The experimental epub long notes is a great idea. Constructive feedback : The code segments render in the popular FBReader as fixed width sans oblique font, and some lines are indented further than intended, including indenting the line number. The code however looks fine in the alternative "E-Book Viewer" app on my Open With menu. (I don't even remember what package that came with.)

Comment #2 posted on 2015-04-28T15:35:53Z by Dave Morriss

Re: epub

Thanks Bill,

The epub notes need work, I know. I have not yet done a comprehensive look at how different readers render them.

As soon as I can I plan to follow Jon Kulp's lead and build them with some of the tools he recommends rather than with pandoc, which I'm using now.

Comment #3 posted on 2015-04-28T17:18:30Z by 0xf10e

exitcodes

Only 0 being true in shell is due to 0 being the "everything is fine" exitcode in UNIX.
Everything else signals some kind of error. Which exitcode correlates to which error depends AFAIK on the command. But you can make your tools scripting friendly by exiting w/ 1 on invalid input, 2 on invalid configuration and so on.
When you stick to values of 2^x you can even AND them and fill up all 8bits I think an exitcode can have! ;)

Comment #4 posted on 2015-04-28T21:53:42Z by Dave Morriss

Re: exitcodes

Thanks 0xf10e,

I guess I'm easily confused :-)

You make some good points. I've worked on (old, obsolete) mainframe operating systems where the exit code was caught and turned into a text message, and it was possible to write and register your own "Message Text Module" for your own application. I thought that was a good design.

Leave Comment

Note to Verbose Commenters
If you can't fit everything you want to say in the comment below then you really should record a response show instead.

Note to Spammers
All comments are moderated. All links are checked by humans. We strip out all html. Feel free to
record a show about yourself, or your industry, or any other topic we may find interesting. We also check shows for spam :).

Provide feedback
Your Name/Handle:
Title:
Comment:
Anti Spam Question: What does the P in HPR stand for ?
Are you a spammer →
Who hosted this show →
What does HPR mean to you ?