Search This Blog

Thursday, March 3, 2011

Summary shell script for DBA

Arithmetic Operators
 Operator  Description
 -eq  True if two integers are equal
 -ne  True if two integers are not equal
 -lt  True if operand1 is less than operand2
 -le  True if operand1 is less than or equal to operand2
 -gt  True if operand1 is greater than operand2
 -ge  True if operand1 is greater than or equal to operand2

String Operators
 String Operator  Description
 -z string  True if the string is empty
 -n string  True if the string is not empty
 string1 = string2  True if the strings are equal
 string1 != string2  True if the strings are not equa
 string1 < string2  True if string1 sorts before string2
 string1 > string2  True if string1 sorts after string2

Files Operators
 File Operator  Description
 -a  True if file exists
 -b  True if file is a block device file
 -c  True if file is a character device file
 -d  True if file is a directory
 -e  True if file exists
 -f  True if file exists and is a regular file
 -g  True if file has set-group-id permission set
 -h  True if file is a symbolic link
 -L  True if file is a symbolic link
 -k  True if file’s sticky bit is set
 -p  True if file is a named pipe
 -r  True if the file is readable (by current user)
 -s  True if file exists and is not empty
 -S  True if file is socket
 -u  True if file is set-user-id
 -w  True if file is writable (by current user)
 -x  True if file is executable
 -O  True if file is effectively owned by current user
 -G  True if file is effectively owned by current user’s group
 -N  True if file has been modified since it was last read
 file1 -nt file2  True if operand1 is greater than or equal to operand2
 file1 -ot file2  True if operand1 is greater than or equal to operand2
 file1 -ef file2  True if file1 is a hard link to file2

Common Character Matching Patterns
 Parttern  Description
 a|b  Matches either an a or a b
 *  Matches any string of characters, often used for a catchall
 [abc]  Matches any character a, b, or c
 [a-c]  Matches any character a, b, or c
 [0-9]  Matches any character 0 through 9
 "<string>"  Matches the string enclosed in the quotes

Special Shell Variables
 Name  Description
 $1 - $n  Positional parameters that hold values for parameters passed to the script.
 $?  The exit status of the last command. Contains a 0 for successfully executed commands.
  Contains a nonzero value for commands that failed.
 This nonzero value depends on what the command actually returned.
 $0  Within a shell script, contains the name of the shell script being executed.
 $#  The number of positional parameters passed to a script.
 $$  The process number of the shell. Can be used to generate unique file names.
 $!  The process number of the most recently executed background process.
 $*  Contains all the positional parameters passed to the script.

Testing a Condition
[ operand1 operator operand2 ]

Control Structures
if/then/else statement
Syntax Examples
if condition ; then
    commands
else
    commands
if

if condition ; then
    commands
elsif condition
    commands
elsif condition
    commands
if
#!/bin/bash
thresh=1048576
totMem=$(grep MemTotal /proc/meminfo | awk '{print $2}')
if [ $totMem -lt $thresh ]; then
  echo "Total Memory $totMem is less than: $thresh"
  exit 1
else
  echo "Total Memory is: $totMem"
fi

case statement
Syntax Examples
case expression in
    pattern1)
        commands ;;
    pattern2)
        commands ;;
esac
#!/bin/bash
archLoc=/dev/sdb2
usedSpc=$(df -h $archLoc | awk '{print $5}' | grep -v Use | cut -d "%" -f1 - )

case $usedSpc in
  [0-9])
    arcStat="relax, lots of disk space: $usedSpc"
  ;;
  [1-7][0-9])
    arcStat="disk space okay: $usedSpc"
  ;;
  [8][0-9])
    arcStat="gulp, space getting low: $usedSpc"
  ;;
  [9][0-9])
    arcStat="red alert, running out of space: $usedSpc"
  ;;
  [1][0][0])
    arcStat="update resume, no space left; $usedSpc"
  ;;
  *)
    arcStat="huh?: $usedSpc"
esac

BOX=$(uname -a | awk '{print $2}')
echo "archive space on: $BOX $arcStat"
exit 0

for loop
Syntax Examples
for name [in list]
do
  commands can use $name
done
#!/bin/bash
LOC_LIST="/backup /u01 /u02 /u03"
msg=""

for curLoc in $LOC_LIST
do
  usedSpc=$(df -h $curLoc | awk '{print $5}' | grep -v Use | cut -d "%" -f1 - )
  case $usedSpc in
    [0-8][0-9])
      msg="Space getting low: $curLoc on $(uname -a | awk '{print $2}') is used ${usedSpc}%"
    ;;
    [9][0-9])
      msg="Running out of space: $curLoc on $(uname -a | awk '{print $2}') is used ${usedSpc}%"
    ;;
    [1][0][0])
      msg="No space left: $curLoc on $(uname -a | awk '{print $2}') is used ${usedSpc}%"
    ;;
  esac
done

if [ -n "$msg" ] ; then
  echo $msg
fi
exit 0


Command substitution
There are two techniques for archiving command substitution in shell script:
variable=$(shell commands)
variable=`shell commands`


$BOX=$(uname -a | awk '{print$2}')
$BOX=`uname -a | awk '{print$2}'`

Either technique is a valid method. Depending on which shell you use, the $(command) command substitution might not be available.

Other example:
Pgm=$(basename $0)
if [ $# -ne 1 ]
then
  echo "Wrong number of parameters passed to script."
  echo "Usage: $Pgm ORACLE_SID"
  exit 1
fi


.


No comments:

Post a Comment

leave your message if you need help ...

Related Posts with Thumbnails