Introduction to UNIX and Shell Scripting Confidential to

  • Slides: 29
Download presentation
Introduction to UNIX and Shell Scripting Confidential to Trianz Inc. 1

Introduction to UNIX and Shell Scripting Confidential to Trianz Inc. 1

Course Coverage • Introduction to Unix and its architecture • Learn to use basic

Course Coverage • Introduction to Unix and its architecture • Learn to use basic Unix commands • Learn to use vi editor • Learn to write shell scripts • Learn to register and run shell scripts from oracle applications Confidential to Trianz Inc. 2

Introduction to Unix What is UNIX ? UNIX is an operating system co-created by

Introduction to Unix What is UNIX ? UNIX is an operating system co-created by AT&T Bell Labs researchers Dennis Ritchie and Ken Thompson in the late 1960 s. Unix is a multitasking, multiuser, programming environment. Confidential to Trianz Inc. 3

Unix Architecture Confidential to Trianz Inc. 4

Unix Architecture Confidential to Trianz Inc. 4

Unix Architecture Kernel – is the single large program which resides in the memory

Unix Architecture Kernel – is the single large program which resides in the memory which is the operating system. It manages File system, Memory, I/O and Processes. Shell – is a command interpreter which provides the user interface for the UNIX operating system. Confidential to Trianz Inc. 5

UNIX Flavors � � HP-UX - HP UNIX developed by HP Sun. OS and

UNIX Flavors � � HP-UX - HP UNIX developed by HP Sun. OS and Solaris – Developed by Sun Microsystems � AIX – Developed by IBM � SCO Unix – Developed by SCO � Linux - Free Source code � BSD – Berkley Software Design Confidential to Trianz Inc. 6

Types of Shells � � � Bourne shell (sh) C shell (csh) TC shell

Types of Shells � � � Bourne shell (sh) C shell (csh) TC shell (tcsh) Korn shell (ksh) Bourne Again SHell (bash) Confidential to Trianz Inc. 7

Summary of shell facilities Bourne C TC Korn BASH Command history No Yes Yes

Summary of shell facilities Bourne C TC Korn BASH Command history No Yes Yes Command alias No Yes Yes Shell scripts Yes Yes Yes Filename completion No Yes* Yes Command line editing No No Yes* Yes Job control No Yes Yes * not the default setting for this shell Confidential to Trianz Inc. 8

File Permissions There are three types of file access supported by UNIX. � r

File Permissions There are three types of file access supported by UNIX. � r – read, view the contents of a file or a directory � w – write, edit file/directory contents � x – execute, run executable file Confidential to Trianz Inc. 9

File Permissions For example: Suppose you type in ls -l and the result is

File Permissions For example: Suppose you type in ls -l and the result is - rwx r-- 1 user 1 oracle 858 Feb 22 22: 28 file 1. txt owner type links User permissions Group permissions Modification date/time group File name size Other Permissions Confidential to Trianz Inc. 10

UNIX Commands ls - List directory contents cd - Change the current directory cp

UNIX Commands ls - List directory contents cd - Change the current directory cp - Copy files and directories mv - Move (rename) files mkdir - Make a directory rm - Remove files or directories (Use with Caution) rmdir – Delete a directory (Use with Caution) echo – Display a line of text Confidential to Trianz Inc. 11

UNIX Commands cat – Concatenate files and print on the std output date –

UNIX Commands cat – Concatenate files and print on the std output date – Print the system date and time head – Output the first part of files tail – Output the last part of files pwd – Print name of current/Working Directory man – Format and display the on-line manual pages who – Show who is logged on chmod - Change file access permissions Confidential to Trianz Inc. 12

UNIX Commands grep – Print lines matching a pattern find – Search for files

UNIX Commands grep – Print lines matching a pattern find – Search for files in a directory hierarchy diff – Find differences between two files ps - Report process status cut – Remove sections from each line of files su – run a shell with substitute user and group IDs wc - Print the number of bytes, words, and lines in files ln - Make links between files Confidential to Trianz Inc. 13

UNIX Commands alias/unalias - Shorthand for a command or commands sort - Sort lines

UNIX Commands alias/unalias - Shorthand for a command or commands sort - Sort lines of text files exit – Exit the current shell with status shift – Shift positional parameters env – Display the environment variables logout - Exit a login shell Confidential to Trianz Inc. 14

Unix Variables There are 2 types of variables in Unix. Environment variables Ø Shell

Unix Variables There are 2 types of variables in Unix. Environment variables Ø Shell variables Ø As per conventions environment variables will be in uppercase and Shell variables will be in lowercase. Example : APPL_TOP=/srv/115102/inst/visappl pathseg=/home/oracle/bin today=`date` export – to export the variable to the environment Confidential to Trianz Inc. 15

I/O Redirection operators: Output redirection : >, |, >> Input redirection : < Example:

I/O Redirection operators: Output redirection : >, |, >> Input redirection : < Example: cat test 1 test 2 > test 3 cat test 2 >> test 1 ls –l | grep new sort < test. txt > sortedtest. txt Confidential to Trianz Inc. 16

vi Text Editor – King of all editors vi – Visual text editor vi

vi Text Editor – King of all editors vi – Visual text editor vi <file_name> - to open the any text file in vi editor. Two modes of vi Command mode – Esc key Ø Insert mode – a or i Ø Confidential to Trianz Inc. 17

Some Simple VI Commands a - enter append mode i - enter insert mode

Some Simple VI Commands a - enter append mode i - enter insert mode h - move cursor left j - move cursor down k - move cursor up l - move cursor right x - delete char under cursor r - replace one character under the cursor u - undo the last change to the file Confidential to Trianz Inc. 18

Some Simple VI Commands ^ - Goto the beginning of the line $ -

Some Simple VI Commands ^ - Goto the beginning of the line $ - Goto the end of the line [n]dd - delete line/lines from the cursor position [n]yy - yank (copy) line/lines from the cursor position p - paste the yanked line/lines below the cursor position ctrl-f - Scroll forwards one page ctrl-b - Scroll backwards one page : w - Save the file : q - quit vi Confidential to Trianz Inc. 19

Shell scripting Conventionally, a shell script should start with a line: #!/bin/ksh Example shell

Shell scripting Conventionally, a shell script should start with a line: #!/bin/ksh Example shell script #!/bin/bash echo “Hello $USER” echo “Today is “ `date` echo “You are in directory $PWD” exit 0 Confidential to Trianz Inc. 20

Command line arguments Ex: ls –lrt, myshell. sh oracle $1 to $9 – will

Command line arguments Ex: ls –lrt, myshell. sh oracle $1 to $9 – will give the arguments 1 to 9. To get remaining we need to use shift command. $@ - will give the complete list of all the arguments to that shell. Confidential to Trianz Inc. 21

Shell scripting Conditionals, if/then/elif if list then list [elif list then list]. . .

Shell scripting Conditionals, if/then/elif if list then list [elif list then list]. . . [else list] fi Example: if [ $x -lt $y ] # is $x less than $y ? then echo “$y is greater than $x” fi Confidential to Trianz Inc. 22

Shell scripting For loops for name in w 1 w 2. . . do

Shell scripting For loops for name in w 1 w 2. . . do command-list done Example: for X in red green blue do echo $X done Confidential to Trianz Inc. 23

Shell scripting While Loops while command-list 1 do command-list 2 done Example: x=0 while

Shell scripting While Loops while command-list 1 do command-list 2 done Example: x=0 while [ $x -le 20 ] do echo $x x=`expr $x + 1` done Confidential to Trianz Inc. 24

Default shell variables $? - returns 0 if the last command is executed successfully,

Default shell variables $? - returns 0 if the last command is executed successfully, else non zero. $# - returns the number of arguments. $! - returns the last command PID run in bg (using &) $$ - returns the current shell PID Confidential to Trianz Inc. 25

Connect to DB and run a SQL sqlplus -s <userid>/<passwd>@<dbsid> @<filepath>/<filename> Example 1: sqlplus

Connect to DB and run a SQL sqlplus -s <userid>/<passwd>@<dbsid> @<filepath>/<filename> Example 1: sqlplus -s $UID/$PWD@$CONSTR@$EXECDIR/sql/oa_com_monitor_cmrun Example 2 : st_date=`sqlplus -s $UID/$PWD@$CONSTR << EOF set head off set feedback off select to_char(sysdate-15, 'DD-MON-YYYY') from dual; EOF` Confidential to Trianz Inc. 26

Steps to register a shell script The shell script can have either of the

Steps to register a shell script The shell script can have either of the 2 extensions. . prog or. sh. prog : 1. Create a soft link to the shell without extension to $FND_TOP/bin/fndcpesr Ex: ln -s $FND_TOP/bin/fndcpesr $FND_TOP/bin/TEST 2. Grant execute permission to the both the files (. prog and the file without extension) Ex: chmod +x TEST. prog TEST . sh : No need to create a soft link. Confidential to Trianz Inc. 27

Differences b/w. prog and. sh. prog: will get the following parameters as default to

Differences b/w. prog and. sh. prog: will get the following parameters as default to the shell. $1 - Connection string (userid/passwd@dbsid) $2 - Fnd user id $3 - Fnd user $4 - Request id. sh : will only get one parameter which as all the above along with some additional ones. Ex: FCP_REQID=250898 FCP_LOGIN="APPS/APPS" FCP_USERID=1117 FCP_USERNAME="SRVCMGR" FCP_PRINTER="noprint" FCP_SAVE_OUT=Y FCP_NUM_COPIES=0 Confidential to Trianz Inc. 28

QUESTIONS ? ? Confidential to Trianz Inc. 29

QUESTIONS ? ? Confidential to Trianz Inc. 29