]> git.ipfire.org Git - people/ms/network.git/blame - functions.list
Add functions to handle lists very easily.
[people/ms/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
31 eval "${list}=\"${!list} $@\""
32}
33
34function list_remove() {
35 local list=${1}
36 shift
37
38 assert isset list
39
40 local _list k
41 for k in ${!list}; do
42 listmatch ${k} $@ && continue
43
44 _list="${_list} ${k}"
45 done
46
47 eval "${list}=\"${_list}\""
48}
49
50function list_sort() {
51 local i
52 for i in $@; do
53 print "${i}"
54 done | sort | tr '\n' ' '
55 print
56}
57
58function list_match() {
59 local match=${1}
60 shift
61
62 local i
63 for i in $@; do
64 [ "${match}" = "${i}" ] && return ${EXIT_OK}
65 done
66
67 return ${EXIT_ERROR}
68}
69
70function list_length() {
71 local length=0
72
73 local i
74 for i in $@; do
75 length=$(( ${length} + 1 ))
76 done
77
78 print "${length}"
79}