initial checkin
[grml-scripts.git] / 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 # Latest change: Sam Sep 16 13:08:16 CEST 2006 [mika]
7 ################################################################################
8
9 # {{{ set default PATH
10 setpath(){
11   export PATH=${PATH:-'/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin:/usr/local/bin'}
12 }
13 # }}}
14
15 # {{{ check for root-permissions
16 check4root(){
17   if [ "$UID" != 0 ] ; then
18     echo 1>&2 "Error: please run this script with uid 0 (root)." ; return 1
19   fi
20 }
21 # }}}
22
23 # {{{ check for user permissions
24 check4user(){
25   if [ "$UID" == 0 ] ; then
26     echo 1>&2 "Error: please do not run this script with uid 0 (root)." ; return 1
27   fi
28 }
29 # }}}
30
31 # {{{ check for running zsh
32 iszsh(){
33   if ! [ -z "$ZSH_VERSION" ] ; then
34     return 0
35   else
36     return 1
37   fi
38 }
39 # }}}
40
41 # {{{ check for (X)dialog
42 setdialog(){
43   if [ -n "$DISPLAY" ] ; then
44      [ -x /usr/bin/Xdialog ] && DIALOG="Xdialog" && export XDIALOG_HIGH_DIALOG_COMPAT=1
45   else
46      [ -x /usr/bin/dialog ] && DIALOG='dialog' || ( echo 1>&2 "dialog not available" ; return 1 )
47   fi
48 }
49 # }}}
50
51 # {{{ check for availability of program(s)
52 check4progs(){
53   local RC=''
54   for arg in $* ; do
55     type -p $arg >/dev/null 2>&1 || RC="$arg"
56   done
57   if [ -n "$RC" ] ; then
58      echo "$RC not installed"
59      return 1
60   fi
61 }
62 # }}}
63
64 # {{{ simple shell grep
65 stringinfile(){
66   case "$(cat $2)" in *$1*) return 0;; esac
67   return 1
68 }
69 # }}}
70
71 # {{{ simple shell grep for strings
72 stringinstring(){
73   case "$2" in *$1*) return 0;; esac
74   return 1
75 }
76 # }}}
77
78 # {{{ reread boot command line; echo last parameter's argument or return false.
79 getbootparam(){
80   stringinstring " $1=" /proc/cmdline || return 1
81   result="${/proc/cmdline##*$1=}"
82   result="${result%%[   ]*}"
83   echo "$result"
84   return 0
85 }
86 # }}}
87
88 # {{{ check boot commandline for specified option
89 checkbootparam(){
90   stringinstring " $1" /proc/cmdline
91   return "$?"
92 }
93 # }}}
94
95 # {{{ check whether $1 is yes
96 checkvalue(){
97   if [ "$1" = "yes" -o "$1" = "YES" ] ; then
98     return 0
99   else
100     return 1
101   fi
102 }
103 # }}}
104
105 # {{{ grml specific checks
106 isgrml(){
107   [ -f /etc/grml_version ] && return 0 || return 1
108 }
109
110 grmlversion(){
111  cat /etc/grml_version
112 }
113
114 isgrmlcd(){
115   [ -f /etc/grml_cd ] && return 0 || return 1
116 }
117
118 isgrmlhd(){
119   [ -f /etc/grml_cd ] && return 1 || return 0
120 }
121
122 checkgrmlsmall(){
123   grep -q small /etc/grml_version 2>/dev/null && return 0 || return 1
124 }
125 # }}}
126
127 # {{{ filesystems (proc, pts, sys)
128 mount_proc(){
129   check4root || return 1
130   [ -f /proc/version ] || mount -t proc /proc /proc 2>/dev/null
131 }
132
133 mount_pts(){
134   check4root || return 1
135   stringinfile "/dev/pts" /proc/mounts || mount -t devpts /dev/pts /dev/pts 2>/dev/null
136 }
137
138 mount_sys(){
139   check4root || return 1
140   [ -d /sys/devices ] || mount -t sysfs /sys /sys 2>/dev/null
141 }
142 # }}}
143
144 ## END OF FILE #################################################################
145 # vim:foldmethod=marker tw=80 ai expandtab shiftwidth=2 tabstop=2