Debian Codegraph @

ex4.sh

raw symbols SCIP
1 #!/bin/bash
2
3 # subst.sh: a script that substitutes one pattern for
4 #+ another in a file,
5 #+ i.e., "sh subst.sh Smith Jones letter.txt".
6 # Jones replaces Smith.
7
8 ARGS=3 # Script requires 3 arguments.
9 E_BADARGS=85 # Wrong number of arguments passed to script.
10
11 if [ $# -ne "$ARGS" ]
12 then
13 echo "Usage: `basename $0` old-pattern new-pattern filename"
14 exit $E_BADARGS
15 fi
16
17 old_pattern=$1
18 new_pattern=$2
19
20 if [ -f "$3" ]
21 then
22 file_name=$3
23 else
24 echo "File \"$3\" does not exist."
25 exit $E_BADARGS
26 fi
27
28
29 # -----------------------------------------------
30 # Here is where the heavy work gets done.
31 sed -e "s/$old_pattern/$new_pattern/g" $file_name
32 # -----------------------------------------------
33
34 # 's' is, of course, the substitute command in sed,
35 #+ and /pattern/ invokes address matching.
36 # The 'g,' or global flag causes substitution for EVERY
37 #+ occurence of $old_pattern on each line, not just the first.
38 # Read the 'sed' docs for an in-depth explanation.
39
40 exit $? # Redirect the output of this script to write to a file.