]> git.ipfire.org Git - people/stevee/network.git/blame - functions.list
cli: Check for correct arguments in cli_ident.
[people/stevee/network.git] / functions.list
CommitLineData
e726ef8d
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2012 IPFire Network Development Team #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21#
22# Functions for nice handling of lists.
23#
24
25function list_append() {
26 local list=${1}
27 shift
28
29 assert isset list
30
7aa696d1
MT
31 if [ -n "${!list}" ]; then
32 printf -v ${list} "${!list} $@"
33 else
34 printf -v ${list} "$@"
35 fi
e726ef8d
MT
36}
37
38function list_remove() {
39 local list=${1}
40 shift
41
42 assert isset list
43
44 local _list k
45 for k in ${!list}; do
46 listmatch ${k} $@ && continue
47
48 _list="${_list} ${k}"
49 done
50
51 eval "${list}=\"${_list}\""
52}
53
54function list_sort() {
55 local i
56 for i in $@; do
57 print "${i}"
58 done | sort | tr '\n' ' '
59 print
60}
61
62function list_match() {
63 local match=${1}
64 shift
65
66 local i
67 for i in $@; do
68 [ "${match}" = "${i}" ] && return ${EXIT_OK}
69 done
70
71 return ${EXIT_ERROR}
72}
73
74function list_length() {
75 local length=0
76
77 local i
78 for i in $@; do
79 length=$(( ${length} + 1 ))
80 done
81
82 print "${length}"
83}
55ea0266
MT
84
85# Count how often $1 occurs in the list.
86function list_count() {
87 local what=${1}
88 shift
89
90 local counter=0
91
92 local arg
93 for arg in $@; do
94 if [ "${arg}" = "${what}" ]; then
95 counter=$(( ${counter} + 1 ))
96 fi
97 done
98
99 print "${counter}"
100}