Friday, September 13, 2019

Unix Shell Scripting Part - 1


Scripting:
  
-- Weakly or Loosely typed Programming Language
-- Interpreter Based
-- Less libraries available to perform installation
-- Special environment is not required for programming
-- Javascript, LiveScript, VBScript ,Shell Script, Perl Script

Programming:

-- Strictly typed Programming Language
-- Cimpiler Based
-- More libraries available and header files are mandatory to perform installation
-- Special environment and set up required for programming
-- Java,PL/SQL

Types of Shell:

Bourne Shell:  Steven Bourne Shell Prompt is $ and execution command is sh
C Shell: University of California Bill Joy Shell Prompt is % and execution command is Csh
Korn Shell: AT&T David Korn Shell Prompt is $ and execution command is ksh

Default Shell is : bash

Type of shell scripting:

1) Interactive shell script - user has to pass inputs to program
2) Non-Interactive shell script - don't take inputs from user

[oracle@ODIGettingStarted ~]$ pwd
/home/oracle
[oracle@ODIGettingStarted ~]$ cd practice
[oracle@ODIGettingStarted practice]$ pwd
/home/oracle/practice
[oracle@ODIGettingStarted practice]$

To get the name of the shell:

[oracle@ODIGettingStarted practice]$ echo $0
bash

To switch to Korn (ksh) shell:

[oracle@ODIGettingStarted practice]$ ksh
$echo $0
ksh
$exit

User defined Variables :

[oracle@ODIGettingStarted practice]$ x=2019
[oracle@ODIGettingStarted practice]$ echo $x
2019
[oracle@ODIGettingStarted practice]$ echo $y

[oracle@ODIGettingStarted practice]$ y=2020
[oracle@ODIGettingStarted practice]$ echo $y
2020
[oracle@ODIGettingStarted practice]$

Declaring Global variable: (here x is accessible even if we switch to kourn/c shell)

[oracle@ODIGettingStarted practice]$ export x

System defined Variables:

[oracle@ODIGettingStarted practice]$ echo $HOME
/home/oracle
[oracle@ODIGettingStarted practice]$ echo $LOGNAME
oracle
[oracle@ODIGettingStarted practice]$ echo $USER
oracle
[oracle@ODIGettingStarted practice]$ echo $PS1
[\u@\h \W]\$
[oracle@ODIGettingStarted practice]$ echo $PS2
>

Why Scripting is Loosely or Weakly Typed language?

[oracle@ODIGettingStarted practice]$ vi MyFirstScript.sh
[oracle@ODIGettingStarted practice]$ cat MyFirstScript.sh
#It is My First Shell Script
echo "Welcome to the world of Shell Scripting"
echo 'Default Script is BASH'
echo Thank You..!!
[oracle@ODIGettingStarted practice]$ sh MyFirstScript.sh
Welcome to the world of Shell Scripting
Default Script is BASH
Thank You..!!
[oracle@ODIGettingStarted practice]$

Non-Interactive Shell Scripting Examples:

Why Scripting is Interpreter based?

[oracle@ODIGettingStarted practice]$ vi MyFirstScript.sh
[oracle@ODIGettingStarted practice]$ cat MyFirstScript.sh
#It is My First Shell Script
echo "Welcome to the world of Shell Scripting"
cho 'Default Script is BASH'
echo Thank You..!!
[oracle@ODIGettingStarted practice]$ sh MyFirstScript.sh
Welcome to the world of Shell Scripting
MyFirstScript.sh: line 3: cho: command not found
Thank You..!!
[oracle@ODIGettingStarted practice]$

Number of users logged in:

[oracle@ODIGettingStarted practice]$ users
oracle oracle oracle
[oracle@ODIGettingStarted practice]$ users | wc -w
3
[oracle@ODIGettingStarted practice]$ who | wc -l
3
[oracle@ODIGettingStarted practice]$ who -q
oracle oracle oracle
# users=3
[oracle@ODIGettingStarted practice]$ vi users.sh
[oracle@ODIGettingStarted practice]$ cat users.sh
#Number of users
echo Currently logged in users count is:` who | wc -l `
echo currently logged in users are: ` who -q `
echo logged in users count is : ` users | wc -w `
echo "Success"
[oracle@ODIGettingStarted practice]$
[oracle@ODIGettingStarted practice]$ sh users.sh
Currently logged in users count is:3
currently logged in users are: oracle oracle oracle # users=3
logged in users count is : 3
Success
[oracle@ODIGettingStarted practice]$

Number of files in the current directory that are existed:

[oracle@ODIGettingStarted practice]$ pwd
/home/oracle/practice
[oracle@ODIGettingStarted practice]$ ls
MyFirstScript.sh  users.sh
[oracle@ODIGettingStarted practice]$ ls -ltr
total 8
-rw-rw-r--. 1 oracle oracle 124 Sep 13 11:44 MyFirstScript.sh
-rw-rw-r--. 1 oracle oracle 185 Sep 13 13:43 users.sh
[oracle@ODIGettingStarted practice]$ ls | wc -w
2
[oracle@ODIGettingStarted practice]$ ls | wc -l
2
[oracle@ODIGettingStarted practice]$ vi files.sh
[oracle@ODIGettingStarted practice]$ cat files.sh
#Number of files
echo "Number of files in the current directory:" `ls | wc -l`
echo "Success"
[oracle@ODIGettingStarted practice]$ sh files.sh
Number of files in the current directory: 3
Success
[oracle@ODIGettingStarted practice]$ ls -ltr
total 12
-rw-rw-r--. 1 oracle oracle 124 Sep 13 11:44 MyFirstScript.sh
-rw-rw-r--. 1 oracle oracle 185 Sep 13 13:43 users.sh
-rw-rw-r--. 1 oracle oracle  94 Sep 13 13:48 files.sh
[oracle@ODIGettingStarted practice]$

Is file extension is mandatory in Shell Scripting? Answer is : No

[oracle@ODIGettingStarted practice]$ vi noexten
[oracle@ODIGettingStarted practice]$ cat noexten
#script without file extension
echo "Welcome to BASH shell scripting"
echo Thank You!!
[oracle@ODIGettingStarted practice]$ sh noexten
Welcome to BASH shell scripting
Thank You!!
[oracle@ODIGettingStarted practice]$


Interactive Shell Scripting Examples:

[oracle@ODIGettingStarted practice]$ read a
5
[oracle@ODIGettingStarted practice]$ read b
10
[oracle@ODIGettingStarted practice]$ echo $a
5
[oracle@ODIGettingStarted practice]$ echo $b
10
[oracle@ODIGettingStarted practice]$ echo $a+$b
5+10
[oracle@ODIGettingStarted practice]$ echo $[a+b]
15
[oracle@ODIGettingStarted practice]$ read x
Raj
[oracle@ODIGettingStarted practice]$ echo $x
Raj
[oracle@ODIGettingStarted practice]$

echo

[oracle@ODIGettingStarted practice]$ vi read1.sh
[oracle@ODIGettingStarted practice]$ cat read1.sh
#Reading and displaying values
echo Enter your name:
read name
echo "Hello Mr./Mrs " $name
[oracle@ODIGettingStarted practice]$ sh read1.sh
Enter your name:
Rajasekhar
Hello Mr./Mrs  Rajasekhar
[oracle@ODIGettingStarted practice]$

echo -n

[oracle@ODIGettingStarted practice]$ vi read1.sh
[oracle@ODIGettingStarted practice]$ cat read1.sh
#Reading and displaying values
echo -n Enter your name:
read name
echo "Hello Mr./Mrs " $name
[oracle@ODIGettingStarted practice]$ sh read1.sh
Enter your name:Rajasekhar
Hello Mr./Mrs  Rajasekhar
[oracle@ODIGettingStarted practice]$

Arithmetic Operations:

[oracle@ODIGettingStarted practice]$ vi arithmetic.sh
[oracle@ODIGettingStarted practice]$ sh arithmetic.sh
Enter first number:10
Enter Second number:5
Sum of two numbers : 15
Difference of two numbers : 5
Product of two numbers: 50
[oracle@ODIGettingStarted practice]$ cat arithmetic.sh
#Arithmetic Operations
echo -n Enter first number:
read fno
echo -n Enter Second number:
read sno
echo Sum of two numbers : $[fno+sno]
echo Difference of two numbers : $[fno-sno]
echo Product of two numbers: $[fno*sno]

[oracle@ODIGettingStarted practice]$ 

No comments:

Post a Comment

Delete Row Button to all records of Interactive Report in Oracle Apex

 1. add 'Delete' Del column to Report Query 2. Set the Following Properties for the DEL Column Type: Link Heading: Delete Targ...