Bash Best(ish) practices part 2

Part 2 of our series has us beginning to use functions as a part of script development. Bash functions are pretty simple, and much has already been written about them. Today we will primarly be concerned with how they can help your script flow better and potentially speed up development and troubleshooting. Lets start with some code… <code>#!/bin/bash # Copy some files # Print usage information and exit print_usage(){ echo -e "\n" \ "usage: ./copy_stuff -s sourcedir -d destdir \n" \ "\n" \ "-s <sourcedir> source directory\n" \ "-d <destdir> destination directory\n" \ "-h this help\n" \ "\n" && exit 1 } # Loop through $@ to find flags while getopts ":hs:d:" FLAG; do case "${FLAG}" in s) # Our source SOURCE="${OPTARG}" ;; d) # Our destination DEST="${OPTARG}" ;; h) # Print usage information HELP=1;; [:?]) # Print usage information print_usage;; esac done # Copy some files from one place to another copy_files(){ echo "Copying ${SOURCE}/foo to ${DEST}/foo" echo "Copying ${SOURCE}/bar to ${DEST}/bar" echo "Copying ${SOURCE}/baz/foo to ${DEST}/baz/foo" echo "Copying ${SOURCE}/baz/bar/foo to ${DEST}/baz/bar/foo" echo "Copying ${SOURCE}/dir/somefile to ${DEST}/dir/someotherfile" } # Restart some service restart_service(){ echo "Stopping the service" echo "Making sure the service is stopped" echo "Reticulating splines..." echo "Starting the service" } # Test to see if the service is running test_service(){ echo "service is tested" } [ "${HELP}" ] && print_usage if [ ${SOURCE} ] && [ ${DEST} ] then echo "Copying files from ${SOURCE} to ${DEST}" copy_files echo "Restarting the service" restart_service echo "Testing the service" test_service fi </code> There is a lot happening in this script, but you can see that we are expanding upon ideas from last week. ...

March 17, 2013 · 4 min · jsuttonlogin

Bash Best(ish) practices part 1

Hello again. It has been some time since I have posted here. Bash is the focus of todays post. Well… Really it is the focus of several posts over the next few weeks as we delve into some best(ish) practices when writing bash scripts. The culmnination of these articles will be the creation of a bash script template file that can be used to make writing scripts a little faster/better. ...

March 9, 2013 · 3 min · jsuttonlogin

Perl invocation options

I have known about perl invocation options for a while, and I have used them on several occasions. However, I’m not sure I understood just how far the rabbit hole goes. Try the following: [sourcecode language=“perl”] perl -wnle ‘print “Each line of the file is printed here $_”;’ foo #Process through each line. perl -wpie ’s/replace/with/g;’ foo # sed -i replacement perl -wnlae ‘print $F[0]’ foo # awk ‘{print $1}’ replacement [/sourcecode] ...

February 23, 2012 · 1 min · jsuttonlogin

Processing multiple queries with Perl DBI and mysql

Introduction Since I am currently exploring the intricacies of developing an asp.net web app and have nothing terribly interesting to show for it today I will be delivering some information that I found useful on a past project. ...

July 6, 2010 · 4 min · jsuttonlogin

Perl SOAP::Lite Example

Recently I was tasked with integrating systemA with systemB. Could I be more vague? Yes, probably so. Anyway, systemB is a vendor’s system which is implemented with both a SOAP and a REST API. REST is inherently easier to implement given its use of URLs. However, I chose to use SOAP, because I figured that in my field I was far more likely to encounter a vendor who only implemented SOAP than I was a vendor who only implemented REST. That decided I set off down a long and SOAPy road… ...

May 28, 2010 · 4 min · jsuttonlogin