~ubuntu-branches/ubuntu/trusty/abs-guide/trusty-proposed

« back to all changes in this revision

Viewing changes to fetch_address-2.sh

  • Committer: Package Import Robot
  • Author(s): Sandro Tosi
  • Date: 2012-06-03 10:57:27 UTC
  • mfrom: (1.2.6)
  • Revision ID: package-import@ubuntu.com-20120603105727-rm7frl4feikr2swm
Tags: 6.5-1
* New upstream release
* debian/watch
  - updated
* debian/abs-guide.lintian-overrides
  - updated for new upstream code
* debian/control
  - bump Standards-Version to 3.9.3 (no changes needed)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash4
 
2
# fetch_address-2.sh
 
3
# A more elaborate version of fetch_address.sh.
 
4
 
 
5
SUCCESS=0
 
6
E_DB=99    # Error code for missing entry.
 
7
 
 
8
declare -A address
 
9
#       -A option declares associative array.
 
10
 
 
11
 
 
12
store_address ()
 
13
{
 
14
  address[$1]="$2"
 
15
  return $?
 
16
}
 
17
 
 
18
 
 
19
fetch_address ()
 
20
{
 
21
  if [[ -z "${address[$1]}" ]]
 
22
  then
 
23
    echo "$1's address is not in database."
 
24
    return $E_DB
 
25
  fi
 
26
 
 
27
  echo "$1's address is ${address[$1]}."
 
28
  return $?
 
29
}
 
30
 
 
31
 
 
32
store_address "Lucas Fayne" "414 W. 13th Ave., Baltimore, MD 21236"
 
33
store_address "Arvid Boyce" "202 E. 3rd St., New York, NY 10009"
 
34
store_address "Velma Winston" "1854 Vermont Ave, Los Angeles, CA 90023"
 
35
#  Exercise:
 
36
#  Rewrite the above store_address calls to read data from a file,
 
37
#+ then assign field 1 to name, field 2 to address in the array.
 
38
#  Each line in the file would have a format corresponding to the above.
 
39
#  Use a while-read loop to read from file, sed or awk to parse the fields.
 
40
 
 
41
fetch_address "Lucas Fayne"
 
42
# Lucas Fayne's address is 414 W. 13th Ave., Baltimore, MD 21236.
 
43
fetch_address "Velma Winston"
 
44
# Velma Winston's address is 1854 Vermont Ave, Los Angeles, CA 90023.
 
45
fetch_address "Arvid Boyce"
 
46
# Arvid Boyce's address is 202 E. 3rd St., New York, NY 10009.
 
47
fetch_address "Bozo Bozeman"
 
48
# Bozo Bozeman's address is not in database.
 
49
 
 
50
exit $?   # In this case, exit code = 99, since that is function return.