Allodoxaphobia <
knock_you...@example.net> wrote
in <
slrnp6bnsi.2qun.k...@vps.jonz.net>:
# On Sat, 20 Jan 2018 21:13:14 +0000 (UTC), Mike Sanders wrote:
#> Here's to hoping this post manages a reply or two...
#>
#> Do any of you have any 'funky looking' scripts,
#> that despite it all, you still consider handy?
#> Curious to see what the gurus have cooked up.
#
# I have a need" in some of my scripts to detect whether it's running on
# linux or FreeBSD. My ugly hack is to check for my workstation's userid
# -- the FreeBSD environ (either my VPS or my web server) being different.
The following is the last .profile you'll ever need. It tests for your OS,
shell and hostname and sources the correct files for all permutations.
It has neat separate files for aliases, environment variables, functions
and "rc" code so you always know what file to edit.
You want less instead of more everywhere?
That would go to .aliases.
A zsh completion function that returns a list of installed FreeBSD ports?
That would go to .functions.FreeBSD.zsh.
Setting PATH on your hal9000 box?
That's in .envars.hal9000.
Here is that beast. Enjoy. Hack it. Copy it. Don't sue me.
Jens
# $Id: .profile,v 1.23 2017/12/08 21:10:24 schweikh Exp schweikh $
#
# Must be written using only portable Bourne shell syntax and commands.
# This is why I use "set X" instead of "set --" and `foo` instead of $(foo).
# Leave shell specific customization to appropriate rc files.
#
# .profile is sourced at login by sh and ksh. The zsh sources .zshrc and
# bash sources .bashrc. To get the same behaviour from zsh and bash as well
# I suggest "cd; ln -s .profile .zshrc; ln -s .profile .bashrc".
# Preliminary PATH with programs needed during startup; basically uname.
PATH="/bin:/usr/bin"
# A literal ASCII ESC.
export ESC=' '
# A literal ASCII newline.
export NL="
"
# Control Sequence Introducer.
export CSI="${ESC}["
export reset="${CSI}0m"
export emphasis="${CSI}1m"
export fgBlack="${CSI}30m" bgBlack="${CSI}40m"
export fgRed="${CSI}31m" bgRed="${CSI}41m"
export fgGreen="${CSI}32m" bgGreen="${CSI}42m"
export fgYellow="${CSI}33m" bgYellow="${CSI}43m"
export fgBlue="${CSI}34m" bgBlue="${CSI}44m"
export fgMagenta="${CSI}35m" bgMagenta="${CSI}45m"
export fgCyan="${CSI}36m" bgCyan="${CSI}46m"
export fgWhite="${CSI}37m" bgWhite="${CSI}47m"
export fgDefault="${CSI}39m" bgDefault="${CSI}49m"
echo "$fgWhite~$LOGNAME/.profile$reset" # Announce who is responsible.
# Determine the unqualified hostname.
# shellcheck disable=2086 disable=2121 disable=2039
set X $HOST $HOSTNAME
case $# in
(1)
# shellcheck disable=2121 disable=2046 disable=2006
set X `uname -n 2>/dev/null` `hostname 2>/dev/null` localhost
esac
PROFILE_HOST=${2%%.*}
# Determine what (Bourne compatible) shell we are running under. Put the result
# in $PROFILE_SHELL (not $SHELL) so further code can depend on the shell type.
if test -n "$ZSH_VERSION"; then
PROFILE_SHELL=zsh
elif test -n "$BASH_VERSION"; then
PROFILE_SHELL=bash
elif test -n "$KSH_VERSION"; then
PROFILE_SHELL=ksh
elif test -n "$FCEDIT"; then
PROFILE_SHELL=ksh
elif test -n "$PS3"; then
PROFILE_SHELL=unknown
else
PROFILE_SHELL=sh
fi
# Set PROFILE_OS to a string describing the operating system type.
# The possible PROFILE_OS strings are arbitrary; they are used to
# dispatch to OS specific startup code below. Modify to your liking.
# shellcheck disable=2006
uname="`uname -sr 2>/dev/null`"
case $uname in
(FreeBSD*) PROFILE_OS=FreeBSD;;
(NetBSD*) PROFILE_OS=NetBSD;;
(OpenBSD*) PROFILE_OS=OpenBSD;;
(SunOS\ 4*) PROFILE_OS=SunOS;;
(SunOS\ 5*) PROFILE_OS=Solaris;;
(IRIX*) PROFILE_OS=IRIX;;
(HP*) PROFILE_OS=HP-UX;;
(Linux*) PROFILE_OS=Linux;;
(CYGWIN*) PROFILE_OS=Cygwin;;
(*) PROFILE_OS=generic
echo "warning: cannot map \"$uname\" to an OS string,"
echo "assuming $PROFILE_OS. Edit ~/.profile if this is wrong."
esac
if test "x$HOME" = x; then
HOME=/root # FreeBSD: in single user mode HOME is not set.
export HOME # This makes the /bin/sh read the files in /root.
fi
cd || exit
case $PROFILE_SHELL in
(bash)
echo "This appears to be a$fgCyan Bourne Again Shell (bash)$fgDefault."
;;
(ksh)
echo "This appears to be a$fgCyan Korn Shell (ksh)$fgDefault."
;;
(sh)
echo "This appears to be a$fgCyan Bourne Shell (sh)$fgDefault."
;;
(zsh)
echo "This appears to be a$fgCyan Z-Shell (zsh)$fgDefault."
setopt shwordsplit
;;
(*)
echo "Please add an entry for $PROFILE_SHELL in $HOME/.profile"
esac
#┌─────────────────────────────────────────────────────────────────────────────┐
#│ Set the sequence of initialization: │
#│ The functions are first so that they are usable by other dot files; │
#│ functions, envars and aliases should not produce any output. Commands │
#│ producing output should be executed in rc files. │
#└─────────────────────────────────────────────────────────────────────────────┘
for startup in functions envars aliases rc; do
case $startup in
(rc) say=":";; # Do not announce rc processing (only messes up output).
(*) say="echo"
esac
$say "$fgGreen* Setting up $startup$fgDefault:"
if test -r .$startup; then
$say " - universal (.$startup)"
# shellcheck disable=1090
. "./.$startup"
fi
for variant in \
$PROFILE_OS \
$PROFILE_HOST \
$PROFILE_SHELL \
$PROFILE_OS.$PROFILE_HOST \
$PROFILE_OS.$PROFILE_SHELL \
$PROFILE_HOST.$PROFILE_SHELL \
$PROFILE_OS.$PROFILE_HOST.$PROFILE_SHELL; do
if test -r ".$startup.$variant"; then
$say " - $variant specific (.$startup.$variant)"
# shellcheck disable=1090
. "./.$startup.$variant"
fi
done
done
unset say startup uname variant
export PROFILE_OS PROFILE_HOST PROFILE_SHELL
# vim: syntax=sh tabstop=2 shiftwidth=2 expandtab fileformat=unix