1786072d771c8c634af233be48314801582f5b40
[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   stringinstring " $1=" /proc/cmdline || return 1
80   result="${/proc/cmdline##*$1=}"
81   result="${result%%[   ]*}"
82   echo "$result"
83   return 0
84 }
85 # }}}
86
87 # {{{ check boot commandline for specified option
88 checkbootparam(){
89   stringinstring " $1" /proc/cmdline
90   return "$?"
91 }
92 # }}}
93
94 # {{{ check whether $1 is yes
95 checkvalue(){
96   if [ "$1" = "yes" -o "$1" = "YES" ] ; then
97     return 0
98   else
99     return 1
100   fi
101 }
102 # }}}
103
104 # {{{ grml specific checks
105 isgrml(){
106   [ -f /etc/grml_version ] && return 0 || return 1
107 }
108
109 grmlversion(){
110  cat /etc/grml_version
111 }
112
113 isgrmlcd(){
114   [ -f /etc/grml_cd ] && return 0 || return 1
115 }
116
117 isgrmlhd(){
118   [ -f /etc/grml_cd ] && return 1 || return 0
119 }
120
121 checkgrmlsmall(){
122   grep -q small /etc/grml_version 2>/dev/null && return 0 || return 1
123 }
124 # }}}
125
126 # {{{ filesystems (proc, pts, sys)
127 mount_proc(){
128   check4root || return 1
129   [ -f /proc/version ] || mount -t proc /proc /proc 2>/dev/null
130 }
131
132 mount_pts(){
133   check4root || return 1
134   stringinfile "/dev/pts" /proc/mounts || mount -t devpts /dev/pts /dev/pts 2>/dev/null
135 }
136
137 mount_sys(){
138   check4root || return 1
139   [ -d /sys/devices ] || mount -t sysfs /sys /sys 2>/dev/null
140 }
141 # }}}
142
143 # char *reverse_list(list) {{{
144 #
145 #   Returns the reversed order of list
146 #
147 reverse_list() {
148   local ret
149   ret=''
150   while [ "$#" -gt 0 ] ; do
151     if [ -z "${ret}" ] ; then
152       ret="$1"
153     else
154       ret="$1 ${ret}"
155     fi
156     shift
157   done
158   printf '%s' "${ret}"
159 }
160 #}}}
161
162 # bool is_older_than(reference, files/dirs to check) {{{
163 #
164 #   return 0 if any of the files/dirs are newer than
165 #   the reference file
166 #
167 #   EXAMPLE: if is_older_than a.out *.o ; then ...
168 is_older_than() {
169   local x
170   local ref="$1"
171   shift
172
173   for x in "$@" ; do
174     [ "${x}" -nt "${ref}" ] && return 0
175
176     if [ -d "${x}" ] ; then
177       is_older_than "${ref}" "${x}"/* && return 0
178     fi
179   done
180
181   return 1
182 }
183 #}}}
184
185 ## END OF FILE #################################################################
186 # vim:foldmethod=marker tw=80 ai expandtab shiftwidth=2 tabstop=2