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

« back to all changes in this revision

Viewing changes to abs/patt-matching.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/bash
2
 
# patt-matching.sh
3
 
 
4
 
# Pattern matching  using the # ## % %% parameter substitution operators.
5
 
 
6
 
var1=abcd12345abc6789
7
 
pattern1=a*c  # * (wild card) matches everything between a - c.
8
 
 
9
 
echo
10
 
echo "var1 = $var1"           # abcd12345abc6789
11
 
echo "var1 = ${var1}"         # abcd12345abc6789
12
 
                              # (alternate form)
13
 
echo "Number of characters in ${var1} = ${#var1}"
14
 
echo
15
 
 
16
 
echo "pattern1 = $pattern1"   # a*c  (everything between 'a' and 'c')
17
 
echo "--------------"
18
 
echo '${var1#$pattern1}  =' "${var1#$pattern1}"    #         d12345abc6789
19
 
# Shortest possible match, strips out first 3 characters  abcd12345abc6789
20
 
#                                     ^^^^^               |-|
21
 
echo '${var1##$pattern1} =' "${var1##$pattern1}"   #                  6789      
22
 
# Longest possible match, strips out first 12 characters  abcd12345abc6789
23
 
#                                    ^^^^^                |----------|
24
 
 
25
 
echo; echo; echo
26
 
 
27
 
pattern2=b*9            # everything between 'b' and '9'
28
 
echo "var1 = $var1"     # Still  abcd12345abc6789
29
 
echo
30
 
echo "pattern2 = $pattern2"
31
 
echo "--------------"
32
 
echo '${var1%pattern2}  =' "${var1%$pattern2}"     #     abcd12345a
33
 
# Shortest possible match, strips out last 6 characters  abcd12345abc6789
34
 
#                                     ^^^^                         |----|
35
 
echo '${var1%%pattern2} =' "${var1%%$pattern2}"    #     a
36
 
# Longest possible match, strips out last 12 characters  abcd12345abc6789
37
 
#                                    ^^^^                 |-------------|
38
 
 
39
 
# Remember, # and ## work from the left end (beginning) of string,
40
 
#           % and %% work from the right end.
41
 
 
42
 
echo
43
 
 
44
 
exit 0