Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Create a different colored prompt for each users and hosts

I've just coded this script to avoid mistakes when you're using more than one host and user login :

#!/usr/bin/ksh
#######################################
# Program : Prompt-Color
#
# Version : V1.0
# Date : 2009-03-02
# Author : N. PENE
#
# Inputs : none
#######################################
hostname=`hostname`
username=`whoami`

#-- classic colors vars :
BLACK='\[\033[0;30m\]'
RED='\[\033[0;31m\]'
GREEN='\[\033[0;32m\]'
BROWN='\[\033[0;33m\]'
BLUE='\[\033[0;34m\]'
PURPLE='\[\033[0;35m\]'
CYAN='\[\033[0;36m\]'
GRAY='\[\033[0;37m\]'
DEFAULT='\[\033[0m\]'
#-- bolded colors vars :
BBLACK='\[\033[1;30m\]'
BRED='\[\033[1;31m\]'
BGREEN='\[\033[1;32m\]'
BBROWN='\[\033[1;33m\]'
BBLUE='\[\033[1;34m\]'
BPURPLE='\[\033[1;35m\]'
BCYAN='\[\033[1;36m\]'
BGRAY='\[\033[1;37m\]'
BDEFAULT='\[\033[1;0m\]'


cprompt=$DEFAULT
cborder=$DEFAULT
cpath=$DEFAULT

#-- username color :
case $username in
"npene" ) cuser=$CYAN;;
"donunix" ) cuser=$BLUE;;
"fenice" ) cuser=$PURPLE;;
"donpene" ) cuser=$BROWN;;
"root" ) cuser=$GREEN;;
esac

#-- hostname color :
case $hostname in
"host1" ) chost=$BRED;;
"host2" ) chost=$BBLUE;;
esac

PS1="$cborder[$cuser\u$cborder@$chost\h$cborder: $cpath\W$cborder] $cprompt"
export PS1

Script : keep your connection on a remote shell

A script to launch if you doesn't want to be disconnected by timeout on your remote ssh connection :

#!/usr/bin/ksh
#####################################################################
# Program : Keep-Hand
#
# Version : V2.0
# Date : 2009-02-11
# Author : N. PENE
#
# Inputs : none
#####################################################################


while [ 1 -eq 1 ] ; do

#-- check if the code is attached to an xterm :
term=`ps -p $$ | cut -d " " -f 2`
if [ $term == "?" ] ; then
exit
fi

#-- wait 4min :
sleep 240
echo -e ".\c"
done

[Shell-Script] create a lock system for your code

Here is the code for making a lock system in your shell-script program.
The main utility of the lock system is to be sure to not running your code while another process of the same code is running.

The main principe is to create a lock file when the code is running, and check the existance of this file each time you're launching it.

NB : the variable $exec is the name of the script you want to execute.


#-- Parameters:
exec="./my-script.sh"
NAME="my-script"
LOCK="/tmp/${NAME}.lock"
out="/tmp/${NAME}-$(/bin/date '+%Y%m%d').out"

#-- Error Function:
error () {
echo "$1" 1>&2
exit 1
}

#-------------------------
# Launch the code
#-------------------------
if [ -f "$LOCK" ]
then
if [ "$(ps -p `cat $LOCK` | wc -l)" -gt 1 ]; then
#-- process is still running
echo "Quit at start: lingering process `cat $LOCK`"
error "Daemon already running (not launched)!"
exit 10
else
#-- process not running, but lock file not deleted
echo "process not running, but lock file not deleted"
rm $LOCK
echo $$ >> "$LOCK"
fi
else
echo "create lock"
echo $$ >> "$LOCK"
echo "done (pid=$$)"
# launch the code:
ksh ${exec} >> $out 2>&1

fi

rm $LOCK


Dynamic prompt line

Two lines to put on your .bashrc file to have a nice dynamic prompt line.

export MAX_PATH_LENGTH=25
export PROMPT_COMMAND='PS1="\\[\\033[32m\\][\u@\h:\`if [[ \$? = "0" ]]; then echo "\\[\\033[35m\\]"; else echo "\\[\\033[31m\\]"; fi\` \`if [[ `pwd|wc -c|tr -d " "` > $MAX_PATH_LENGTH ]]; then echo "\\W"; else echo "\\w"; fi\`\\[\\033[32m\\]]\$\[\033[0m\] "; echo -ne "\033]0;`hostname -s`:`pwd`\007"'

NB : The color of the path change if the last command written is unknown or wrong, and shortens for long directories path.

[Shell-Script] Loops for each element of a tabular

Because, I have some difficulties to record this information in my mind :)
In this code we create a tabular and then do a loops on each element to print the result:

tab[0]="Don"
tab[1]="Unix"
tab[2]="Unix Tips n Tricks"

for var in ${tab[@]}; do
echo $var
done

a shell-script template

#!/bin/bash
#####################################################################
# Program :
#
# Version :
# Date :
# Author :
#
# Inputs :
#
# Notes :
#
#
#####################################################################

prg=$0

#--------------------------------------------------------------------
# Setup Global Variables
#--------------------------------------------------------------------


#--------------------------------------------------------------------
# Test for prerequisites
#--------------------------------------------------------------------

#--------------------------------------------------------------------
# Do something
#--------------------------------------------------------------------