Wednesday, November 25, 2009

Brace {} in shell script

- Brace is used for expansion. If several words are separated by commas and surrounded by brace then in case of expansion the word/words before the brace is added to each word/words within brace and the word after the brace is suffixed to each word/words within the brace.

Following is an example of appending "Vision" keyword to each words within brace at first and " always." keyword appends to each words within brace at last.

$ echo 'The '{'Virtual','only'}' Vision. '
The Virtual Vision.  The only Vision.

- With combination of brace and and cat command we can concatenate several files contents into one. Following is an example.

$ cat >file1.txt
one

$ cat >file2.txt
two

$ cat >file3.txt
three

$ cat {file1.txt,file2.txt,file3.txt} >file5.txt

$ cat file5.txt
one
two
three

- We can take facility of brace expansion while move or copy or rename files. For example the following can be used to rename a .txt file to .bak file.

$ cat file5.txt
one
two
three

$ mv file5.{txt,bak}

$ cat file5.bak
one
two
three

- Note that no spaces are allowed within the braces unless the spaces are quoted or escaped.

- With double dot we can use extended brace expansion construction which is a feature introduced in version 3 of Bash.

Example:

$ echo {a..m}
a b c d e f g h i j k l m

$ echo {5..10}
5 6 7 8 9 10


- The {} double curly brackets are a placeholder for output text. For example, curly brackets are placeholder for the path name output by "find".

DELETE_DIR=/home/test/junk
find "$DELETE_DIR" -type f -atime +30 -exec rm {} \;

The above script deletes the files from "/home/test/junk" that have not been accessed in at least 30 days (plus sign ... +30).
where,
-type indicates filetype", and
f = regular file

Thanks & Regards,
Anto Joe Natesh I

Dot (.) in linux and shell script

- Dot(.) is equivalent the source command in shell script and linux command line. From command line we can execute a shell script using dot (.). Within a script, a "source file_name" or ". file_name" loads the file file_name. The command "source" or "." just imports code into the script, appending to the script (it is same like include command in php or #include in c). The net result is the same as if the "sourced" lines of code were physically present in the body of the script. This technique is useful in situations when multiple scripts use a common data file or function library.

Let's assume that my welcome.sh is like below which later included using . within main.sh file.

$ cat >welcome.sh
echo "Welcome to shell script programing"

$ cat >main.sh
. welcome.sh
echo "This is main file"

$ sh main.sh
Welcome to shell script programing
This is main file


- Dot (.) is the part of the hidden file name. A leading dot is the prefix of a "hidden" file.

$ ls
main.sh  newfile  semicolon_test.sh  welcome.sh

$ touch .hidden_file

$ ls
main.sh  newfile  semicolon_test.sh  welcome.sh

$ ls -a
.  ..  .hidden_file  main.sh  newfile  semicolon_test.sh  welcome.sh


In the example ls -a shows the hidden file .hidden_file

- When working with directory, a single dot (.) represents the current working directory, and two dots (..) represent the parent directory.
Example:

$ pwd
/home/test/t_dir

$ cd .

$ pwd
/home/test/t_dir

$ cd ..

$ pwd
/home/test


- While copying files we can use dot (.) to represent current directory.
The following example will copy the file /home/test/f to present working directory.

$ cp /home/test/f .

Thanks & Regards,
Anto Joe Natesh I

Saturday, November 21, 2009

Oracle Database Links (DB Link)



A database link is a path through which a remote user in another database can connect to any other database. Once created the database link exists as an object in the user schema.


Type of DB Links
There are 3 types of DB links. They are as follows:


1. PRIVATE: When the DB links is created, it is created under Private mode as default. The Private DBLINK is only available to the user who has created it. It is not possible for a user to grant access on a private DBLINK to other users.


2. PUBLIC: The Public DBLINK is available to all the users and all users can have the access without any restrictions.


3. SHARED: Shared database link uses share the server connection to support database link connection. If there are multiple concurrent database link access into a remote database, shared database link can be used to reduce the number of server connections required. Without the shared clause each database link connection requires a separate connection to the remote database.


Types of Logins:
In dblink we can use 2 types of login. They are as follows:


1. DEFAULT LOGIN: The User name and Password is same in both the databases.
Syntax
======
CREATE [PUBLIC] DATABASE LINK CONNECT TO CURRENT_USER USING
Code: (Text)
Create public database link daslink connect to current_user using ‘ORCL’


2. EXPLICIT LOGIN: The User Name and Password is different in both the databases.
Syntax
======
CREATE [PUBLIC|SHARED] DATABASE LINK CONNECT TOIDENTIFIED BY USING
Code: (text)
CREATE PUBLIC DATABASE LINK DDLNK CONNECT TO SCOTT IDENTIFIED BY TIGER USING ‘ORCL’


Note: To create the public DBLINK the user must have create public database link system privileges.

Friday, November 13, 2009