File: insparnum

package info (click to toggle)
afbackup 3.3.8.1beta2-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,128 kB
  • ctags: 3,370
  • sloc: ansic: 46,932; sh: 4,654; tcl: 4,199; makefile: 536; csh: 416; perl: 133; sed: 93
file content (80 lines) | stat: -rwxr-xr-x 1,611 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/sh
#
# $RCSfile: insparnum,v $
#

filename="$3"
startnum="$1"
endnum="$2"

usage(){
  echo `basename $1` '<start-number> <end-number> <filename>'
  exit 2
}

if [ $# -ne 3 ] ; then
  usage $0
fi

echo "$startnum"|grep '^[1-9][0-9]*$' >/dev/null
EST1=$?
echo "$endnum"|grep '^[1-9][0-9]*$' >/dev/null
if [ $? -ne 0 -o $EST1 -ne 0 ] ; then
  echo "Error: Argument 1 and 2 must be numbers" >&2
  usage $0
fi
if [ $startnum -ge $endnum ] ; then
  echo "Error: Argument 1 must be a smaller number than argument 2" >&2
  usage $0
fi

if [ ! -r "$filename" ] ; then
  echo "Error: File $filename must be readable" >&2
  exit 3
fi

head -1 "$filename" | egrep '/bin/(wi|tcl)sh' >/dev/null
if [ $? -eq 0 ] ; then
  STARTCHRS="("
  ENDCHRS=")"
else
  STARTCHRS="__"
  ENDCHRS=""
fi

TMPFILE=/tmp/modscipt.$$
TMPFILE2="$TMPFILE".2
/bin/rm -f $TMPFILE $TMPFILE2
if [ -f $TMPFILE -o -f $TMPFILE2 ] ; then
  echo "Error: Cannot remove file $TMPFILE" >&2
  exit 4
fi

cp "$filename" $TMPFILE
if [ $? -ne 0 ] ; then
  echo "Error: cannot copy file $filename" >&2
  /bin/rm -f $TMPFILE $TMPFILE2
  exit 5
fi

I=$endnum
while [ $I -ge $startnum ] ; do
  I_1=`expr $I + 1`
  sed s/"$STARTCHRS$I$ENDCHRS/$STARTCHRS$I_1$ENDCHRS"/g $TMPFILE > $TMPFILE2
  if [ $? -ne 0 ] ; then
    echo "Error occured during rewrite of $TMPFILE." >&2
    /bin/rm -f $TMPFILE $TMPFILE2
    exit 6
  fi
  /bin/mv $TMPFILE2 $TMPFILE
  if [ $? -ne 0 ] ; then
    echo "Error occured during rename of $TMPFILE2 to $TMPFILE." >&2
    /bin/rm -f $TMPFILE $TMPFILE2
    exit 7
  fi

  I=`expr $I - 1`
done

cat $TMPFILE
/bin/rm -f $TMPFILE $TMPFILE2