Why do we Use Shell Scripting
We use Shell Scripts to obtain the following purposes.
1. Customizing your work environment.
2. Automating Your Daily Task.
3.Automating Repetitive Task.
4.Executing Important Procedures like shutting down the system,formatting a disk,Creating a file system on it,mounting the file system,letting the users use the floppy and finally un mounting the disk.
5.Performing the same operation on many files.
When Not to use Shell Scripting :
- Is too complex,such as writing an entire billing system.
- Requires a high degree of efficiency
- Requires a variety of Software tools.
Usage of Shell Scripts with in Informatica:
1.To run an batch via pmcmd.
2. To have header and footer in case we are goanna write to an flat file
3.To run the command task.
4. To update parameter file with session start time and end time.
============================================================================
Basic Commands Part1
Command | Option | Description | ||||||||||||||||||||||||
1. Uname |
|
| ||||||||||||||||||||||||
2. Man |
|
| ||||||||||||||||||||||||
3.Cal |
|
| ||||||||||||||||||||||||
4. Date |
|
| ||||||||||||||||||||||||
5.Who |
|
| ||||||||||||||||||||||||
6.Passwd |
| |||||||||||||||||||||||||
7.Lock |
|
| ||||||||||||||||||||||||
8.Bc |
| |||||||||||||||||||||||||
9.ls |
|
| ||||||||||||||||||||||||
10.Cat |
|
| ||||||||||||||||||||||||
11.CP |
|
| ||||||||||||||||||||||||
12.rm |
|
| ||||||||||||||||||||||||
13.lp |
| |||||||||||||||||||||||||
14.Split |
|
| ||||||||||||||||||||||||
15. Cmp comm Diff |
| |||||||||||||||||||||||||
16.wc |
|
| ||||||||||||||||||||||||
17.Path | PATH=$PATH:/home/ gagan:/home/deep | Set the path variable for SHELL to use these directory path to locate the executables commands. | ||||||||||||||||||||||||
18.stty |
|
| ||||||||||||||||||||||||
19.Alias |
|
| ||||||||||||||||||||||||
20.History |
|
| ||||||||||||||||||||||||
21. r !! |
|
| ||||||||||||||||||||||||
22.Tilde(~) |
| |||||||||||||||||||||||||
23.cd - |
| |||||||||||||||||||||||||
24.chmod |
|
|
=================================================================================
Basic Commands Part2
Command | Options | Description | ||||||||||||||||
25.Head |
|
| ||||||||||||||||
26.Tail |
|
| ||||||||||||||||
27.$$ |
| |||||||||||||||||
28.ps |
|
| ||||||||||||||||
29.Background job |
|
| ||||||||||||||||
30.nohup |
|
| ||||||||||||||||
31.Kill |
|
| ||||||||||||||||
32.Nice |
|
| ||||||||||||||||
33.At |
|
| ||||||||||||||||
34.Batch |
|
| ||||||||||||||||
35.Cron |
| |||||||||||||||||
36.Finger |
|
| ||||||||||||||||
37.Set |
|
| ||||||||||||||||
38.Shift |
|
| ||||||||||||||||
39.Chown |
|
| ||||||||||||||||
40.Chgrp |
|
| ||||||||||||||||
41.Touch |
|
| ||||||||||||||||
42.Linking in |
|
| ||||||||||||||||
43.df |
|
| ||||||||||||||||
44.du |
|
|
==============================================================================
Zipping Files
Command | Options | Description |
1. gzip | gzip etl_code eg:etl_code.gz | |
gunzip etl_code.gz eg: etl_code | ||
zip file*sql eg: file.zip | ||
unzip file.zip eg:file*sql | ||
2. tar | create backup of files recursively. | |
-cvf eg :tar -cvf /home/gagan/sqlbackup ./*.sql | ||
- x eg:tar-xcvf /home/gagan/sqlbackup ./*.sql | files are restored using -x option. | |
3. Cut | -c <column start and no> eg : cut -c -5,6-12 test file | column (by specifying position). C stands for column cut. |
-f <field start and end no> eg:cut -f 1,5 test file | field (default deliminator tab). F stands for field cut. | |
'-d -f <field start and end no> eg: cut -d "|" -f 1,5 test file | new file | Cut the field b/w 1 and 5 and piped the output to new file. | |
4. sort | sort test file | By default the sorting starts with first character of each line. Priority1.space, tabs 2. numerals 3.uppercase letters 4. Lower case letters. |
-t eg : sort -t "|" +2 test file | Sorting starts from 3rd field skipping 2nd field, overring the default.delimiter to distinguish b/w start and end of field. | |
-r eg : sort -t \| -r +2 test file | Reverse sort starting with 3rd field. | |
eg : sort -t \| +2r test file | The above command can be written in another way. | |
-o eg : sort -o abc.txt abc_sort.txt | save sorted data in file. | |
eg : sort -t "|" +1 -2 +3 <> | Sorting based on different field as in case of order by clause.Sorting starts with 2nd field then with 4th field, -2 indicate to stop the sorting after 2nd field and resume it with 3rd field. | |
-n eg : sort -n <> | Numeric sort. | |
-u eg : sort -u <> | Unique sort. | |
5.paste | -d eg:paste -d "|" <> <> | deliminator. |
6.tr | eg : tr '|\' '~-' <test.txt | translate all | with ~ and \ with - . |
eg : tr '[a-z]' '[A-Z] <test.txt | translate to upper case. | |
-d eg : tr -d '|' test.txt | delete all occurrence of | . | |
7.uniq | Unique require sorted file as input. | |
-u eg : cut -d "|" -f3 <>|sort|uniq -u | remove duplicate. | |
-d eg: cut -d "|" -f3 <>|sort|uniq -d | select only dup records. | |
-c eg : cut -d "|" -f3|sort|uniq -c | duplicate count. | |
8.Changing time stamp | touch mon date hrs mins <file>. | |
ls -lt | time of last modification | touch -m 01290430 <file>. |
ls -lu | time of last access | touch -a 01290415 <file>. |
9.Change Date | date 09181754 | |
10.wall | wall -g dba "hello" | to selectively send msg to dba group. |
11.shutdown | shutdown -g2 | power down after 2 mins. |
shutdown -y -g0 | immediate shutdown. | |
shutdown -y -g0 -i6 | shutdown and reboot (init level 6). | |
shutdown 17:30 | shutdown at 17:30. | |
shutdown -r now | shutdown immediate and reboot. | |
12. du | Disk usage. | |
du /home/expimp/create_db | tree output for each directory inside. | |
du -s //home/expimp/create_db | summary. | |
13.find | find <loc> <option> <pattern> <action> | find in root dir abc in emp.lst file |
'-mtime = mod time eg: find . -mtime -2 -print | find file modi in less then 2 days. | |
'atime = access time eg: find . -atime +365 -print | find the file not accessed in last 1 year. | |
! –newer eg: find / -name "*.pl" ! -newer last_backup -print | file modi before last_backup. | |
-size. eg: find . -size +2048 -print | files greater then x blocks. | |
-a (and) -o (OR) eg: find . \( -name "*.sh" -o -name "*.lst" \) -print | double quotes necessary. | |
-exec eg: find . -atime +180 -ok rm -f {} \; | remove the files which are not modi for last 20 days | |
-ok eg: find . -atime +180 -ok rm -f {} \; | before removing prompt for confirmation. | |
xargs eg: find . -mtime +20 | xargs rm -f | remove all file rm will be executed only once. | |
xargs -n -p –t eg:find . -mtime +20 | xargs -n20 -p -t rm -f | remove at max 20 files in batch and in interactive mode. | |
-type eg: find / -name log -type f -print | f for file and d for directory. | |
-prune eg: find . -name *.log -prune exe –print. | don't descend exe directory. |
=============================================================================
Pattern Searching
Commands | Options | Description |
/ | /Unix | Forward search of Unix keyword in file |
? | ?Unix | Backward search of Unix keyword in file |
n | Repeat the last search | |
. | Repeat the previous command | |
:1, $s/<search string>/<Replace String >/g | Pattern search and replacement. | |
1,$ | Represent all lines in the file. | |
g | Stands for globally | |
:3,10s/gagan/deep/g | search between lines 3 and 10 | |
: .s/gagan/deep/g | Only the current line. | |
: $s/gagan/deep/g | Only the last line. | |
: $s/gagan/deep/gc | c | Ask for confirmation for replacement |
grep | ||
-c | counting occurrence. | |
-n | display line number for record. | |
-v | skip records that contain directory. | |
-l | display files containing record. | |
-i | ignore case. | |
[PQR] | match any single character. | |
[c1-c2] | match char with ASCII range | |
[^PQR] | match single character which is not PQR. | |
^<pat> | beginning with pattern. | |
<pat>$ | ending with pattern. | |
{a-Z A-Z 0-9} | match any single character. | |
ls –l |grep “^d” | Prints only directories. | |
egrep | ||
egrep '[aA]g+[ar][ar]wal' test1.txt | match one or more occurrence matches ag & agg. | |
egrep '[aA]gg?[ar][ar]wal' test1.txt | match zero or one occurrence. | |
egrep prashant|director test1.txt | match eap1 or eap2 (finds prashant or line with director) | |
egrep (das|sen)gupta test1.txt | match exp x1x3 or x2x3 (like dasgupta & sengupta) | |
egrep -f <pattern_file_name> test1.txt | Huge list of pattern search can passed in the form of file name .pattern stored in file eg(prashant|admin|director) | |
+ | Matches one or more occurrence of previous character. | |
? | Matches zero or one occurrence of previous character. | |
Fgrep | fgrep and egrep accepts multiple pattern both form command line and a file but unlike grep and egrep does not accept regular expression. | |
fgrep -f pattern file emp file | Faster than grep and egrep family |
========================================================================
Pattern Matching
Command | Options | Description |
* | Matches any number of character including NONE. * does not match all files beginning with dot <.>. | |
ls -l chap* | Matches all the files which starts with chap. | |
ls -x chap* | Matches all the files which starts with chap and prints in multi column way. | |
? | Matches single character. | |
ls -l chap? | Matches all the files with only 5 character name and should start with chap. | |
[ ijk ] | Matches single character either i or j or k. | |
[ !ijk ] | Matches single character that is not i or j or k | |
[ x - z ] | Matches single character that is not within the ASCII range of character x and z. | |
[ ! x - z ] | Matches single character that is not within the ASCII range of character x and z | |
ls -l chap0[1 - 4] | Range specification is also available. | |
ls -l [ a – z A-Z] | Matches all file names beginning with alphabet irrespective of case. | |
ls -l chap[!0 - 9] | Matches all file names beginning with alphabet chap and not ending with any number. | |
ls *.ist | Print all the files which end with ist extensions. | |
cp chap?? abc | Copy all files starts with chap to abc directory. | |
cmp chap[12] | Compares file chap1 and chap2. | |
mv * ../bin | Moves all the files to bin directory. | |
cat chap[!0 - 9] | Concatenates all the files beginning with chap and not ending with number. | |
ls -l * | * does not match all files beginning with dot <.>. | |
ls -l .???* | The above problem can be solved with specifying first few character using meta character <?> explicitly. | |
Escaping Backslash ( \ ) | Playing with file names which uses meta character in their file name. | |
ls - l chap* | Print all files whose names start with chap but not the one whose name is chap*. | |
ls - l chap\* | The above problem can be solved by escaping the special character. | |
Pipe | | To pass the standard output of one command as the standard input to another. | |
who | wc -l | Output of who command <three users> passed as input to wc which count the number of lines present. | |
Tee | who | tee users list | Tee saves the output of who command in user list as well as display it also. |
Shell Variable | Shell variables are initialized to null value < by default > so it returns null. All words starts with $ are considered as variable unless single quoted or escaped. | |
a=ab, b=cd, z=$a$b. echo $z | shell concatenate two variable. | |
echo '$10' eg: $10 | All words starts with $ are considered as variable unless single quoted or escaped. | |
echo "$10" eg: 0 | Shell is looking for $1 variable which is undefined so passes null for this. $1 is part of positional parameter. |
=============================================================================
No comments:
Post a Comment