SHIFT

--- Sjoerd Hooft's InFormation Technology ---

User Tools

Site Tools


startscript
Differences

This shows you the differences between two versions of the page.

Link to this comparison view

startscript [2013/05/14 20:29] (current)
sjoerd created
Line 1: Line 1:
 += Start Script =
 +
 +This is an overview of variables I use within my scripts. It makes it easier to start. ​
 +
 +<code bash>
 +#!/bin/bash
 +########################################################################################################################​
 +# Author : Sjoerd Hooft
 +# Date Initial Version: 11 July 2011
 +# Comments: sjoerd_ @ _getshifting.com
 +#
 +# Description:​
 +# This is the description for this script.
 +#
 +# Recommendations:​
 +# The script is designed for a 120 column terminal.
 +# The running user must be root.
 +#
 +# Changes:
 +# Please comment on your changes to the script (your name and email address, line number, description):​
 +# DATE - USERNAME - EMAILADDRESS - CHANGE DESCRIPTION
 +########################################################################################################################​
 +
 +# Debug option; uncomment for debugging
 +# set -x
 +
 +# Script Variables
 +HOSTNAME_SHORT=`hostname -s`
 +BASEDIR=`dirname $0`
 +WHATAMI=`basename $0`
 +LOGFILE="​$BASEDIR/​$WHATAMI.log"​
 +DATE=`date +%Y%m%d`
 +# Highlight output on screen
 +BOLD=`tput bold`
 +BOLDOFF=`tput sgr0`
 +# Send all output to logfile; disable if screen output is needed
 +exec > $LOGFILE 2>&1
 + 
 +# Mail Variables and Function
 +MAILTOSUCCESS="​sjoerd_warmetal.nl"​
 +MAILTOFAILURE="​alarm_warmetal.nl"​
 +MAILTOALARM="​alarm_warmetal.nl sjoerd_warmetal.nl"​
 +MAILTOALARMCC="​ccmail1_warmetal.nl ccmail2@warmetal.nl"​
 +MAILSTATUS=success
 +
 +mailFunction() {
 +   if [[ "​$MAILSTATUS"​ -eq "​success"​ ]]; then
 +      cat $LOGFILE | mail -s "​Success $WHATAMI on $HOSTNAME_SHORT at $DATE" $MAILTOSUCCESS
 +   fi
 +   if [[ "​$MAILSTATUS"​ -eq "​failed"​ ]]; then
 +      cat $LOGFILE | mail -s "​Failed $WHATAMI on $HOSTNAME_SHORT at $DATE" $MAILTOFAILURE
 +   fi
 +   if [[ "​$MAILSTATUS"​ -eq "​alarm"​ ]]; then
 +      cat $LOGFILE | mail -s "ALARM FOR $WHATAMI ON $HOSTNAME_SHORT AT $DATE" -c $MAILTOALARMCC $MAILTOALARM
 +   fi
 +}
 +
 +# Start your script here
 +</​code>​
 +
 += Snippet Pause the Script =
 +
 +<code bash>
 +# Function to pause the script
 +# The operator can evaluate the outcome of the previous function
 +scriptContinue () {
 +   if [ "​$AUTOMATIC"​ == "​0"​ ]; then
 +      echo "Press ENTER to continue"​
 +      read CONTINUE
 +      clear
 +   fi
 +}
 +</​code>​
 +
 += Snippet Script Usage =
 +
 +<code bash>
 +# Function to show how to use the script if the operator forgot script arguments
 +usageCommand() {
 +   echo
 +   echo "Usage $0 COMMAND:"​
 +   echo "​--------------------------------------"​
 +   echo "$BOLD $0 <​required arguments>​ $BOLDOFF"​
 +   echo "​--------------------------------------"​
 +   echo
 +   echo argument: syntax
 +   echo
 +}
 +
 +case "​$1"​ in
 +
 +argument )
 +   echo "You choose option $1"
 +   # Start your script here
 +;;
 +
 +* )
 +   ​usageCommand
 +   exit 1
 +;;
 +
 +esac
 +</​code>​
 +
 += Script Variables =
 +
 +* $$ = The PID number of the process executing the shell.
 +* $? = Exit status variable.
 +* $0 = The name of the command you used to call a program.
 +* $1 = The first argument on the command line.
 +* $2 = The second argument on the command line.
 +* $n = The nth argument on the command line.
 +* $* = All the arguments on the command line.
 +* $# = The number of command line arguments.
 +* $@ = All arguments
 +
 +The "​shift"​ command can be used to shift command line arguments to the left, ie $1 becomes the value of $2, $3 shifts into $2, etc. The command, "shift 2" will shift 2 places meaning the new value of $1 will be the old value of $3 and so forth.
 +
 += If possibillities =
 +|[ -a FILE ]|True if FILE exists. |
 +|[ -b FILE ]|True if FILE exists and is a block-special file. |
 +|[ -c FILE ]|True if FILE exists and is a character-special file. |
 +|[ -d FILE ]|True if FILE exists and is a directory. |
 +|[ -e FILE ]|True if FILE exists. |
 +|[ -f FILE ]|True if FILE exists and is a regular file. |
 +|[ -g FILE ]|True if FILE exists and its SGID bit is set. |
 +|[ -h FILE ]|True if FILE exists and is a symbolic link. |
 +|[ -k FILE ]|True if FILE exists and its sticky bit is set. |
 +|[ -p FILE ]|True if FILE exists and is a named pipe (FIFO). |
 +|[ -r FILE ]|True if FILE exists and is readable. |
 +|[ -s FILE ]|True if FILE exists and has a size greater than zero. |
 +|[ -t FD ]|True if file descriptor FD is open and refers to a terminal. |
 +|[ -u FILE ]|True if FILE exists and its SUID (set user ID) bit is set. |
 +|[ -w FILE ]|True if FILE exists and is writable. |
 +|[ -x FILE ]|True if FILE exists and is executable. |
 +|[ -O FILE ]|True if FILE exists and is owned by the effective user ID.|
 +|[ -G FILE ]|True if FILE exists and is owned by the effective group ID. |
 +|[ -L FILE ]|True if FILE exists and is a symbolic link. |
 +|[ -N FILE ]|True if FILE exists and has been modified since it was last read. |
 +|[ -S FILE ]|True if FILE exists and is a socket. |
 +|[ FILE1 -nt FILE2 ]|True if FILE1 has been changed more recently than FILE2, or if FILE1 exists and FILE2 does not. |
 +|[ FILE1 -ot FILE2 ]|True if FILE1 is older than FILE2, or is FILE2 exists and FILE1 does not. |
 +|[ FILE1 -ef FILE2 ]|True if FILE1 and FILE2 refer to the same device and inode numbers. |
 +|[ -o OPTIONNAME ]|True if shell option "​OPTIONNAME"​ is enabled. |
 +|[ -z STRING ]|True of the length if "​STRING"​ is zero. |
 +|[ -n STRING ] or [ STRING ]|True if the length of "​STRING"​ is non-zero. |
 +|[ STRING1 == STRING2 ] |True if the strings are equal. "​="​ may be used instead of "​=="​ for strict POSIX compliance. |
 +|[ STRING1 != STRING2 ] |True if the strings are not equal. |
 +|[ STRING1 < STRING2 ] |True if "​STRING1"​ sorts before "​STRING2"​ lexicographically in the current locale. |
 +|[ STRING1 > STRING2 ] |True if "​STRING1"​ sorts after "​STRING2"​ lexicographically in the current locale. |
 +|[ ARG1 OP ARG2 ]|"​OP"​ is one of -eq, -ne, -lt, -le, -gt or -ge. These arithmetic binary operators return true if "​ARG1"​ is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to "​ARG2",​ respectively. "​ARG1"​ and "​ARG2"​ are integers. |
 +|[ ! EXPR ]|True if EXPR is false. |
 +|[ ( EXPR ) ]|Returns the value of EXPR. This may be used to override the normal precedence of operators.|
 +|[ EXPR1 -a EXPR2 ]|True if both EXPR1 and EXPR2 are true. |
 +|[ EXPR1 -o EXPR2 ]|True if either EXPR1 or EXPR2 is true. |
 +|command |True if command returns a 0 (zero). |
 +
 +{{tag>​scripts notes}}
  
startscript.txt ยท Last modified: 2013/05/14 20:29 by sjoerd