File: next_param

package info (click to toggle)
mercury 0.9-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 18,488 kB
  • ctags: 9,800
  • sloc: objc: 146,680; ansic: 51,418; sh: 6,436; lisp: 1,567; cpp: 1,040; perl: 854; makefile: 450; asm: 232; awk: 203; exp: 32; fortran: 3; csh: 1
file content (48 lines) | stat: -rwxr-xr-x 1,239 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
#!/bin/sh
#
# The scripts cur_param and next_param allow their callers to cycle through
# circular lists of parameters. Both scripts take two parameters, a directory
# name and a counter name.
#
# The idea is that the user sets up the file $dir/list.$counter to contain
# a list of parameters, one per line. Each time the user calls cur_param,
# they get back as the output of the script the current parameter (initially
# the first). Each time the user calls next_param, the scripts' notion of
# the current parameter is set to the parameter on the next line of the file,
# or, if there are none left, back to the parameter on the first line.
# next_param has only this side-effect; it does not output anything.
#
# Both scripts exit with a non-zero status in case of internal error.

usage="next_param dir counter"

if test $# != 2
then
	echo $usage
	exit 1
fi

dir=$1
counter=$2

if test ! -f $dir/next.$counter
then
	echo 0 > $dir/next.$counter
fi

if test -s $dir/list.$counter
then
	prev=`cat $dir/next.$counter`
	next=`expr $prev + 1`
	length=`wc -l $dir/list.$counter`
	if test "$next" -gt "$length"
	then
		next=1
	fi
	echo "$next" > $dir/next.$counter
else
	echo "$dir/list.$counter doesn't exist or is empty"
	exit 1
fi

exit 0