Merge branch 'release-2.140.101'
[feisty_meow.git] / infobase / examples / bashisms / fred_techniques.txt
1 #!/bin/bash
2
3 ##############
4 #  Name   : fred_techniques
5 #  Author : Chris Koeritz
6 #  Rights : Copyright (C) 2010-$now by Author
7 ##############
8 # Copyright (c) 2010-$now By Author.  This script is free software; you can
9 # redistribute it and/or modify it under the terms of the simplified BSD
10 # license.  See: http://www.opensource.org/licenses/bsd-license.php
11 # Please send updates for this code to: fred@gruntose.com -- Thanks, fred.
12 ##############
13
14 # this script is a collection of helpful bash practices that unfortunately
15 # sometimes slip my mind when i need them.  it's intended to collect all the
16 # good bits so they don't slip away.  feel free to re-use them in your own
17 # code as needed.
18
19 ##############
20
21 # clean for loops in bash:
22
23 for ((i=0; i < 5; i++)) do
24   echo $i
25 done
26
27 ##############
28
29 # removing an array element
30 # --> only works on arrays that have no elements containing a space character.
31
32 # define a 5 element array.
33 arr=(a b c d e)
34
35 # remove element 2 (the 'c').
36 removepoint=2
37
38 # set the array to slices of itself.
39 arr=(${arr[*]:0:$removepoint} ${arr[*]:(($removepoint+1))} )
40
41 # show the new contents.
42 echo ${arr[*]}
43 # shows: a b d e
44
45 ##############
46
47 # store to a variable name by derefercing it.
48
49 # shows how you can store into a variable when you are given only its name.
50 function store_to_named_var()
51 {
52   local name="$1"; shift
53   eval ${name}=\(gorbachev "perestroikanator 12000" chernenko\)
54 }
55
56 declare -a ted=(petunia "butter cup" smorgasbord)
57 echo ted is ${ted[@]}
58 store_to_named_var ted
59 echo ted is now ${ted[@]}
60
61 ##############
62