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)
59 # check for availability
60 if ! \which "${ARG}" >/dev/null 2>&1
62 printf "%s: binary not found\n" "${ARG}" >&2
68 # return non zero, if at least one prog is missing!
73 # {{{ simple shell grep
75 case "$(cat $2)" in *$1*) return 0;; esac
80 # {{{ simple shell grep for strings
82 case "$2" in *$1*) return 0;; esac
87 # {{{ reread boot command line; echo last parameter's argument or return false.
89 CMDLINE=$(cat /proc/cmdline)
90 stringinstring " $1=" "$CMDLINE" || return 1
91 result="${CMDLINE##*$1=}"
92 result="${result%%[ ]*}"
98 # {{{ check boot commandline for specified option
100 stringinfile " $1" /proc/cmdline
105 # {{{ check whether $1 is yes
107 if [ "$1" = "yes" -o "$1" = "YES" ] ; then
115 # {{{ grml specific checks
117 [ -f /etc/grml_version ] && return 0 || return 1
121 cat /etc/grml_version
125 [ -f /etc/grml_cd ] && return 0 || return 1
129 [ -f /etc/grml_cd ] && return 1 || return 0
133 grep -q small /etc/grml_version 2>/dev/null && return 0 || return 1
137 # {{{ filesystems (proc, pts, sys)
139 check4root || return 1
140 [ -f /proc/version ] || mount -t proc /proc /proc 2>/dev/null
144 check4root || return 1
145 stringinfile "/dev/pts" /proc/mounts || mount -t devpts /dev/pts /dev/pts 2>/dev/null
149 check4root || return 1
150 [ -d /sys/devices ] || mount -t sysfs /sys /sys 2>/dev/null
154 # char *reverse_list(list) {{{
156 # Returns the reversed order of list
161 while [ "$#" -gt 0 ] ; do
162 if [ -z "${ret}" ] ; then
173 # bool is_older_than(reference, files/dirs to check) {{{
175 # return 0 if any of the files/dirs are newer than
178 # EXAMPLE: if is_older_than a.out *.o ; then ...
185 [ "${x}" -nt "${ref}" ] && return 0
187 if [ -d "${x}" ] ; then
188 is_older_than "${ref}" "${x}"/* && return 0
196 ## END OF FILE #################################################################
197 # vim:foldmethod=marker tw=80 ai expandtab shiftwidth=2 tabstop=2