Wednesday, October 8, 2014

Automating Memory Management

Problem

You want to automate memory management in your Oracle database. You have both OLTP and batch
jobs running in this database. You want to take advantage of the automatic memory management
feature built into Oracle Database 11g.

Solution

Here are the steps to implement automatic memory management in your database, if you’ve already set
either the SGA_TARGET or the PGA_AGGREGATE_TARGET parameters (or both). We assume that we are going to
allocate 2,000 MB to the MEMORY_MAX_TARGET parameter and 1,000 MB to the MEMORY_TARGET parameter.

1. Connect to the database with the SYSDBA privilege.
2. Assuming you’re using the SPFILE, first set a value for the MEMORY_MAX_TARGET
parameter:

SQL> alter system set memory_max_target=2G scope=spfile;
System altered.

You must specify the SCOPE parameter in the alter system command, because MEMORY_MAX_TARGET isn’t a dynamic parameter, which means you can’t change it on the fly while the instance is running.

3. Note that if you’ve started the instance with a traditional init.ora parameter
file instead of the SPFILE, you must add the following to your init.ora file:
memory_max_target = 2000M
memory_target = 1000M

4. Bounce the database.

5. Turn off the SGA_TARGET and the PGA_AGGREGATE_TARGET parameters by issuing
the following ALTER SYSTEM commands:

SQL> alter system set sga_target = 0;
SQL> alter system set pga_aggregate_target = 0;

6. Turn on automatic memory management by setting the MEMORY_TARGET parameter:

SQL> alter system set memory_target = 1000M;

From this point on, the database runs under the automatic memory management mode, with it shrinking and growing the individual allocations to the various components of Oracle memory according to the requirements of the ongoing workload. You can change the value of the MEMORY_TARGET parameter dynamically anytime, as long as you don’t exceed the value you set for the MEMORY_MAX_TARGET parameter.

■ Tip The term “target” in parameters such as memory_target and pga_memory_target means just that—Oracle will try to stay under the target level, but there’s no guarantee that it’ll never go beyond that. It may exceed the target allocation on occasion, if necessary.

You don’t have to set the SGA_TARGET and PGA_AGGREGATE_TARGET parameters to 0 in order to use automatic memory management. In Recipe 3-3, we show how to set minimum values for these parameters even when you choose to implement automatic memory management. That recipe assumes you’re implementing automatic memory management, but that for some reason, you need to specify your own minimum values for components such as the SGA and the PGA.

How It Works

In earlier releases of the Oracle database, DBAs used to set values for the various SGA components, or would specify values for the SGA and the PGA. Starting with the Oracle Database 11g release, Oracle enables you to completely automate the entire instance memory allocation, by just setting a single initialization parameter, MEMORY_TARGET, under what’s known as automatic memory management. In this Recipe, we show you how to set up the automatic memory management feature in your database.

If you’re creating a new Oracle database with the help of the Database Configuration Assistant (DBCA), you’re given a choice among automatic memory management, shared memory management, and manual memory management. Select the automatic memory management option, and specify the values for two automatic memory-related parameters: MEMORY_TARGET and MEMORY_MAX_TARGET. The first parameter sets the current value of the memory allocation to the database, and the second parameter sets the limit to which you can raise the first parameter if necessary.
Oracle’s memory structures consist of two distinct memory areas. The system global area (SGA) contains the data and control information and is shared by all server and background processes. The SGA holds the data blocks retrieved from disk by Oracle. The program global area (PGA) contains data and control information for a server process. Each server process is allocated its own chunk of the PGA. Managing Oracle’s memory allocation involves careful calibration of the needs of the database. Some database instances need more memory for certain components of the memory. For example, a data warehouse will need more PGA memory in order to perform huge sorts that are common in such an environment. Also, during the course of a day, the memory needs of the instance might vary; during
business hours, for example, the instance might be processing more online transaction processing (OLTP) work, whereas after business hours, it might be running huge batch jobs that involve data warehouse processing, jobs that typically need higher PGA allocations per each process.

In prior versions of the Oracle database, DBAs had to carefully decide the optimal allocation of memory to the individual components of the memory one allocated to the database. Technically, you can still manually set the values of the individual components of the SGA as well as set a value for the PGA, or partially automate the process by setting parameters such as SGA_TARGET and PGA_AGGREGATE_TARGET. Although Oracle still allows you to manually configure the various components of memory, automatic memory management is the recommended approach to managing Oracle’s memory allocation. Once you specify a certain amount of memory by setting the MEMORY_TARGET and MEMORY_MAX_TARGET parameters, Oracle automatically tunes the actual memory allocation, by
redistributing memory between the SGA and the PGA.

■ Tip When you create a database with the Database Configuration Assistant (DBCA), automatic memory management is the default.

Oracle Database 11g lets you automate all the memory allocations for an instance, including shared memory and the PGA memory, if you choose to implement automatic memory management by setting the MEMORY_TARGET and MEMORY_MAX_TARGET parameters. Under an automatic memory management regime, Oracle automatically tunes the total SGA size, the SGA component sizes, the instance PGA size, and the individual PGA size. This dynamic memory tuning by the Oracle instance optimizes database performance, as memory allocations are changed automatically by Oracle to match changing database workloads. Automatic memory management means that once you set the MEMORY_TARGET parameter, you can simply ignore the following parameters by not setting them at all:

• SGA_TARGET
• PGA_AGGREGATE_TARGET
• DB_CACHE_SIZE
• SHARED_POOL_SIZE
• LARGE_POOL_SIZE
• JAVA_POOL_SIZE\

If you’re moving from a system where you were using the SGA_TARGET and PGA_AGGREGATE_TARGET parameters, you can follow the procedures shown in the “Solution” section of this recipe to move to the newer automatic memory management mode of managing Oracle’s memory allocation. Note that while setting the MEMORY_TARGET parameter is mandatory for implementing automatic memory management, the MEMORY_MAX_TARGET parameter isn’t—if you don’t set this parameter, Oracle sets its value internally to that of the MEMORY_TARGET parameter. Also, the MEMORY_MAX_TARGET parameter acts as the upper bound for the MEMORY_TARGET parameter. Oracle has different minimum permissible settings for the MEMORY_TARGET
parameter, depending on the operating system. If you try to set this parameter below its minimum allowable value, the database will issue an error. Some of the memory components can’t shrink quickly and some components must have a minimum size for the database to function properly. Therefore, Oracle won’t let you set too low a value for the MEMORY_TARGET parameter. The following example shows this:

SQL> alter system set memory_target=360m scope=both;
alter system set memory_target=360m scope=both
*
ERROR at line 1:
ORA-02097: parameter cannot be modified because specified value is invalid
ORA-00838: Specified value of MEMORY_TARGET is too small, needs to be at least
544M

SQL> alter system set memory_target=544m scope=both;
alter system set memory_target=544m scope=both
*
ERROR at line 1:
ORA-02097: parameter cannot be modified because specified value is invalid
ORA-00838: Specified value of MEMORY_TARGET is too small, needs to be at least
624M

SQL> alter system set memory_target=624m scope=both;
System altered.
SQL>

You’ll notice that Oracle issued an error when we tried to set a very low value for the MEMORY_TARGET
parameter. Note that Oracle took iterations to decide to let you know the minimum allowable level for
the MEMORY_TARGET parameter.

How does one go about setting the value of the MEMORY_MAX_TARGET parameter? It’s simple—you just pick a value that’s high enough to accommodate not only the current workloads, but also the future needs of the database. Since the MEMORY_TARGET parameter is dynamic, you can alter it on the fly and if necessary, re-allocate memory among multiple instances running on a server. Just be sure that you set the value of the MEMORY_MAX_TARGET parameter to a size that’s at least equal to the combined value of the present settings of the SGA_TARGET and the PGA_AGGREGATE_TARGET parameters (if you're migrating from the 10g release). Always make sure to check with your system administrator, so you don’t allocate too high an amount of memory for your Oracle instance, which could result in problems such as paging and swapping at the operating system level, which will affect not only your Oracle database, but also everything else that’s running on that server.

Saturday, May 24, 2014

ASM Instance Dynamic Views((G)v$) List

 

To find the list of ASM related dynamic views present in ASM instance, we need to connect to ASM instance and query the v$fixed_table ASM instance dynamic view.

1 [oracle@rac1 ~]$ ps -ef |grep -v grep|grep smon
2 oracle 3054 1 0 May24 ? 00:00:06 asm_smon_+ASM1
3 root 3090 1 4 May24 ? 00:59:49 /u01/app/12.1.0/grid/bin/osysmond.bin
4 oracle 6687 1 0 07:04 ? 00:00:01 mdb_smon_-MGMTDB
5 oracle 6804 1 0 07:04 ? 00:00:02 ora_smon_MyDB1
6 [oracle@rac1 ~]$ . oraenv
7 ORACLE_SID = [oracle] ? +ASM1
8 The Oracle base has been set to /u01/app/oracle
9 [oracle@rac1 ~]$ sqlplus / as sysasm
10
11 SQL*Plus: Release 12.1.0.1.0 Production on Sun May 25 08:10:45 2014
12
13 Copyright (c) 1982, 2013, Oracle. All rights reserved.
14
15
16 Connected to:
17 Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production
18 With the Real Application Clusters and Automatic Storage Management options
19
20 SQL> select * from v$fixed_table where name like '%ASM%'
21 2 /
22
23 NAME OBJECT_ID TYPE TABLE_NUM CON_ID
24 ------------------------------ ---------- ----- ---------- ----------
25 GV$ASM_TEMPLATE 4294951861 VIEW 65537 0
26 V$ASM_TEMPLATE 4294951862 VIEW 65537 0
27 GV$ASM_ALIAS 4294951864 VIEW 65537 0
28 V$ASM_ALIAS 4294951865 VIEW 65537 0
29 GV$ASM_FILE 4294951867 VIEW 65537 0
30 V$ASM_FILE 4294951868 VIEW 65537 0
31 GV$ASM_VOLUME 4294952802 VIEW 65537 0
32 V$ASM_VOLUME 4294952803 VIEW 65537 0
33 GV$ASM_FILESYSTEM 4294952908 VIEW 65537 0
34 V$ASM_FILESYSTEM 4294952909 VIEW 65537 0
35 GV$ASM_ACFSVOLUMES 4294952902 VIEW 65537 0
36 V$ASM_ACFSVOLUMES 4294952903 VIEW 65537 0
37 GV$ASM_ACFSSNAPSHOTS 4294953048 VIEW 65537 0
38 V$ASM_ACFSSNAPSHOTS 4294953049 VIEW 65537 0
39 GV$ASM_ACFSTAG 4294953498 VIEW 65537 0
40 V$ASM_ACFSTAG 4294953499 VIEW 65537 0
41 GV$ASM_ACFS_SECURITY_INFO 4294953224 VIEW 65537 0
42 V$ASM_ACFS_SECURITY_INFO 4294953225 VIEW 65537 0
43 GV$ASM_ACFS_ENCRYPTION_INFO 4294953227 VIEW 65537 0
44 V$ASM_ACFS_ENCRYPTION_INFO 4294953228 VIEW 65537 0
45 GV$ASM_ACFS_SEC_RULE 4294953510 VIEW 65537 0
46 V$ASM_ACFS_SEC_RULE 4294953511 VIEW 65537 0
47 GV$ASM_ACFS_SEC_REALM 4294953513 VIEW 65537 0
48 V$ASM_ACFS_SEC_REALM 4294953514 VIEW 65537 0
49 GV$ASM_ACFS_SEC_REALM_USER 4294953516 VIEW 65537 0
50 V$ASM_ACFS_SEC_REALM_USER 4294953517 VIEW 65537 0
51 GV$ASM_ACFS_SEC_REALM_GROUP 4294953519 VIEW 65537 0
52 V$ASM_ACFS_SEC_REALM_GROUP 4294953520 VIEW 65537 0
53 GV$ASM_ACFS_SEC_REALM_FILTER 4294953522 VIEW 65537 0
54 V$ASM_ACFS_SEC_REALM_FILTER 4294953523 VIEW 65537 0
55 GV$ASM_ACFS_SEC_RULESET 4294953525 VIEW 65537 0
56 V$ASM_ACFS_SEC_RULESET 4294953526 VIEW 65537 0
57 GV$ASM_ACFS_SEC_RULESET_RULE 4294953528 VIEW 65537 0
58 V$ASM_ACFS_SEC_RULESET_RULE 4294953529 VIEW 65537 0
59 GV$ASM_ACFS_SEC_CMDRULE 4294953531 VIEW 65537 0
60 V$ASM_ACFS_SEC_CMDRULE 4294953532 VIEW 65537 0
61 GV$ASM_ACFS_SEC_ADMIN 4294953534 VIEW 65537 0
62 V$ASM_ACFS_SEC_ADMIN 4294953535 VIEW 65537 0
63 GV$ASM_ACFSREPL 4294953588 VIEW 65537 0
64 V$ASM_ACFSREPL 4294953589 VIEW 65537 0
65 GV$ASM_ACFSREPLTAG 4294953591 VIEW 65537 0
66 V$ASM_ACFSREPLTAG 4294953592 VIEW 65537 0
67 GV$ASM_VOLUME_STAT 4294952905 VIEW 65537 0
68 V$ASM_VOLUME_STAT 4294952906 VIEW 65537 0
69 GV$ASM_CLIENT 4294951872 VIEW 65537 0
70 V$ASM_CLIENT 4294951873 VIEW 65537 0
71 GV$ASM_DISKGROUP 4294951877 VIEW 65537 0
72 V$ASM_DISKGROUP 4294951878 VIEW 65537 0
73 GV$ASM_DISKGROUP_STAT 4294952651 VIEW 65537 0
74 V$ASM_DISKGROUP_STAT 4294952652 VIEW 65537 0
75 GV$ASM_DISK 4294951880 VIEW 65537 0
76 V$ASM_DISK 4294951881 VIEW 65537 0
77 GV$ASM_DISK_STAT 4294952653 VIEW 65537 0
78 V$ASM_DISK_STAT 4294952654 VIEW 65537 0
79 GV$ASM_DISK_IOSTAT 4294952225 VIEW 65537 0
80 V$ASM_DISK_IOSTAT 4294952226 VIEW 65537 0
81 GV$ASM_OPERATION 4294951967 VIEW 65537 0
82 V$ASM_OPERATION 4294951968 VIEW 65537 0
83 GV$ASM_ATTRIBUTE 4294952221 VIEW 65537 0
84 V$ASM_ATTRIBUTE 4294952222 VIEW 65537 0
85 GV$ASM_USER 4294952241 VIEW 65537 0
86 V$ASM_USER 4294952242 VIEW 65537 0
87 GV$ASM_USERGROUP 4294952235 VIEW 65537 0
88 V$ASM_USERGROUP 4294952236 VIEW 65537 0
89 GV$ASM_USERGROUP_MEMBER 4294952238 VIEW 65537 0
90 V$ASM_USERGROUP_MEMBER 4294952239 VIEW 65537 0
91 GV$ASM_ESTIMATE 4294952252 VIEW 65537 0
92 V$ASM_ESTIMATE 4294952253 VIEW 65537 0
93 GV$ASM_AUDIT_CLEAN_EVENTS 4294952275 VIEW 65537 0
94 V$ASM_AUDIT_CLEAN_EVENTS 4294952276 VIEW 65537 0
95 GV$ASM_AUDIT_CLEANUP_JOBS 4294952278 VIEW 65537 0
96 V$ASM_AUDIT_CLEANUP_JOBS 4294952279 VIEW 65537 0
97 GV$ASM_AUDIT_CONFIG_PARAMS 4294952281 VIEW 65537 0
98 V$ASM_AUDIT_CONFIG_PARAMS 4294952282 VIEW 65537 0
99 GV$ASM_AUDIT_LAST_ARCH_TS 4294952284 VIEW 65537 0
100 V$ASM_AUDIT_LAST_ARCH_TS 4294952285 VIEW 65537 0
101
102 76 rows selected.
103
104

Thursday, March 6, 2014

ASMCA

ASMCA stands for – Automatic Storage Management Configuration Assistant.

In Oracle 11gR2, ASMCA is used to create and configure ASM instance

Prior to 11gR2, DBCA is used to create and configure ASM instance.

As typical with all of oracle’s wizard, CA(configuration assistance is a Wizard), so has provided below wizards for different activities

ASMCA - Automatic Storage Management Configuration Assistant

DBCA – Database Configuration Assistant

NetCA – Network Configuration Assistant

EMCA – Enterprise Manager Configuration Assistant

 

- In RAC environment, for installing and configuring Grid Infrastructure(GI) Architecture we ASMCA is used.

- Installing and configuring Grid Infrastructure does a variety of things like

-It installs the GI software

-Installs the cluster software

-Creates ASM Instance

-Creates a default disk group to contain RAC specific disks (eg: Voting Disk & OCR Disks)

- After Installing the GI software, we will install DB software(just the software, without creating the DB)

- So before creating the DB (using DBCA), we usually use ASMCA to create the ASM disk groups to be used by Database.

- After the disk groups are created using ASM disk groups, Database is created using DBCA and point the disk groups created by ASMCA

To launch the asmca, one should be in ASM instance as below.

[oracle@rac1 ~]$ asmca
bash: asmca: command not found
[oracle@rac1 ~]$ . oraenv
ORACLE_SID = [oracle] ? MyDB
The Oracle base has been set to /u01/app/oracle
[oracle@rac1 ~]$ asmca
bash: asmca: command not found

[oracle@rac1 ~]$ ps -ef | grep smon
oracle    2954     1  0 Mar03 ?        00:00:13 asm_smon_+ASM1
root      2993     1  2 Mar03 ?        02:01:42 /u01/app/12.1.0/grid/bin/osysmond.bin
oracle    3371     1  0 Mar03 ?        00:00:09 mdb_smon_-MGMTDB
oracle    3755     1  0 Mar03 ?        00:00:20 ora_smon_MyDB1
oracle    4007  3486  0 08:35 pts/1    00:00:00 grep smon
[oracle@rac1 ~]$ . oraenv
ORACLE_SID = [oracle] ? +ASM1
The Oracle base has been set to /u01/app/oracle
[oracle@rac1 ~]$ asmca

image

image

To create a new disk group click on the Create button, in my current environment, there are no disks to add to the DiskGroups

 

image

High Redundancy – Should have at least 3 disks in diskgroups

Normal Redundancy – Should have at least 2 disks in diskgroups

External Redundacy – Should have at least 1 disk in the diskgroup

 

 

[oracle@rac1 ~]$ crsctl stat res -t
--------------------------------------------------------------------------------
Name           Target  State        Server                   State details      
--------------------------------------------------------------------------------
Local Resources
--------------------------------------------------------------------------------
ora.DATA.dg  <====================   Data DiskGroup(dg) which was listed in asmca
               ONLINE  ONLINE       rac1                     STABLE
               ONLINE  ONLINE       rac2                     STABLE
ora.LISTENER.lsnr
               ONLINE  ONLINE       rac1                     STABLE
               ONLINE  ONLINE       rac2                     STABLE
ora.asm
               ONLINE  ONLINE       rac1                     Started,STABLE
               ONLINE  ONLINE       rac2                     Started,STABLE
ora.net1.network
               ONLINE  ONLINE       rac1                     STABLE
               ONLINE  ONLINE       rac2                     STABLE
ora.ons
               ONLINE  ONLINE       rac1                     STABLE
               ONLINE  ONLINE       rac2                     STABLE
--------------------------------------------------------------------------------
Cluster Resources
--------------------------------------------------------------------------------
ora.LISTENER_SCAN1.lsnr
      1        ONLINE  ONLINE       rac2                     STABLE
ora.LISTENER_SCAN2.lsnr
      1        ONLINE  ONLINE       rac1                     STABLE
ora.LISTENER_SCAN3.lsnr
      1        ONLINE  ONLINE       rac1                     STABLE
ora.MGMTLSNR
      1        ONLINE  ONLINE       rac1                     169.254.90.64 192.16
                                                             8.10.1,STABLE
ora.cvu
      1        ONLINE  ONLINE       rac1                     STABLE
ora.mgmtdb
      1        ONLINE  ONLINE       rac1                     Open,STABLE
ora.mydb.db
      1        ONLINE  ONLINE       rac1                     Open,STABLE
      2        ONLINE  ONLINE       rac2                     Open,STABLE
ora.oc4j
      1        ONLINE  ONLINE       rac1                     STABLE
ora.rac1.vip
      1        ONLINE  ONLINE       rac1                     STABLE
ora.rac2.vip
      1        ONLINE  ONLINE       rac2                     STABLE
ora.scan1.vip
      1        ONLINE  ONLINE       rac2                     STABLE
ora.scan2.vip
      1        ONLINE  ONLINE       rac1                     STABLE
ora.scan3.vip
      1        ONLINE  ONLINE       rac1                     STABLE
--------------------------------------------------------------------------------

Sunday, March 2, 2014

Unix Shell Scripting–Expr

- The Bourne shell has no notion of a number (only strings), and as such incapable of doing numerical calculations

- However, there is a UNIX program called “expr” which was designed specifically for this purpose.

- It works like this:

[oracle@rac1 ~]$ expr 3 + 5
8
Note: There should be space before and after numerical operator (+, –, *, / etc)

Multiplication can be done using *, but * is also used for wildcard by Unix, so multiplications one should escape using backslash like below

[oracle@rac1 ~]$ expr 3 * 4
expr: syntax error
[oracle@rac1 ~]$ expr 3 \* 4
12

- To assign the output of an expr to a other variable, the expression need to placed in the backquotes (as expr is a Unix command and we are working with $ and variables)

oracle@rac1 ~]$ a=15
[oracle@rac1 ~]$ b=3
[oracle@rac1 ~]$ c=expr $a / $b
bash: 15: command not found  <===  without back quotes Unix considers the literal value of $a (which is 15) and throws the error saying 15 is not a command in bash.
[oracle@rac1 ~]$ c=`expr $a / $b`
[oracle@rac1 ~]$ echo $c
5

- Example of using expr in loops:

echo -n "Enter the counter to loop: "
read count

i=1

while [ $i -le $count ]
do
echo "This is loop number $i"
i=`expr $i + 1`
done

Enter the counter to loop: 5
This is loop number 1
This is loop number 2
This is loop number 3
This is loop number 4
This is loop number 5

Sunday, February 16, 2014

Unix Shell Scripting–While Loop

- The while loop repeats the execution of a code block in the same way that the if statement conditionally executes a code block

- The syntax for the while loop is as follows

    while command  <—when command condition is true (status zero), then entered into loop

    do

        code block

     done

- The given code block will be repeatedly executed instill the given command returns an exit status that is non-zero

- Example

echo -n 'Please enter your answer : '

read answer

while [ "$answer" != tiger ]

do

      echo That answer is incorrect

      echo "Please try again, you entered $answer"

       read answer

done

Output:

Please enter your answer : Tiger
That answer is incorrect
Please try again, you entered Tiger
Chicken
That answer is incorrect
Please try again, you entered Chicken
tiger <—Exits the program as the condition is met, the while loop is not entered

Other Example:

echo -n "Please enter the directory name :"
read dir

while [ ! -d $dir ]
do
echo "You didn't enter a directory name"
echo -n "Please try again by entering a directory name :"
read dir
done

echo "Congratulation!! Your Directory Name is $dir"

Output:

Please enter the directory name :..
Congratulation!! Your Directory Name is ..

Please enter the directory name :sdfsdfdsf
You didn't enter a directory name
Please try again by entering a directory name :fdsg
You didn't enter a directory name
Please try again by entering a directory name :

- Using while loop we can pipe the output of a Unix command, read the values and loop them

Example:

Output of who command has 3 columns

- User – Details regarding the user who currently logged in

- Terminal – Details of the terminal the user is using

- Time – Details of time the user has logged in since.

[oracle@rac1 ~]$ who
oracle   tty1         2014-02-16 10:20 (:0)
oracle   pts/0        2014-02-16 10:20 (:0.0)

who | while read user term time
do
     echo $user is using $term logged in since $time

done

Output:

[oracle@rac1 ~]$ . PipedWhile
oracle is using tty1 logged in since 2014-02-16 10:20 (:0)
oracle is using pts/0 logged in since 2014-02-16 10:20 (:0.0)

- Break & Continue can be used in conjunction with while loop (for that matter any other loop in Unix Shell Programming)

Break – The break statement will cause the loop to immediately terminate. Execution will recommence with the next line after the done

Continue – The continue statement will cause the loop to abandon the current iteration of the loop and begin the next one (the loop condition is retested)

Saturday, February 1, 2014

Unix Shell Scripting-Conditional Command Execution

- It is possible in Unix shell scripting to specify that a command to run only if particular condition is met.

- Such conditions are always expressed in terms of the exit status of another program as follows

command1 && command2  ==> command2 runs only when command1 exit status is 0(when there are no errors in executing command1)

command3 || command4 ==> command4 runs only when command3 exit status is not 0(when there are errors in executing command3)

oracle@rac1 tmp]$ ls ffdsfdf && cp ffdsfdf /tmp
ls: cannot access ffdsfdf: No such file or directory

[oracle@rac1 ~]$ ls myscript && cp myscript /tmp
myscript


[oracle@rac1 ~]$ cd /tmp
[oracle@rac1 tmp]$ ls -lart myscript
-rwxr-xr-x 1 oracle dba 545 Feb  2 11:17 myscript

- The only problem with these constructs is that they are very limited

    - You cannot specify a second command to be run if the condition is not met

    - You can only perform one command if the condition is met. (however, it is possible to group the commands)

Unix Shell Scripting-Conditional Code

- When every UNIX command completes, it invisibly returns a value to the program that started it (usually the shell) informing that program of the “status” of completion of the command.

- That value is a number, and is known as the “exit” status of the command

- Typically an exit status of 0 means that the program completed without any errors, while an exit status of some other value (other than 0) means errors has occurred

- The exit status is stored in a build-in variable called “?” and can be examined at any time with the command -   echo $?  as below

- The contents of this variable(?) as updated every time a program is run (including the echo command)

[oracle@rac1 ~]$ ls dffsdf
ls: cannot access dffsdf: No such file or directory


[oracle@rac1 ~]$ echo $?
2

[oracle@rac1 ~]$ echo $?
0  <--  The value 0 in here means that the above echo command has run successfully without any errors

- It is often useful for shell programming to think of exit status of 0 & 1 as below

0 – True – Executed without any errors

1 – False – Executed with errors

[oracle@rac1 ~]$ true
[oracle@rac1 ~]$ echo $?
0


[oracle@rac1 ~]$ false
[oracle@rac1 ~]$ echo $?
1