Wednesday, November 4, 2020

Variables in UNIX

 [scott]$ cat sample.sh
#!/bin/bash
#This is basic bash script exmaple.
a=Hello
b='Hello'
c="Hello"
d=123
echo $a
echo $b
echo $c
echo $d
echo "Hello, value of d is $d"
[scott]$ sh sample.sh
Hello
Hello
Hello
123
Hello, value of d is 123
[scott]$
Variables Declaration
[scott]$ cat sample.sh
#!/bin/bash
#This is basic bash script exmaple.
a=Hello
b='Hello'
c="Hello"
d=123
echo $a
echo $b
echo $c
echo $d
echo "Hello, value of d is $d"
echo
echo
declare -i e=123 #e is an integer
declare -r f=999 #f is read-only
declare -l g="king kong" #g is in lower case
declare -u h="Hello World" #h is in upper case
echo $e
echo $f
echo $g
echo $h
[scott]$ sh sample.sh
Hello
Hello
Hello
123
Hello, value of d is 123

123
999
king kong
HELLO WORLD
[scott]$
Built-in Variables
[scott]$ echo $HOME
/u02/test/abc
[scott]$ echo $PWD
/u04/ftp/scott
[scott]$ echo $MACHTYPE
x86_64-redhat-linux-gnu
[scott]$ echo $HOSTNAME
abc12345667890
[scott]$ echo $BASH_VERSION
4.1.2(2)-release
[scott]$ echo $SECONDS
8010
[scott]$ echo $0
-bash
[scott]$

echo in UNIX

 [scott]$ vi sample.sh
[scott]$ cat sample.sh
#!/bin/bash
#This is basic bash script exmaple.
name="Raj"
echo Hello $name, country\(ind\)!
echo 'Hello $name, country(IND)!'
echo "Hello $name,country(IND)!"
[scott]$ sh sample.sh
Hello Raj, country(ind)!
Hello $name, country(IND)!
Hello Raj,country(IND)!
[scott]$

Tilde and Brace usage in UNIX

 [test scott]$ pwd
/u04/ftp/orcl/scott
[test scott]$ cd ..
[test orcl]$ echo ~-
/u04/ftp/orcl/scott
[test orcl]$ pwd
/u04/ftp/orcl
[test orcl]$ touch {apple,grape,banana}
[test orcl]$ ls
apple  banana  grape  scott/
[test orcl]$ ls -l
total 2
-rw-rw-r--  1 apps apps  0 Nov  4 06:21 apple
-rw-rw-r--  1 apps apps  0 Nov  4 06:21 banana
-rw-rw-r--  1 apps apps  0 Nov  4 06:21 grape
drwxrwxrwx 13 apps root  14 Nov  3 10:56 scott/
[test orcl]$ touch file_{1..6}
[test orcl]$ ls -l
total 2
-rw-rw-r--  1 apps apps  0 Nov  4 06:21 apple
-rw-rw-r--  1 apps apps  0 Nov  4 06:21 banana
-rw-rw-r--  1 apps apps  0 Nov  4 06:22 file_1
-rw-rw-r--  1 apps apps  0 Nov  4 06:22 file_2
-rw-rw-r--  1 apps apps  0 Nov  4 06:22 file_3
-rw-rw-r--  1 apps apps  0 Nov  4 06:22 file_4
-rw-rw-r--  1 apps apps  0 Nov  4 06:22 file_5
-rw-rw-r--  1 apps apps  0 Nov  4 06:22 file_6
-rw-rw-r--  1 apps apps  0 Nov  4 06:21 grape
drwxrwxrwx 13 apps root  14 Nov  3 10:56 scott/
[test orcl]$ ls -l
total 2
drwxrwxrwx 13 apps root 14 Nov  3 10:56 scott/
[test orcl]$ ls -l
total 2
drwxrwxrwx 13 apps root 14 Nov  3 10:56 scott/
[test orcl]$ ls
scott/
[test orcl]$ pwd
/u04/ftp/orcl
You have mail in /var/spool/mail/apps
[test orcl]$ cd scott
[test scott]$
[test scott]$
[test scott]$ clear
[test scott]$ pwd
/u04/ftp/orcl/scott
[test scott]$ cd ..
[test orcl]$ echo ~-
/u04/ftp/orcl/scott
[test orcl]$ pwd
/u04/ftp/orcl
[test orcl]$ ls
scott/
[test orcl]$ ls -l
total 2
drwxrwxrwx 13 apps root 14 Nov  3 10:56 scott/
[test orcl]$ touch {apple banana cherry}
[test orcl]$ ls -l
total 2
-rw-rw-r--  1 apps apps  0 Nov  4 06:31 {apple
-rw-rw-r--  1 apps apps  0 Nov  4 06:31 banana
-rw-rw-r--  1 apps apps  0 Nov  4 06:31 cherry}
drwxrwxrwx 13 apps root  14 Nov  3 10:56 scott/
[test orcl]$ toych file_{1..5}
-bash: toych: command not found
[test orcl]$ ls -l
total 2
-rw-rw-r--  1 apps apps  0 Nov  4 06:31 {apple
-rw-rw-r--  1 apps apps  0 Nov  4 06:31 banana
-rw-rw-r--  1 apps apps  0 Nov  4 06:31 cherry}
drwxrwxrwx 13 apps root  14 Nov  3 10:56 scott/
[test orcl]$ echo {1..10..2}
1 3 5 7 9
[test orcl]$ echo {1..10..3}
1 4 7 10
[test orcl]$ 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
[test orcl]$ 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
[test orcl]$ echo {0..9}
0 1 2 3 4 5 6 7 8 9
[test orcl]$ echo {0..21}
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
[test orcl]$ echo {a..Z}
a ` _ ^ ]  [ Z
[test orcl]$ echo {m..a..2}
m k i g e c a
[test orcl]$ touch {apple,banana}_{0..2}{a..b}
You have mail in /var/spool/mail/apps
[test orcl]$ ls
apple_0a  apple_0b  apple_1a  apple_1b  apple_2a  apple_2b  banana_0a  banana_0b  banana_1a  banana_1b  banana_2a  banana_2b  scott/
[test orcl]$

Tuesday, November 3, 2020

copy file names from UNIX server to Oracle Database

1. connect to scott user and create below table

 CREATE TABLE SFTP_FILE_NAMES (FILE_NAME VARCHAR2(100),PROCESSED_ON DATE);

2. assume /u04/ftp/abcdev/abc/scripts/file_list.20201102 contains below files

sftp> cd /test
sftp> ls -l 20201102*
-rwx------    0 0        0          112255 Nov  2 01:04 20201102_orcl_result1.xml
-rwx------    0 0        0          112255 Nov  2 05:09 20201102_orcl_result2.xml
-rwx------    0 0        0          181669 Nov  2 09:03 20201102_orcl_result3.xml
-rwx------    0 0        0          166850 Nov  2 13:05 20201102_orcl_result4.xml
-rwx------    0 0        0          117372 Nov  2 17:01 20201102_orcl_result5.xml
-rwx------    0 0        0           35570 Nov  2 22:01 20201102_orcl_result50.xml
-rwx------    0 0        0           77009 Nov  2 21:03 20201102_orcl_result6.xml
sftp> 

3. create load_files2db.sh to copy the files from unix to oracle database

#!/bin/bash
export ORACLE_HOME=/u02/test/abc/instantclient/instantclient_11_2
export TNS_ADMIN=/u02/test/abc/instantclient/instantclient_11_2/network/admin
export LD_LIBRARY_PATH=$ORACLE_HOME
export PATH=$PATH:$ORACLE_HOME/bin
DATETIME=`date +%d-%^h-%y`
CTLDIR=/u04/ftp/abcdev/ac/inactive_ctls
LOGDIR=/u04/ftp/abcdev/abc/inactive_logs
BADDIR=/u04/ftp/abcdev/abc/inactive_bads
ctlfile=$CTLDIR/$DATETIME".ctl"
logfile=$LOGDIR/$DATETIME".log"
badfile=$BADDIR/$DATETIME".bad"
FILENAMES=/u04/ftp/abcdev/abc/scripts/file_list.`date -d "1 days ago" -u +%Y%m%d`
echo "LOAD DATA" > $ctlfile
echo "INFILE *" >> $ctlfile
echo "BADFILE '$badfile'" >> $ctlfile
echo "APPEND" >> $ctlfile
echo "INTO TABLE SCOTT.SFTP_FILE_NAMES" >> $ctlfile
echo "FIELDS TERMINATED BY ','" >> $ctlfile
echo "(" >> $ctlfile
echo "FILE_NAME CHAR(100)," >> $ctlfile
echo "PROCESSED_ON DATE" >> $ctlfile
echo ")" >> $ctlfile
echo "BEGINDATA" >> $ctlfile
if [ $(grep -m 1 '\.xml' $FILENAMES|awk '{print $NF}') ];
then
for line in $(grep '\.xml' $FILENAMES|awk '{print $NF}')
do
echo `echo $line|tr -d '\r'`","$DATETIME >> $ctlfile
done
else
echo ""","$DATETIME >> $ctlfile
fi
$ORACLE_HOME/bin/sqlldr control=$ctlfile userid=SCOTT/tiger@ORCL log=$logfile
exit

4. run the script using the command -  ./ load_files2db.sh or sh load_files2db.sh

5. connect to scott user and verify the results 

SELECT * FROM SCOTT.SFTP_FILE_NAMES;

FILE_NAME        PROCESSED_ON
----------------------------------------------------
20201102_orcl_result1.xml 03-NOV-20
20201102_orcl_result2.xml 03-NOV-20
20201102_orcl_result3.xml 03-NOV-20
20201102_orcl_result4.xml 03-NOV-20
20201102_orcl_result5.xml 03-NOV-20
20201102_orcl_result50.xml 03-NOV-20
20201102_orcl_result6.xml 03-NOV-20

Monday, November 2, 2020

echo and read commands in UNIX

 [oracle]$ echo "Hello World"
Hello World
[oracle]$ read
Hi
[oracle]$ echo "Hello";echo "World"
Hello
World
[oracle]$ echo "what is your name?";read name;echo "Hello $name"
what is your name?
Raj
Hello Raj
[oracle]$ echo "what is your name?";read name;echo "Hello $name";echo "Press Enter to continue";read
what is your name?
Raj
Hello Raj
Press Enter to continue
[oracle]$

$ echo "One"
One
$ echo "one" > output.txt
$ cat output.txt
one
$ echo "one" > output.txt
$ cat output.txt
one
$ echo "one" >> output.txt
$ cat output.txt
one
one
$ echo "one" >> output.txt
$ cat output.txt
one
one
one
$

sed command in unix

$ vi sample.txt
$ cat sample.txt
one two three
this is example for sed substitution
$ cat sample.txt|sed 's/t/T/'
one Two three
This is example for sed substitution
$ cat sample.txt|sed 's/t/T/g'
one Two Three
This is example for sed subsTiTuTion
$ sed 's/t/T/g' sample.txt
one Two Three
This is example for sed subsTiTuTion
$ cat sample.txt
one two three
this is example for sed substitution
$ sed -i 's/t/T/g' sample.txt
$ cat sample.txt
one Two Three
This is example for sed subsTiTuTion
$
**************************************************************************
[oracle]$ cat sample.txt
one two three
10 aug 1991
5 may 2020
sed command is used to replace string or characters
[oracle]$ sed 's/[0-9]/*/g' sample.txt
one two three
** aug ****
* may ****
sed command is used to replace string or characters
[oracle]$ sed 's/[0-9][0-9]/*/g' sample.txt
one two three
* aug **
5 may **
sed command is used to replace string or characters
[oracle]$ sed 's/[0-9]/(&)/g' sample.txt
one two three
(1)(0) aug (1)(9)(9)(1)
(5) may (2)(0)(2)(0)
sed command is used to replace string or characters
[oracle]$ sed 's/[a-z]/(&)/g' sample.txt
(o)(n)(e) (t)(w)(o) (t)(h)(r)(e)(e)
10 (a)(u)(g) 1991
5 (m)(a)(y) 2020
(s)(e)(d) (c)(o)(m)(m)(a)(n)(d) (i)(s) (u)(s)(e)(d) (t)(o) (r)(e)(p)(l)(a)(c)(e) (s)(t)(r)(i)(n)(g) (o)(r) (c)(h)(a)(r)(a)(c)(t)(e)(r)(s)
[oracle]$ sed -i  's/t/T/g' sample.txt
[oracle]$ cat sample.txt
one Two Three
10 aug 1991
5 may 2020
sed command is used To replace sTring or characTers
[oracle]$ sed 's/[a-z]/*/g' sample.txt
*** T** T****
10 *** 1991
5 *** 2020
*** ******* ** **** T* ******* *T**** ** ******T***
[oracle]$ sed 's/[A-Z]/*/g' sample.txt
one *wo *hree
10 aug 1991
5 may 2020
sed command is used *o replace s*ring or charac*ers
[oracle]$ sed 's/[A-Za-z]/*/g' sample.txt
*** *** *****
10 *** 1991
5 *** 2020
*** ******* ** **** ** ******* ****** ** **********
[oracle]$ sed 's/[a-Z]/*/g' sample.txt
*** *** *****
10 *** 1991
5 *** 2020
*** ******* ** **** ** ******* ****** ** **********
[oracle]$ sed 's/[0-Z]/*/g' sample.txt
*** *** *****
** *** ****
* *** ****
*** ******* ** **** ** ******* ****** ** **********
You have mail in /var/spool/mail/ougbs
[oracle]$ sed 's/[0-5]/*/g' sample.txt
one Two Three
** aug *99*
* may ****
sed command is used To replace sTring or characTers
[oracle]$ sed 's/[d-m]/*/g' sample.txt
on* Two T*r**
10 au* 1991
5 *ay 2020
s** co**an* *s us** To r*p*ac* sTr*n* or c*aracT*rs
[oracle]$
*********************************************************************

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...