script-functions: fix getbootparam/checkbootparam
[grml-etc-core.git] / etc / grml / script-functions
1 # Filename:      /etc/grml/script-functions
2 # Purpose:       some often used functions for use in shellscripts
3 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
4 # Bug-Reports:   see http://grml.org/bugs/
5 # License:       This file is licensed under the GPL v2.
6 ################################################################################
7
8 # {{{ set default PATH
9 setpath(){
10   export PATH=${PATH:-'/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin:/usr/local/bin'}
11 }
12 # }}}
13
14 # {{{ check for root-permissions
15 check4root(){
16   if [ "$(id -u 2>/dev/null)" != 0 ] ; then
17     echo 1>&2 "Error: please run this script with uid 0 (root)." ; return 1
18   fi
19 }
20 # }}}
21
22 # {{{ check for user permissions
23 check4user(){
24   if [ "$(id -u 2>/dev/null)" = 0 ] ; then
25     echo 1>&2 "Error: please do not run this script with uid 0 (root)." ; return 1
26   fi
27 }
28 # }}}
29
30 # {{{ check for running zsh
31 iszsh(){
32   if ! [ -z "$ZSH_VERSION" ] ; then
33     return 0
34   else
35     return 1
36   fi
37 }
38 # }}}
39
40 # {{{ check for (X)dialog
41 setdialog(){
42   if [ -n "$DISPLAY" ] ; then
43      [ -x /usr/bin/Xdialog ] && DIALOG="Xdialog" && export XDIALOG_HIGH_DIALOG_COMPAT=1
44   else
45      [ -x /usr/bin/dialog ] && DIALOG='dialog' || ( echo 1>&2 "dialog not available" ; return 1 )
46   fi
47 }
48 # }}}
49
50 # {{{ check for availability of program(s)
51 check4progs(){
52   local RC=''
53   for arg in $* ; do
54     which $arg >/dev/null 2>&1 || RC="$arg"
55   done
56   if [ -n "$RC" ] ; then
57      echo "$RC not installed"
58      return 1
59   fi
60 }
61 # }}}
62
63 # {{{ simple shell grep
64 stringinfile(){
65   case "$(cat $2)" in *$1*) return 0;; esac
66   return 1
67 }
68 # }}}
69
70 # {{{ simple shell grep for strings
71 stringinstring(){
72   case "$2" in *$1*) return 0;; esac
73   return 1
74 }
75 # }}}
76
77 # {{{ reread boot command line; echo last parameter's argument or return false.
78 getbootparam(){
79   CMDLINE=$(cat /proc/cmdline)
80   stringinstring " $1=" "$CMDLINE" || return 1
81   result="${CMDLINE##*$1=}"
82   result="${result%%[   ]*}"
83   echo "$result"
84   return 0
85 }
86 # }}}
87
88 # {{{ check boot commandline for specified option
89 checkbootparam(){
90   stringinfile " $1" /proc/cmdline
91   return "$?"
92 }
93 # }}}
94
95 # {{{ check whether $1 is yes
96 checkvalue(){
97   if [ "$1" = "yes" -o "$1" = "YES" ] ; then
98     return 0
99   else
100     return 1
101   fi
102 }
103 # }}}
104
105 # {{{ grml specific checks
106 isgrml(){
107   [ -f /etc/grml_version ] && return 0 || return 1
108 }
109
110 grmlversion(){
111  cat /etc/grml_version
112 }
113
114 isgrmlcd(){
115   [ -f /etc/grml_cd ] && return 0 || return 1
116 }
117
118 isgrmlhd(){
119   [ -f /etc/grml_cd ] && return 1 || return 0
120 }
121
122 checkgrmlsmall(){
123   grep -q small /etc/grml_version 2>/dev/null && return 0 || return 1
124 }
125 # }}}
126
127 # {{{ filesystems (proc, pts, sys)
128 mount_proc(){
129   check4root || return 1
130   [ -f /proc/version ] || mount -t proc /proc /proc 2>/dev/null
131 }
132
133 mount_pts(){
134   check4root || return 1
135   stringinfile "/dev/pts" /proc/mounts || mount -t devpts /dev/pts /dev/pts 2>/dev/null
136 }
137
138 mount_sys(){
139   check4root || return 1
140   [ -d /sys/devices ] || mount -t sysfs /sys /sys 2>/dev/null
141 }
142 # }}}
143
144 # char *reverse_list(list) {{{
145 #
146 #   Returns the reversed order of list
147 #
148 reverse_list() {
149   local ret
150   ret=''
151   while [ "$#" -gt 0 ] ; do
152     if [ -z "${ret}" ] ; then
153       ret="$1"
154     else
155       ret="$1 ${ret}"
156     fi
157     shift
158   done
159   printf '%s' "${ret}"
160 }
161 #}}}
162
163 # bool is_older_than(reference, files/dirs to check) {{{
164 #
165 #   return 0 if any of the files/dirs are newer than
166 #   the reference file
167 #
168 #   EXAMPLE: if is_older_than a.out *.o ; then ...
169 is_older_than() {
170   local x
171   local ref="$1"
172   shift
173
174   for x in "$@" ; do
175     [ "${x}" -nt "${ref}" ] && return 0
176
177     if [ -d "${x}" ] ; then
178       is_older_than "${ref}" "${x}"/* && return 0
179     fi
180   done
181
182   return 1
183 }
184 #}}}
185
186 ## END OF FILE #################################################################
187 # vim:foldmethod=marker tw=80 ai expandtab shiftwidth=2 tabstop=2