Bash-4.3 distribution sources and documentation
[platform/upstream/bash.git] / examples / functions / ksh-compat-test
1 #
2 #  Chet Ramey <chet.ramey@case.edu>
3 #
4 #  Copyright 1999 Chester Ramey
5 #
6 #   This program is free software; you can redistribute it and/or modify
7 #   it under the terms of the GNU General Public License as published by
8 #   the Free Software Foundation; either version 2, or (at your option)
9 #   any later version.
10 #
11 #   TThis program is distributed in the hope that it will be useful,
12 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #   GNU General Public License for more details.
15 #
16 #   You should have received a copy of the GNU General Public License
17 #   along with this program; if not, write to the Free Software Foundation,
18 #   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #
20 # replacements for test/[ that do arithmetic expansion on the operands to
21 # the arithmetic operators, like ksh.
22 #
23 function test()
24 {
25         local -i n1 n3
26         case "$#" in
27         3)      case "$2" in
28                 -lt|-gt|-eq|-ne|-le|-ge)        n1=$(( $1 ))
29                                                 n3=$(( $3 ))
30                                                 builtin test "$n1" $2 "$n3"
31                                                 return $?;;
32                 *)      builtin test "$@" ;;
33                 esac;;                                                          
34         *)      builtin test "$@" ;;
35         esac
36 }
37
38 function [()
39 {
40         local -i n1 n3
41         case "$#" in
42         4)      case "$2" in
43                 -lt|-gt|-eq|-ne|-le|-ge)        n1=$(( $1 ))
44                                                 n3=$(( $3 ))
45                                                 builtin [ "$n1" $2 "$n3" ]
46                                                 return $?;;
47                 *)      builtin [ "$@" ;;
48                 esac;;                                                          
49         *)      builtin [ "$@" ;;
50         esac
51 }
52
53 q=7
54
55 [ q -lt 10 ]
56 echo $?
57 [ $q -lt 10 ]
58 echo $?