1. Get rid of all advertisements and get unlimited access to documents by upgrading to Premium Membership. Upgrade to Premium Now and also get a Premium Badge!

Unix instruction

Discussion in 'Installation - Unix and Unix Like' started by Ramayan, Dec 4, 2012.

  1. Ramayan

    Ramayan Active Member

    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    55
    Location:
    India
    Hi Forum,
    I am fairly new to Oracle database. I work on toad and good with creating schema objects. I want to learn about Unix shell scripting and also like to know what kind knowledge and skill is required for pl/sql developer.

    Thanks
    Ramayan
     
  2. zargon

    zargon Community Moderator Forum Guru

    Messages:
    2,347
    Likes Received:
    348
    Trophy Points:
    1,430
    Location:
    Aurora, CO
    UNIX shell scripting is a broad topic -- there are several 'shells' one can script for and, depending on which shell you use, the commands may not be interchangable. For example in earlier versions of /bin/sh exporting environment variables was a two-step process:

    MYDIR=/floop/noober/wambo/yazoo/schmoox
    export MYDIR

    Other similar shells (/bin/bash, /bin/ksh) took a slightly different aproach:

    export MYDIR=/floop/noober/wambo/yazoo/schmoox

    /bin/csh is a different shell entirely and uses a C-like syntax; it's not used widely due to security holes (it's the first shell from the beginning of the UNIX operating system).

    Other things one should know about basic shell scripting (/bin/sh, /bin/ksh, /bin/bash) involve conditional execution (if statements, while loops, case statements), how to increment numeric variables (the expr statement), monitoring processes spawned from a script -- the list goes on. It's not as simple as one might think. Let's look at a working shell script:


    #!/bin/ksh

    #
    # memory_mon.sh
    #
    # Script to generate storage trend data
    # and report the findings
    #
    # ddf May 9,2011
    #

    #
    # User-modifiable variables
    #

    LOG="../memmon/reports/`date '+%Y%m%d%H%M'`_memory_mon.log"
    USER="/ as sysdba"

    #
    # End of user-modifiable variables
    #

    #
    # Script variables
    #

    MAILLIST="wombat_bob@yarpoo.com"
    RPTDIR="../memmon/reports"
    SQL=`which sqlplus`
    SUBJ="Current pga, uga and workarea memory usage, by instance, for SNORPO"
    TABCR="../memmon/memory_mon_tbl"
    TABDAT="../memmon/memory_mon"
    TABRPT="../memmon/memory_mon_rpt"

    if [ -z $ORACLE_SID ]
    then
    export ORACLE_SID=snorpo
    fi

    #
    # Check for directories
    #

    if [ ! -d $RPTDIR ]
    then
    mkdir -p $RPTDIR
    status=$?
    if [ $status -ne 0 ]
    then
    echo "Error creating $RPTDIR -- `date`"
    echo "Terminating"
    exit 1
    fi
    fi

    #
    # Connect to the cluster database
    #

    EXISTS=`$SQL /nolog << EOF
    connect $USER
    select table_name
    from dba_tables
    where table_name = 'MEMORY_MONITOR';
    EOF | grep MEMORY`

    #
    # Check for table existence
    #

    #
    # Table exists
    #

    if [ ! -z $EXISTS ]
    then

    #
    # Generate current data row
    # and compute report
    #
    # Mail report to interested parties
    #

    $SQL /nolog <<EOF
    connect $USER
    @TABDAT
    @TABRPT $LOG
    EOF
    mailx -s "$SUBJ" $MAILLIST < $LOG

    #
    # Table does not exist
    #

    else

    #
    # Create table
    #
    # Load first run of memory data
    #

    $SQL /nolog <<EOF
    connect $USER
    @TABCR
    EOF
    fi

    status=$?

    if [ $status -ne 0 ]
    then

    #
    # Create table failed
    #

    echo "Monitoring table did not create -- status $status -- `date`"
    echo "Check with the DBA"
    echo "Terminating"
    exit 2

    #
    # Create table succeeded
    #
    # Load first run of memory data
    #

    else
    echo "Monitoring table created -- `date`"
    $SQL /nolog <<EOF
    connect $USER
    @TABDAT
    EOF
    status=$?
    if [ $status -eq 0 ]
    then
    echo "Initial data load succeeded -- `date`"
    else
    echo "Initial data load failed -- `date`"
    exit 3
    fi
    fi

    #
    # We're here
    #
    # Everything succeeded so we exit cleanly
    #

    exit 0


    This may be confusing at first so let's look at it and explain what's happening.

    The very first line instructs UNIX to use the Korn shell to execute the commands (#!/bin/ksh). Every line after that is an instruction that /bin/ksh understands. Comments (lines preceded by '#') guide you through what each section is doing. Notice that variables are assigned values but not exported, which makes them local to the script. LOG. USER, MAILLIST and the others only need to be known locally.

    We now get to the first conditional statement, the if -- this is checking to see if the ORACLE_SID is set. If it isn't (the -z checks for a zero-length string) then the script sets it to a default value. Another if statement follows, checking to see if the RPTDIR exists (the -d does this) and if it doesn't the directory is created by the commands inside the if/fi block (fi, since I didn't explain that earlier, terminates the if 'block'). Status of the create directory statement is checked using the shell built-in $? and this is assigned to another shell variable so it can be passed to other statements later. As promised $status is used in the next if statement and if success is not achieved the script terminates and displays an error message.

    Presuming all goes well the next section of code connects to the database and determines the existence of the necessary table. Similar logic applies -- if the table exists then generate the desired repoirts and if not create the table and populate it so it's there for the next run. If the table cannot be created then an error message is reported and the script terminates. Then, again presuming all goes well the script reports success on the table load (if that is necessary) or reports that the desired reports are created, and then exits.

    This should give you a good start on learning shell scripting.
     
    Ramayan likes this.
  3. Tommy

    Tommy Guest

    It turns out that this syntax:

    MYDIR=/floop/noober/wambo/yazoo/schmoox export MYDIR

    is correct on every known shell, except for that fossil C-shell.

    How it works:

    The shell syntax allows an environment variable at the beginning of a shell command to be defined with a scope only for that one command. For example

    $ echo ${LD_LIBRARY_PATH}
    /lib
    $ LD_LIBRARY_PATH=${HOME}/lib cmd arg1 arg2
    $ echo ${LD_LIBRARY_PATH}
    /lib

    For the "cmd" application only, for this invocation only, the ${LD_LIBRARY_PATH} is set to ${HOME}/lib, but the environment for the originating shell is unchanged.

    This is a good way to run a program using a malloc(3) debugger:

    LD_PRELOAD=libefence.so.0.0 cmd arg1 arg2

    This will load the ElectricFence library for only the single execution of "cmd"; not for anything else, as would be the case if you simply exported the symbol.
     
  4. zargon

    zargon Community Moderator Forum Guru

    Messages:
    2,347
    Likes Received:
    348
    Trophy Points:
    1,430
    Location:
    Aurora, CO
    I'm wondering what, if anything, that has to do with what was posted 6 years ago.