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 ################################################################################
10 export PATH=${PATH:-'/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin:/usr/local/bin'}
14 # {{{ check for root-permissions
16 if [ "$(id -u 2>/dev/null)" != 0 ] ; then
17 echo 1>&2 "Error: please run this script with uid 0 (root)." ; return 1
22 # {{{ check for user permissions
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
30 # {{{ check for running zsh
32 if ! [ -z "$ZSH_VERSION" ] ; then
40 # {{{ check for (X)dialog
42 if [ -n "$DISPLAY" ] ; then
43 [ -x /usr/bin/Xdialog ] && DIALOG="Xdialog" && export XDIALOG_HIGH_DIALOG_COMPAT=1
45 [ -x /usr/bin/dialog ] && DIALOG='dialog' || ( echo 1>&2 "dialog not available" ; return 1 )
50 # {{{ check for availability of program(s)
52 # check4progs [-s,-q,--quiet,--silent] arg [arg .... argn]
54 # with option given either of:
55 # -s,-q,--quiet,--silent
57 # check for available progs but produce no output
59 [ -n "${ZSH_VERSION}" ] && emulate -L sh
66 -q | -s | --quiet | --silent)
84 if [ -x "${d}/${ARG}" ]
92 # check for availability
95 if [ ${VERBOSE} -eq 1 ]
97 printf "%s: binary not found\n" "${ARG}" >&2
103 # return non zero, if at least one prog is missing!
108 # {{{ simple shell grep
110 case "$(cat $2)" in *$1*) return 0;; esac
115 # {{{ simple shell grep for strings
117 case "$2" in *$1*) return 0;; esac
122 # {{{ reread boot command line; echo last parameter's argument or return false.
124 CMDLINE=$(cat /proc/cmdline)
125 stringinstring " $1=" "$CMDLINE" || return 1
126 result="${CMDLINE##*$1=}"
127 result="${result%%[ ]*}"
133 # {{{ check boot commandline for specified option
135 stringinfile " $1" /proc/cmdline
140 # {{{ check whether $1 is yes
142 if [ "$1" = "yes" -o "$1" = "YES" ] ; then
150 # {{{ grml specific checks
152 [ -f /etc/grml_version ] && return 0 || return 1
156 cat /etc/grml_version
160 [ -f /etc/grml_cd ] && return 0 || return 1
164 [ -f /etc/grml_cd ] && return 1 || return 0
168 grep -q small /etc/grml_version 2>/dev/null && return 0 || return 1
172 # {{{ filesystems (proc, pts, sys)
174 check4root || return 1
175 [ -f /proc/version ] || mount -t proc /proc /proc 2>/dev/null
179 check4root || return 1
180 stringinfile "/dev/pts" /proc/mounts || mount -t devpts /dev/pts /dev/pts 2>/dev/null
184 check4root || return 1
185 [ -d /sys/devices ] || mount -t sysfs /sys /sys 2>/dev/null
189 # char *reverse_list(list) {{{
191 # Returns the reversed order of list
196 while [ "$#" -gt 0 ] ; do
197 if [ -z "${ret}" ] ; then
208 # bool is_older_than(reference, files/dirs to check) {{{
210 # return 0 if any of the files/dirs are newer than
213 # EXAMPLE: if is_older_than a.out *.o ; then ...
220 [ "${x}" -nt "${ref}" ] && return 0
222 if [ -d "${x}" ] ; then
223 is_older_than "${ref}" "${x}"/* && return 0
231 ## END OF FILE #################################################################
232 # vim:foldmethod=marker tw=80 ai expandtab shiftwidth=2 tabstop=8 ft=sh