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 # Latest change: Tue, 23 Oct 2007 16:53:48 +0200 [ft]
7 ################################################################################
11 export PATH=${PATH:-'/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin:/usr/local/bin'}
15 # {{{ check for root-permissions
17 if [ "$(id -u 2>/dev/null)" != 0 ] ; then
18 echo 1>&2 "Error: please run this script with uid 0 (root)." ; return 1
23 # {{{ check for user permissions
25 if [ "$(id -u 2>/dev/null)" = 0 ] ; then
26 echo 1>&2 "Error: please do not run this script with uid 0 (root)." ; return 1
31 # {{{ check for running zsh
33 if ! [ -z "$ZSH_VERSION" ] ; then
41 # {{{ check for (X)dialog
43 if [ -n "$DISPLAY" ] ; then
44 [ -x /usr/bin/Xdialog ] && DIALOG="Xdialog" && export XDIALOG_HIGH_DIALOG_COMPAT=1
46 [ -x /usr/bin/dialog ] && DIALOG='dialog' || ( echo 1>&2 "dialog not available" ; return 1 )
51 # {{{ check for availability of program(s)
55 type -p $arg >/dev/null 2>&1 || RC="$arg"
57 if [ -n "$RC" ] ; then
58 echo "$RC not installed"
64 # {{{ simple shell grep
66 case "$(cat $2)" in *$1*) return 0;; esac
71 # {{{ simple shell grep for strings
73 case "$2" in *$1*) return 0;; esac
78 # {{{ reread boot command line; echo last parameter's argument or return false.
80 stringinstring " $1=" /proc/cmdline || return 1
81 result="${/proc/cmdline##*$1=}"
82 result="${result%%[ ]*}"
88 # {{{ check boot commandline for specified option
90 stringinstring " $1" /proc/cmdline
95 # {{{ check whether $1 is yes
97 if [ "$1" = "yes" -o "$1" = "YES" ] ; then
105 # {{{ grml specific checks
107 [ -f /etc/grml_version ] && return 0 || return 1
111 cat /etc/grml_version
115 [ -f /etc/grml_cd ] && return 0 || return 1
119 [ -f /etc/grml_cd ] && return 1 || return 0
123 grep -q small /etc/grml_version 2>/dev/null && return 0 || return 1
127 # {{{ filesystems (proc, pts, sys)
129 check4root || return 1
130 [ -f /proc/version ] || mount -t proc /proc /proc 2>/dev/null
134 check4root || return 1
135 stringinfile "/dev/pts" /proc/mounts || mount -t devpts /dev/pts /dev/pts 2>/dev/null
139 check4root || return 1
140 [ -d /sys/devices ] || mount -t sysfs /sys /sys 2>/dev/null
144 # char *reverse_list(list) {{{
146 # Returns the reversed order of list
151 while [ "$#" -gt 0 ] ; do
152 if [ -z "${ret}" ] ; then
163 # bool is_older_than(reference, files/dirs to check) {{{
165 # return 0 if any of the files/dirs are newer than
168 # EXAMPLE: if is_older_than a.out *.o ; then ...
175 [ "${x}" -nt "${ref}" ] && return 0
177 if [ -d "${x}" ] ; then
178 is_older_than "${ref}" "${x}"/* && return 0
186 # e*() output functions {{{
187 # heavily based on gentoo's functions.sh; stripped down and modified
188 # to match our needs.
203 # copyright 1999-2005 gentoo foundation
204 # distributed under the terms of the gnu general public license v2
205 # $header: /var/cvsroot/gentoo-src/rc-scripts/sbin/functions.sh,v 1.81.2.6 2005/05/15 20:00:31 vapier exp $
210 # dont output to stdout?
213 # default values for e-message indentation and dots
219 # should we use color?
220 if [ -r /proc/cmdline ] ; then
221 grep -q ' nocolor' /proc/cmdline && RC_NOCOLOR='yes'
223 [ -n "$NOCOLORS" ] && RC_NOCOLOR='yes'
224 RC_NOCOLOR="${RC_NOCOLOR:-no}"
226 # Can the terminal handle endcols?
229 # Setup COLS and ENDCOL so eend can line up the [ ok ]
230 # width of [ ok ] == 7
231 COLS="$(stty size 2>/dev/null | cut -d' ' -f2)"
232 if [ -z "${COLS}" ] || [ "${COLS}" -le 0 ] ; then
236 if [ "${RC_ENDCOL}" = "yes" ]; then
237 ENDCOL="
\e[A
\e[$(( ${COLS} - 8 ))G"
242 # Setup the colors so our messages all look pretty
243 if [ "${RC_NOCOLOR}" = "yes" ]; then
244 unset GOOD WARN BAD NORMAL HILITE BRACKET
255 # void esyslog(char* priority, char* tag, char* message)
257 # use the system logger to log a message
263 [ "$#" -le 2 ] && return 0
264 if [ -x /usr/bin/logger ] ; then
269 /usr/bin/logger -p "${pri}" -t "${tag}" -- "$@"
275 # void eindent(int num)
277 # increase the indent used for e-commands.
281 [ "$i" -gt 0 ] || i="${RC_DEFAULT_INDENT}"
282 esetdent $(( ${#RC_INDENTATION} + $i ))
285 # void eoutdent(int num)
287 # decrease the indent used for e-commands.
291 [ "$i" -gt 0 ] || i="${RC_DEFAULT_INDENT}"
292 esetdent $(( ${#RC_INDENTATION} - $i ))
295 # void esetdent(int num)
297 # hard set the indent used for e-commands.
302 [ "$i" -lt 0 ] && i=0
303 RC_INDENTATION="$(printf "%${i}s" '')"
306 # void einfo(char* message)
308 # show an informative message (with a newline)
316 # void einfon(char* message)
318 # show an informative message (without a newline)
321 [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
322 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
323 printf " ${GOOD}*${NORMAL} ${RC_INDENTATION}$*"
328 # void ewarn(char* message)
330 # show a warning message + log it
333 if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
336 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
337 printf " ${WARN}*${NORMAL} ${RC_INDENTATION}$*\n"
340 # Log warnings to system log
341 esyslog "daemon.warning" "rc-scripts" "$@"
347 # void eerror(char* message)
349 # show an error message + log it
352 if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
355 [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
356 printf " ${BAD}*${NORMAL} ${RC_INDENTATION}$*\n"
359 # Log errors to system log
360 esyslog "daemon.err" "rc-scripts" "$@"
366 # void ebegin(char* message)
368 # show a message indicating the start of a process
371 local msg="$@" dots spaces
372 spaces="$(printf '%'"${#RC_DOT_PATTERN}"'s' '')"
373 [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
375 if [ -n "${RC_DOT_PATTERN}" ]; then
376 dots="$(printf "%$(( $COLS - 3 - ${#RC_INDENTATION} - ${#msg} - 7 ))s" '')"
377 while [ "${dots#${spaces}}" != "${dots}" ] ; do
378 dots="${dots#${spaces}}${RC_DOT_PATTERN}"
385 [ "${RC_ENDCOL}" = "yes" ] && echo
387 LAST_E_LEN=$(( 3 + ${#RC_INDENTATION} + ${#msg} ))
392 # void _eend(int error, char *efunc, char* errstr)
394 # indicate the completion of process, called from eend/ewend
395 # if error, show errstr via efunc
397 # This function is private to functions.sh. Do not call it from a
401 local retval="${1:-0}" efunc="${2:-eerror}" msg
404 if [ "${retval}" -eq 0 ]; then
405 [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
406 msg="${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}"
408 if [ "$#" -gt 0 ] ; then
411 msg="${BRACKET}[ ${BAD}!!${BRACKET} ]${NORMAL}"
414 if [ "${RC_ENDCOL}" = "yes" ]; then
415 printf "${ENDCOL} ${msg}\n"
417 [ "${LAST_E_CMD}" = "ebegin" ] || LAST_E_LEN=0
418 printf "%$(( ${COLS} - ${LAST_E_LEN} - 6 ))s%b\n" '' "${msg}"
424 # void eend(int error, char* errstr)
426 # indicate the completion of process
427 # if error, show errstr via eerror
430 local retval="${1:-0}"
433 _eend "${retval}" eerror "$@"
439 # void ewend(int error, char* errstr)
441 # indicate the completion of process
442 # if error, show errstr via ewarn
445 local retval="${1:-0}"
448 _eend "${retval}" ewarn "$@"
455 ## END OF FILE #################################################################
456 # vim:foldmethod=marker tw=80 ai expandtab shiftwidth=2 tabstop=2