|
| 1 | +--- |
| 2 | +title: bash - Script Stub |
| 3 | +author: Steven Spencer |
| 4 | +--- |
| 5 | + |
| 6 | +# Bash - Script Stub |
| 7 | + |
| 8 | +Where I was previously employed, we had an ace programmer who knew a bunch of languages. He was also the go-to guy when you had questions on how to accomplish something with a script. He finally created a little stub which had some examples of how to do things that you could just strip out and edit as needed. Eventually, I got good enough at these routines that I didn't have to look at the stub, but it was a good learning tool, and something that others may find useful. |
| 9 | + |
| 10 | +## The Actual Stub |
| 11 | + |
| 12 | +The stub is well documented. Keep in mind that this is by no means an exhaustive script! There are a lot more routines that could be added. If **you** have examples that would fit nicely into this stub, then please feel free to add some changes: |
| 13 | + |
| 14 | +``` |
| 15 | +#!/bin/sh |
| 16 | +
|
| 17 | +# By exporting the path, this keeps you from having to enter full paths for commands that exist in those paths: |
| 18 | +
|
| 19 | +export PATH="$PATH:/bin:/usr/bin:/usr/local/bin" |
| 20 | +
|
| 21 | +# Determine and save absolute path to program directory. |
| 22 | +# When done will be in same directory as script: |
| 23 | +
|
| 24 | +PGM=`basename $0` # Name of the program |
| 25 | +CDIR=`pwd` # Save directory program was run from |
| 26 | +
|
| 27 | +PDIR=`dirname $0` |
| 28 | +cd $PDIR |
| 29 | +PDIR=`pwd` |
| 30 | +
|
| 31 | +# If program accepts filenames as arguments, this will put us back where we started. |
| 32 | +# (Needed so references to files using relative paths work.): |
| 33 | +
|
| 34 | +cd $CDIR |
| 35 | +
|
| 36 | +# Use this if script must be run by certain user: |
| 37 | +
|
| 38 | +runby="root" |
| 39 | +iam=`/usr/bin/id -un` |
| 40 | +if [ $iam != "$runby" ] |
| 41 | +then |
| 42 | + echo "$PGM : program must be run by user \"$runby\"" |
| 43 | + exit |
| 44 | +fi |
| 45 | +
|
| 46 | +# Check for missing parameter. |
| 47 | +# Display usage message and exit if it is missing: |
| 48 | +
|
| 49 | +if [ "$1" = "" ] |
| 50 | +then |
| 51 | + echo "$PGM : parameter 1 is required" |
| 52 | + echo "Usage: $PGM param-one" |
| 53 | + exit |
| 54 | +fi |
| 55 | +
|
| 56 | +# Prompt for data (in this case a yes/no response that defaults to "N"): |
| 57 | +
|
| 58 | +/bin/echo -n "Do you wish to continue? [y/N] " |
| 59 | +read yn |
| 60 | +if [ "$yn" != "y" ] && [ "$yn" != "Y" ] |
| 61 | +then |
| 62 | + echo "Cancelling..." |
| 63 | + exit; |
| 64 | +fi |
| 65 | +
|
| 66 | +# If only one copy of your script can run at a time use this block of code. |
| 67 | +# Check for lock file. If it doesn't exist create it. |
| 68 | +# If it does exist display error message and exit: |
| 69 | +
|
| 70 | +LOCKF="/tmp/${PGM}.lock" |
| 71 | +if [ ! -e $LOCKF ] |
| 72 | +then |
| 73 | + touch $LOCKF |
| 74 | +else |
| 75 | + echo "$PGM: cannot continue -- lock file exists" |
| 76 | + echo |
| 77 | + echo "To continue make sure this program is not already running, then delete the" |
| 78 | + echo "lock file:" |
| 79 | + echo |
| 80 | + echo " rm -f $LOCKF" |
| 81 | + echo |
| 82 | + echo "Aborting..." |
| 83 | + exit 0 |
| 84 | +fi |
| 85 | +
|
| 86 | +script_list=`ls customer/*` |
| 87 | +
|
| 88 | +for script in $script_list |
| 89 | +do |
| 90 | + if [ $script != $PGM ] |
| 91 | + then |
| 92 | + echo "./${script}" |
| 93 | + fi |
| 94 | +done |
| 95 | +
|
| 96 | +# Remove the lock file |
| 97 | +
|
| 98 | +rm -f $LOCKF |
| 99 | +``` |
| 100 | + |
| 101 | +## Conclusion |
| 102 | + |
| 103 | +Scripting is a System Administrators friend. Being able to quickly do certain tasks in a script streamlines process completion. While by no means an exhaustive set of script routines, this stub offers some common usage examples. |
0 commit comments