#!/bin/bash ############################################################################### # # File: $RCSfile: functions,v $ # Revision: $Revision: 1.20 $ # Description: Common bash functions # Author: Andrew@DeFaria.com # Created: Thu Jun 6 08:31:57 PDT 1996 # Modified: $Date: 2013/03/26 20:38:23 $ # Language: bash # # (c) Copyright 2000-2005, Andrew@DeFaria.com, all rights reserved. # ############################################################################### ESC=$(echo "\033") CR=$(echo "\015") export WIZARD="Wizard" view_name= # Function to set the title bar. Works on the terminal emulators listed. function title_bar { if [ $# -gt 1 ]; then ROOT=shift ROOT="${NORMAL}\($ROOT\)" fi prefix="$@" # Change $HOME -> ~ if [ "${PWD#$HOME*}" != "$PWD" ]; then current_dir="~${PWD#$HOME*}" elif [ "$PWD" = "$HOME" ]; then current_dir=~ else current_dir="$PWD" fi # Remove view name current_dir="${current_dir#/view/$view_name*}" current_dir="${current_dir#/sview/$view_name*}" # Add CVS/Root if there is one if [ -f "CVS/Root" ]; then current_dir="$current_dir ($(cat CVS/Root | tr -d $CR))" fi # Add GIT info if available if in_git_repo; then current_dir="$current_dir [git: $(git branch | sed -n -e 's/^\* \(.*\)/\1/p')]" fi if [ "$TERM" = "hpterm" -o \ "$TERM" = "hp" -o \ "$TERM" = "2392A" ]; then string=$(echo "${SYSNAME##*:}:$@") echo -n "${ESC}&f0k${#string}D$string" elif [ "$TERM" = "dtterm" -o \ "$TERM" = "vt221" ]; then string=$(echo "${SYSNAME##*:}:$@") echo -n "${ESC}]2;$string\007" elif [ "$TERM" = "cygwin" -o \ "$TERM" = "vt100" -o \ "$TERM" = "xterm" -o \ "$TERM" = "xtermc" -o \ "$TERM" = "xterm-256color" ]; then PS1="\[\e]0;$prefix$current_dir\007\]$ROOT\[$B_YELLOW\]$SYSNAME:\[$B_WHITE\]" fi } # title_bar # Function to set the icon name. Works on the terminal emulators listed. function icon_name { if [ "$TERM" = "hpterm" -o \ "$TERM" = "hp" -o \ "$TERM" = "2392A" ]; then string=$(echo "$1") echo -n "${ESC}&f-1k${#string}D$string" elif [ "$TERM" = "dtterm" -o \ "$TERM" = "vt100" -a "$DTTERM" = "True" ]; then # Note setting icon_name on vt100 overwrites the title bar so skip it echo -n "${ESC}]1;$@\007" fi } # icon_name # Sets both the title bar and the icon name. function title { title_bar "$@" icon_name "${SYSNAME##*:}" } # title # Sets title bar to machine name and path. Will include a view name if in a # view and a string to indicate that you are root. function set_title { if [ $($id -u) -eq 0 ]; then root="$WIZARD " else root= fi view_name=$(scm pwv -short 2> /dev/null); if [ $? -ne 0 -o -z "$view_name" ]; then view_name='*NONE*' fi if [[ $view_name = *NONE* ]]; then view_name="" title_bar "$root" else title_bar "${root}View: $view_name: " fi icon_name "${SYSNAME##*:}" } # set_title # Sets prompt on terminals listed. function set_prompt { if [ $($id -u) -eq 0 ]; then if [ "$TERM" = "hpterm" -o \ "$TERM" = "hp" -o \ "$TERM" = "2392A" -o \ "$TERM" = "dtterm" -o \ "$TERM" = "vt100" -o \ "$TERM" = "cygwin" -o \ "$TERM" = "xterm" -o \ "$TERM" = "xtermc" -o \ "$TERM" = "xterm-256color" -o \ "$TERM" = "vt220" ]; then ROOT="\[${ROOT_COLOR}\]$WIZARD\[$NORMAL\] " else ROOT="$WIZARD " fi else ROOT="" fi if [ "$TERM" = "vt100" -o \ "$TERM" = "cygwin" -o \ "$TERM" = "xterm" -o \ "$TERM" = "xtermc" -o \ "$TERM" = "xterm-256color" -o \ "$TERM" = "vt220" ]; then PS1="$ROOT\[$B_YELLOW\]$SYSNAME:\[$B_WHITE\]" else PS1="$ROOT$SYSNAME:" fi set_title } # set_prompt # Function to override the normal cd command, setting title and prompt. function mycd { if [ -z "$1" ]; then \cd ~ else \cd "$1" fi set_title set_prompt } # mycd export mycd # Functions to override the normal push/popd commands, setting title and prompt. function mypushd { if [ -z "$1" ]; then \pushd > /dev/null else \pushd "$1" > /dev/null fi set_title set_prompt } # mypushd function mypopd { if [ -z "$1" ]; then cd - > /dev/null else \popd "$1" > /dev/null fi set_title set_prompt } # mypopd # Function to override rlogin. Note that it fixes up the title and prompt # upon return. function rlogin { /usr/bin/rlogin "$@" set_title set_prompt } # rlogin # Function to override rsh. Note that it fixes up the title and prompt # upon return. function rsh { /usr/bin/rsh "$@" set_title set_prompt } # rsh # Function to override ssh. Note that it fixes up the title and prompt # upon return. function ssh { /usr/bin/ssh "$@" set_title set_prompt } # ssh function processRunning { if [ $ARCHITECTURE = "FreeBSD" ]; then psopts="-aux" else psopts="-ef" fi if [ $1 != '' ]; then return $(ps $psopts | grep "$1" | grep -v "grep $1" | grep -v "grep -d skip" | wc -l) fi } # processRunning function sj { if [ $ARCHITECTURE = "FreeBSD" ]; then psopts="-aux" else psopts="-ef" fi if [ $# = 0 ]; then ps $psopts | $PAGER else for str; do ps $psopts | grep "$str" | grep -ve "grep $str" -e "grep -d skip" -e "grep --color=auto" done fi } # sj function start_imap { # Starts an ssh tunnel for IMAP ssh -C -L 143:defaria.com:143 andrew@defaria.com } # start_imap function cmdline { # Displays the command line from the /proc filesystem (if present) me=$0; if [ $# -ne 1 ]; then error "Usage: cmdline " return 1 fi pid=$1; if [ ! -d "/proc" ]; then error "This OS has no /proc filesystem" return 1 fi if [ ! -d "/proc/$pid" ]; then error "PID $pid does not exist" return 1 fi if [ ! -f "/proc/$pid/cmdline" ]; then error "PID $pid has no cmdline!" return 1 fi cat /proc/$pid/cmdline | tr -c [:print:] " " display } # cmdline function user { processRunning ypbind; ypbind=$? processRunning winbind; winbind=$? if [ $# -gt 0 ]; then if [ $ypbind -gt 0 ]; then ypcat passwd | grep -i $@ elif [ $winbind -gt 0 ]; then for user in $(wbinfo -u | grep -i $@); do wbinfo --user-info $user done fi else if [ $ypbind -gt 0 ]; then ypcat passwd | $PAGER elif [ $wbind -gt 0 ]; then for user in $(wbinfo -u); do wbinfo --user-info $user done | $PAGER fi fi } # user function group { processRunning ypbind; ypbind=$? processRunning winbind; winbind=$? if [ $# -gt 0 ]; then if [ $ypbind -gt 0 ]; then ypcat group | grep -i $@ elif [ $winbind -gt 0 ]; then for group in $(wbinfo -g | grep -i $@); do wbinfo --group-info $group done fi else if [ $ypbind -gt 0 ]; then ypcat group | $PAGER elif [ $winbind -gt 0 ]; then for group in $(wbinfo -g); do wbinfo --group-info $group done | $PAGER fi fi } # group