Quiz-1

create a script that displays the time and date, lists all logged-in users, and gives the system uptime and then saves this information to a logfile.

1. Creating a script to clean up the log files in /var/log

#!/bin/bash
# Script Name: logfile1.sh
# Run as oracle user.
LOGFILE=/home/oracle/admin/log/testlog1.log
echo "Today is: `date` " >> $LOGFILE
echo "List of user: `finger` " >> $LOGFILE
echo `uptime` >> $LOGFILE

exit 0

uptime: Shows how long the system has been running, along with associated statistics. A load average of 1 or less indicates that the system handles processes immediately. A load average greater than 1 means that processes are being queued. When the load average gets above 3, then system performance is significantly degraded.

A script to clean up the log files in /var/log

System administrators often write scripts to automate common tasks. Bash has become a de facto standard for shell scripting on most flavors of UNIX. Most of the principles this Tutorial covers apply equally well to scripting with other shells, such as the Korn Shell, from which Bash derives some of its features.

Creating a script to clean up the log files in /var/log

#!/bin/bash
# Script Name: cleanup.sh
# Run as root user.
LOG_DIR=/var/log
cd $LOG_DIR
ls -ltr 
cat /dev/null > messages
ls -ltr # 
-rw-------  1 root  root       0 Feb 14 10:20 messages
exit 0

1. Special Characters

# This number sign indicates that anything followed by # sign is commented and 
# will not be executed
if [ -x "$filename" ]; then # Note the space after the semicolon.
echo "The # here does not begin a comment."
echo 'The # here does not begin a comment.'
echo The \# here does not begin a comment.
echo The # here begins a comment.
echo ${PATH#*:} # Parameter substitution, not a comment.
echo $(( 2#101011 )) # Base conversion, not a comment.
Command separator [semicolon]
# Command separator [semicolon]. Permits putting two or more commands on the same line
Example:

if [ -x "$filename" ]; then # Note the space after the semicolon.
 echo "File $filename exists."; cp $filename $filename.bak
else
echo "File $filename not found."; touch $filename
fi; echo "File test complete."
Terminator in a case option [ Double semicolon]

case "$variable" in
abc) echo "\$variable = abc" ;;
xyz) echo "\$variable = xyz" ;;
esac

A signle dot and two dots
When considering directory names, a single dot represents the current working directory, and two dots denote the parent directory.

bash$ pwd
/home/oracle/app
bash$ cd .
bash$ pwd
/home/oracle/app
bash$ cd ..
bash$ pwd
/home/oracle/

partial quoting [double quote]
"STRRING"

full quoting [single quote].
'STRING'. This is astronger form of quoting than "STRING" comma operator.
Comma operator links together arithmetic operations. All evaluated but only the last one is returned.

for file in /{,usr/}bin/*sum
# Find all executable files ending in "sum"
#+ in /bin and /usr/bin directories.
do
if [ -x "$file" ]
then
echo $file
fi
done

"\" escape [backslash].A quoting mechanism for single characters. \M 'M' char will be scaped.

"/" This forward slas is used for file path separator i.e. /home/oracle/app.

"`" command substitution. The `command` substitution makes available the output of command for assignment to a variable. This is also known as backquotes or backticks.

":" colon or null command which is shell equivalent of a NOP (no op, do-no-operation). The ":" command is itself a Bash builtin, and its exit status is true (0). :
echo $? # 0

 while true
  do
  ...
 done
or
################################
if condition
then : # Do nothing and branch ahead
else # Or else ...
take-some-action
fi
################################
: > test.log # File "test.log" now empty.
# Same effect as cat /dev/null > test.log
# However, this does not fork a new process, since ":" is a builtin.
#########################################################################
"!" is what we call "bang" operator. The ! operator inverts the exit status of
the command to which it is applied. This also refer to NOT EQUAL as in "!="
"*" wild card [asterisk]. The * character serves as a "wild card" for filename expansion in globbing. By
itself, it matches every filename in a given directory.
The * also represents any number (or zero) characters in a regular expression.In the context of
 arithmetic operations, the * denotes multiplication
ls -ltr|grep control.*
"?" test operator.
===================
Within certain expressions, the ? indicates a test for a condition.
In a parameter substitution expression, the ? tests whether a variable has been set. 
wild card. The ? character used as a single-character search "wild card" for filename expansion in
globbing.

"$" Dollar sign (variable substitution).
=========================================
It is used to substitute variable as in the following example:

var1=7
var2="hello"
echo $var1 
7
echo $var2 
hello
A $ prefixing a variable name indicates the value the variable holds.
In a regular expression, a "$" addresses the end-of-line of text.

${} Parameter substitution
==============================
$*, $@ Posistional Parameter
===============================
$? Exit status variable.
=========================
The "$?" variable holds the exit status of a command, 
a function, or of the script itself.
$$ Process ID variable.
========================
The "$$" variable holds the process ID of the script in which it appears.
() Parenthesis
===================
It is used in Command Group i.e. (x=hello; echo $x)
It can be used for an array initialization i.e.
Array=(ele1 ele2 ele3)

{} Brace Expansion or curly braket
==================================
 {file1,file2,file3,...}
# Concatenates the files file1, file2, and file3 into combined_file_name.
cat {file1,file2,file3} > combined_file_name

# Copies "file.txt" to "file22.backup"
cp file22.{txt,backup}

# Can be used as extended Braces expansion such as:
$ echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z

# Used in the block of code such as creating an anonymous function

$ { local a;
a=123; }

{} \; pathname. 
==================
Used in find construct. The ";" ends the -exec option of a find command sequence. It needs to be escaped to
protect it from interpretation by the shell.

[ ] test.
================
Test expression between [ ]. Note that [ is part of the shell builtin test (and a synonym for it), not a
link to the external command /usr/bin/test.

[[ ]] test expression.
==============================
Test expression between [[ ]]. More flexible than the single-bracket [ ] test, this is a shell keyword.

[ ] An Array Element.
=========================
In the context of an array, brackets set off the numbering of each element of that array.
$ Array[1]=patern_1
[oracle@db01 ~]$ echo ${Array[1]}
patern_1

[ ] range of character.
=============================
This as part of a regular expression, brackets delineate a range of characters to match.

$((..)) integer expansion.
================================
It evaluates integer expression between $((..))
x=8
y=7
echo $(($x+$y))
15

> &> >& >> < <> Redirection.
===============================
'scriptname >filename' redirects the output of scriptname to file filename.
cat file1 > test_file.txt

'command &>filename' redirects both the stdout and the stderr of command to filename.
$ type bogus_command &>/dev/null
[oracle@db01 ~]$ echo $?
1
 and
$ type ls &>/dev/null # ls is valid command here hence dispalys 0
[oracle@db01 ~]$ echo $?
0

'command >&2' redirects stdout of command to stderr.

'scriptname >>filename' appends the output of scriptname to file filename. If
filename does not already exist, it is created.

2. Variables and Parameters

Find objects that are performing unrecoverable/nologgging option?



Using the steps below one can find the object that is unrecoverable from UNRECOVERABLE_CHANGE# in the v$datafile, one would use this to find the object that is unrecoverable since the last backup.

In this scenario datafile# 5 had unrecoverable/nologging as when we query v$datafile it shows and UNRECOVERABLE_TIME was greater than when the last backup was done.

SQL> column UNRECOVERABLE_CHANGE# format 99999999999999

SQL> select file#, UNRECOVERABLE_CHANGE# from v$datafile where file# = 5;

FILE# UNRECOVERABLE_CHANGE#
———- ———————
5 37640948153

– find the archive log that contains the sequence#
SQL> select sequence#, name from v$archived_log where 37640948153 >= first_change# and 37640948153 < next_change#;

SEQUENCE# NAME
———- ——————————————————————————–
2108 /u01/TESTDB/arch/arch_1_2108.arc

– This will create file in user_dump
SQL> alter system dump logfile '/u01/TESTDB/arch/arch_1_2108.arc' layer 19;

In 8i the object id is not included but in the 10g trace file the object id is included in the trace file
/u01/admin/TESTDB/udump $ grep DBA ora_168168_testdb.trc

…
CHANGE #1 INVLD AFN:5 DBA:0x01401e0a BLKS:0×0020 SCN:0×0008.c39349b9 SEQ: 1 OP:19.2
…

# to find the file# and block
SET SERVEROUTPUT ON
DECLARE
l_dba NUMBER := TO_NUMBER (’01401e0a’,'XXXXXXXX’);
l_file NUMBER := DBMS_UTILITY.DATA_BLOCK_ADDRESS_FILE (l_dba);
l_block NUMBER := DBMS_UTILITY.DATA_BLOCK_ADDRESS_BLOCK (l_dba);
BEGIN
DBMS_OUTPUT.PUT_LINE (‘File : ‘||l_file);
DBMS_OUTPUT.PUT_LINE (‘Block : ‘||l_block);
END;
/

Output:
File : 5
Block : 7690

SQL> SELECT owner, segment_name FROM dba_extents where file_id = 5 and block_id = 7690;

OWNER SEGMENT_NAME
—————————— —————-
SCOTT TEST_TABLE

OTN Discussion Forums: Message List - Database - General

Ask Tom HOT ARTICLES

 
© Copyright 2008 - 2011. All rights reserved to dba-sansar.blogspot.com.
All other blogs, posts and entries are the property of their respective authors.
Please email us for your comments and suggessions Click Here