BASH_SYSTEM
_arch()
{ #check system arch, return 64|32 (32 by default)
if [ -z "${MACHTYPE}" ]; then
_arch__arch="$(uname -m)"
else
_arch__arch="$(printf "%s" "${MACHTYPE}" | cut -d- -f1)"
fi
case "${_arch__arch}" in
x86_64) _arch__arch="64" ;;
*) _arch__arch="32" ;;
esac
printf "%s" "${_arch__arch}"
}
_arch
1. arch() : Find out system architecture (32/64)
52 _aptproxy() { #test if an apt proxy exist on the local network
53 avahi-browse -a -t | grep apt-cacher-ng >/dev/null && return 0
54 return 1
55 }
56 #==================================================== Example ====
57 #if ! _aptproxy; then
58 #printf "%s\\n" "no apt proxy was found, setting up one ..."
59 #fi
60 #=================================================================
61
128 _daemonize() { #daemonize an external command
129 #http://blog.n01se.net/blog-n01se-net-p-145.html
130 [ -z "${1}" ] && return 1
131 ( # 1. fork, to guarantee the child is not a process
132 # group leader, necessary for setsid) and have the
133 # parent exit (to allow control to return to the shell)
134
135 # 2. redirect stdin/stdout/stderr before running child
136 [ -t 0 ] && exec </dev/null
137 [ -t 1 ] && exec >/dev/null
138 [ -t 2 ] && exec 2>/dev/null
139 if ! command -v "setsid" >/dev/null 2>&1; then
140 # 2.1 guard against HUP and INT (in child)
141 trap '' 1 2
142 fi
143
144 # 3. ensure cwd isn't a mounted fs so it does't block
145 # umount invocations
146 cd /
147
148 # 4. umask (leave this to caller)
149 # umask 0
150
151 # 5. close unneeded fds
152 #XCU 2.7 Redirection says: open files are represented by
153 #decimal numbers starting with zero. The largest possible
154 #value is implementation-defined; however, all
155 #implementations shall support at least 0 to 9, inclusive,
156 #for use by the application.
157 i=3; while [ "${i}" -le "9" ]; do
158 eval "exec ${i}>&-"
159 i="$(($i + 1))"
160 done
161
162 # 6. create new session, so the child has no
163 # controlling terminal, this prevents the child from
164 # accesing a terminal (using /dev/tty) and getting
165 # signals from the controlling terminal (e.g. HUP, INT)
166 if command -v "setsid" >/dev/null 2>&1; then
167 exec setsid "$@"
168 elif command -v "nohup" >/dev/null 2>&1; then
169 exec nohup "$@" >/dev/null 2>&1
170 else
171 if [ ! -f "${1}" ]; then
172 "$@"
173 else
174 exec "$@"
175 fi
176 fi
177 ) &
178 # 2.2 guard against HUP (in parent)
179 if ! command -v "setsid" >/dev/null 2>&1 \ &&
180 ! command -v "nohup" >/dev/null 2>&1; then
181 disown -h "${!}"
182 fi
183 }
184 #==================================================== Example ====
185 #_daemonize /bin/sh /path/script #daemonize script
186 #kill -15 $(cat /var/run/script.pid #finish daemon
187 #=================================================================
188
281 _distro() { #return distro name in lower string
282 _distro__DIST_INFO="/etc/lsb-release"
283 if [ -r "${_distro__DIST_INFO}" ]; then
284 . "${_distro__DIST_INFO}"
285 fi
286
287 if [ -z "${DISTRIB_ID}" ]; then
288 _distro__DISTRIB_ID="Unknown";
289 if [ -f /etc/arch-release ]; then
290 _distro__DISTRIB_ID="Arch"
291 elif [ -r /etc/knoppix-version ]; then
292 _distro__DISTRIB_ID="Knoppix"
293 elif [ -r /etc/sidux-version ]; then
294 _distro__DISTRIB_ID="Sidux"
295 elif [ -r /etc/debian_version ]; then
296 _distro__DISTRIB_ID="Debian"
297 elif [ -r /etc/issue ]; then
298 _distro__DISTRIB_ID="$(awk '{print $1}' /etc/issue.net)"
299 if [ X"${_distro__DISTRIB_ID}" = X"Ubuntu" ]; then
300 _distro__DISTRIB_ID="Ubuntu"
301 fi
302 elif [ -r /etc/gentoo-release ]; then
303 _distro__DISTRIB_ID="Gentoo"
304 elif [ -f /etc/lfs-version ]; then
305 _distro__DISTRIB_ID="LFS"
306 elif [ -r /etc/pclinuxos-release ]; then
307 _distro__DISTRIB_ID="PCLinuxOS"
308 elif [ -f /etc/mandriva-release ] || [ -f /etc/mandrake-release ]; then
309 _distro__DISTRIB_ID="Mandriva"
310 elif [ -f /etc/redhat-release ]; then
311 _distro__DISTRIB_ID="RedHat"
312 elif [ -f /etc/fedora-release ]; then
313 _distro__DISTRIB_ID="Fedora"
314 elif [ -r /etc/vector-version ]; then
315 _distro__DISTRIB_ID="VectorLinux"
316 elif [ -r /etc/slackware-version ]; then
317 _distro__DISTRIB_ID="$(cat /etc/slackware-version)"
318 elif [ -f /etc/release ]; then
319 _distro__DISTRIB_ID="Solaris"
320 elif [ -r /etc/SuSE-release ]; then
321 _distro__DISTRIB_ID="$(grep -i suse /etc/SuSE-release)"
322 elif [ -f /etc/yellowdog-release ]; then
323 _distro__DISTRIB_ID="YellowDog Linux"
324 elif [ -f /etc/zenwalk-version ]; then
325 _distro__DISTRIB_ID="Zenwalk"
326 fi
327 printf "%s\\n" "${_distro__DISTRIB_ID}" | \
328 tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'
329 else
330 printf "%s\\n" "${DISTRIB_ID}" | \
331 tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'
332 fi
333 }
334 #==================================================== Example ====
335 #distro="$(_distro)"
336 #=================================================================
337
501 _ensurecron() { #adds cron job, returns 1 on error
502 [ -z "${1}" ] && return 1
503 _ensurecron__exist="$(crontab -l 2>/dev/null | awk -v p="${1}" '{ if ($0 == p) {print p}}')"
504 [ -n "${_ensurecron__exist}" ] && return 0
505 ( crontab -l 2>/dev/null; printf "%s\\n" "${1}" ) | crontab -
506 }
507 #==================================================== Example ====
508 #_ensurecron "* * * * * /path/task"
509 #=================================================================
510
558 _getfs() { #returns file system detected or 1 on failure
559 [ -z "${1}" ] && return 1
560 udevadm info -q all -n "${1}" | awk -F"=" '/ID_FS_TYPE/ {print $2 }'
561 }
562 #==================================================== Example ====
563 #_getfs /dev/sda1 #print "ext4" or the format found
564 #=================================================================
565
597 _internet() {
598 #check for internet connection, return 0 on success, 1 otherwise
599 wget --tries=3 --timeout=5 http://www.google.com -O /tmp/index.google > /dev/null 2>&1
600 if [ -s /tmp/index.google ]; then
601 rm -rf /tmp/index.google
602 return 0
603 else
604 rm -rf /tmp/index.google
605 return 1
606 fi
607 }
608 #==================================================== Example ====
609 #if ! _internet; then
610 #printf "%s\\n" \
611 #"You must have an Internet connection to run this script"
612 #exit 1
613 #fi
614 #=================================================================
615
644 _livecd() {
645 #detect a livecd system, return 0 on success, 1 otherwise
646 grep boot=live /proc/cmdline >/dev/null && return 0
647 grep boot=casper /proc/cmdline >/dev/null && return 0
648 return 1
649 }
650 #==================================================== Example ====
651 #if _livecd ; then
652 #printf "%s\\n" "Live cd detected"
653 #else
654 #printf "%s\\n" "Non LiveCd detected"
655 #fi
656 #=================================================================
657
658 _lock() {
659 #locks the execution of a program, it should be used at the
660 #beggining of it, exit on failure
661 exec 9>/tmp/"$(expr "${1}" : '.*/\([^/]*\)')".lock #verify only one instance is running
662 [ X"${LOGNAME}" = X"root" ] && chmod 666 /tmp/"$(expr "${1}" : '.*/\([^/]*\)')".lock
663 if ! flock -n 9 ; then #http://mywiki.wooledge.org/BashFAQ/045
664 printf "%s\\n" "$(expr "${1}" : '.*/\([^/]*\)'): another instance is running";
665 exit 1
666 fi
667 }
668 #==================================================== Example ====
669 #_lock "${0}" #locks script till finish
670 #=================================================================
671
704 _notify() {
705 #output messages through the notification system, prints to
706 #stdout in the worst escenario
707 [ -z "${1}" ] && return 1
708 if [ X"${TERM}" = X"linux" ] || [ -z "${TERM}" ]; then
709 kill -9 $(pgrep notify-osd) >/dev/null 2>&1
710 if ! DISPLAY=${DISPLAY:-:0} notify-send -t 1000 "${1}" "${2}"; then
711 if command -v "gxmessage" 2>/dev/null; then
712 font="Monaco 9"
713 DISPLAY=${DISPLAY:-:0} gxmessage "${font:+-fn "$font"}" "${1} ${2}" "ok"
714 elif command -v "xmessage" 2>/dev/null; then
715 font="fixed"
716 DISPLAY=${DISPLAY:-:0} xmessage "${font:+-fn "$font"}" "${1} ${2}" "ok"
717 fi
718 fi
719 else
720 if [ -z "${2}" ]; then
721 printf "%s\\n" "${1}"
722 else
723 printf "%s: %s\\n" "${1}" "${2}"
724 fi
725 fi
726 }
727 #==================================================== Example ====
728 #_notify "You need html2text" "$ sudo apt-get install html2text"
729 #=================================================================
730
731 _notify2() {
732 #output messages through the notification system, prints to
733 #stdout in the worst escenario
734 [ -z "${1}" ] && return 1
735 if [ X"${TERM}" = X"linux" ] || [ -z "${TERM}" ]; then
736 kill -9 $(pgrep notify-osd) >/dev/null 2>&1
737 if ! DISPLAY=${DISPLAY:-:0} notify-send -t 1000 "${1}" "${3}"; then
738 if command -v "gxmessage" 2>/dev/null; then
739 font="Monaco 9"
740 DISPLAY=${DISPLAY:-:0} gxmessage "${font:+-fn "$font"}" "${1}" "ok"
741 elif command -v "xmessage" 2>/dev/null; then
742 font="fixed"
743 DISPLAY=${DISPLAY:-:0} xmessage "${font:+-fn "$font"}" "${1}" "ok"
744 fi
745 fi
746 else
747 printf "%s\\n" "${2}"
748 fi
749 case "${3}" in
750 *clipboard*)
751 #dumping xclip's stdout to the bitbucket works around xclip's
752 #failure to properly daemonize
753 #https://sourceforge.net/p/xclip/patches/9/
754 printf "%s" "${2}" 2>/dev/null | xclip -selection clipboard -i >/dev/null 2>&1 || \
755 printf "%s" "${2}" 2>/dev/null | xsel -bi 2>/dev/null || \
756 printf "%s" "${2}" 2>/dev/null | pbcopy 2>/dev/null || :
757 ;;
758 esac
759 }
760 #==================================================== Example ====
761 #_notify2 "5+3 = 8" "8" "Copied to the clipboard!"
762 #=================================================================
763
1247 _user() {
1248 #test if an user exists on the system
1249 #return 0 on sucess, 1 otherwise
1250 [ -z "${1}" ] && return 1
1251 if id -u "${1}" >/dev/null 2>&1; then
1252 return 0
1253 else
1254 return 1
1255 fi
1256 }
1257 #==================================================== Example ====
1258 #if _user user; then
1259 #printf "%s\\n" "user exists"
1260 #fi
1261 #=================================================================
1262
939 _rmcron() { #remove cron jobs, returns 1 on failure
940 [ -z "${1}" ] && return 1
941 crontab -l | grep -v "${1}" | crontab
942 }
943 #==================================================== Example ====
944 #_rmcron "pattern"
945 #=================================================================
946