script-functions: Set filetype to "sh" with vim
[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 RTN=0
53     local ARG=''
54     while [ ${#} -gt 0 ]
55     do
56         ARG="${1}"
57         shift
58
59         # check for availability
60         if ! \which "${ARG}" >/dev/null 2>&1
61         then
62             printf "%s: binary not found\n" "${ARG}" >&2
63             RTN=1
64         fi
65
66     done
67
68     # return non zero, if at least one prog is missing!
69     return ${RTN}
70 }
71 # }}}
72
73 # {{{ simple shell grep
74 stringinfile(){
75   case "$(cat $2)" in *$1*) return 0;; esac
76   return 1
77 }
78 # }}}
79
80 # {{{ simple shell grep for strings
81 stringinstring(){
82   case "$2" in *$1*) return 0;; esac
83   return 1
84 }
85 # }}}
86
87 # {{{ reread boot command line; echo last parameter's argument or return false.
88 getbootparam(){
89   CMDLINE=$(cat /proc/cmdline)
90   stringinstring " $1=" "$CMDLINE" || return 1
91   result="${CMDLINE##*$1=}"
92   result="${result%%[   ]*}"
93   echo "$result"
94   return 0
95 }
96 # }}}
97
98 # {{{ check boot commandline for specified option
99 checkbootparam(){
100   stringinfile " $1" /proc/cmdline
101   return "$?"
102 }
103 # }}}
104
105 # {{{ check whether $1 is yes
106 checkvalue(){
107   if [ "$1" = "yes" -o "$1" = "YES" ] ; then
108     return 0
109   else
110     return 1
111   fi
112 }
113 # }}}
114
115 # {{{ grml specific checks
116 isgrml(){
117   [ -f /etc/grml_version ] && return 0 || return 1
118 }
119
120 grmlversion(){
121  cat /etc/grml_version
122 }
123
124 isgrmlcd(){
125   [ -f /etc/grml_cd ] && return 0 || return 1
126 }
127
128 isgrmlhd(){
129   [ -f /etc/grml_cd ] && return 1 || return 0
130 }
131
132 checkgrmlsmall(){
133   grep -q small /etc/grml_version 2>/dev/null && return 0 || return 1
134 }
135 # }}}
136
137 # {{{ filesystems (proc, pts, sys)
138 mount_proc(){
139   check4root || return 1
140   [ -f /proc/version ] || mount -t proc /proc /proc 2>/dev/null
141 }
142
143 mount_pts(){
144   check4root || return 1
145   stringinfile "/dev/pts" /proc/mounts || mount -t devpts /dev/pts /dev/pts 2>/dev/null
146 }
147
148 mount_sys(){
149   check4root || return 1
150   [ -d /sys/devices ] || mount -t sysfs /sys /sys 2>/dev/null
151 }
152 # }}}
153
154 # char *reverse_list(list) {{{
155 #
156 #   Returns the reversed order of list
157 #
158 reverse_list() {
159   local ret
160   ret=''
161   while [ "$#" -gt 0 ] ; do
162     if [ -z "${ret}" ] ; then
163       ret="$1"
164     else
165       ret="$1 ${ret}"
166     fi
167     shift
168   done
169   printf '%s' "${ret}"
170 }
171 #}}}
172
173 # bool is_older_than(reference, files/dirs to check) {{{
174 #
175 #   return 0 if any of the files/dirs are newer than
176 #   the reference file
177 #
178 #   EXAMPLE: if is_older_than a.out *.o ; then ...
179 is_older_than() {
180   local x
181   local ref="$1"
182   shift
183
184   for x in "$@" ; do
185     [ "${x}" -nt "${ref}" ] && return 0
186
187     if [ -d "${x}" ] ; then
188       is_older_than "${ref}" "${x}"/* && return 0
189     fi
190   done
191
192   return 1
193 }
194 #}}}
195
196 ## END OF FILE #################################################################
197 # vim:foldmethod=marker tw=80 ai expandtab shiftwidth=2 tabstop=8 ft=sh