Sie sind auf Seite 1von 34

UNIX BASICS

TIBCO Software Inc.

This document (including, without limitation, any product roadmap or statement of direction data) illustrates the planned testing, release and availability dates for TIBCO products and services. This document is provided for informational purposes only and its contents are subject to change
without notice. TIBCO makes no warranties, express or implied, in or relating to this document or any information in it, including, without limitation, that this document, or any information in it, is error-free or meets any conditions of merchantability or fitness for a particular purpose. This
document may not be reproduced or transmitted in any form or by any means without our prior written permission.

CONTENTS

Introduction
Unix File System

Basic Commands

Wild Cards and Regular Expression


Variables
Special Characters
Command Line Arguments
Command Substitution
I/O Redirection
Pipes
Logical/Relational/Arithmetic Operators
IF Then Else Construct
Looping and Branching

Introduction UNIX Architecture

Introduction
A shell is a program that takes commands typed by the
user and calls the operating system to run those

Commands
Shell script is a list of Commands stored in executable
file

Introduction
My First Script

#!/usr/bin/ksh
## This is a comment
echo " Hello World"
echo " today the date is `date`"
echo " Bye for now"
exit 0

Unix File System


Everything in Unix is considered to be a file, including
physical devices such as simple files , directories, DVDROMs, USB devices, floppy drives, and so forth
Unix uses a hierarchical file system structure, much like
an upside-down tree, with root (/) at the base of the file
system and all other directories spreading from there

Unix File System

Unix Directory Definition

Unix File System


o File Access Permission specifies the Access Permission for Owner ,
group, and Others
o File Access Permission can be controled using the Command chmod
o chmod Owner( Perm )group(Perm ) Others(Perm)
o chmod can be set using numbers or letters
o chmod u+a file add all permissions to owner
o chmod go-a file remove all permissions to group and other
o chmod +r file add read permissiosn to everyone (u, g, o)

Unix File System


File Access Permission specifies the Access
Permission for Owner , group, and Others

Basic commands

Basic commands

Basic Commands

Basic Commands
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o

sort n
Sort t | k2 filename
(sort -k 2,2 infile) sorts the contents of infile with the second field as the sort key
-o option is used to output result to a diff file
find /opt/scripts/prod -ctime +100 \( -name "*.log" -o -name "*.msg" \) -exec rm {} \; -print
grep "/bin/bash" /etc/passwd
grep "[hH][aA][Mm]" /etc/passwd
Grep c abc Filename
Grep v abc filename
cut -d |' -f 2-7
cut -c 1-5 a.dat
date1=`date "+%Y%m%d"`
date2=`date "+%Y%m%d%H%M%S"`
date3=`date "+%Y-%m-%d"`
date4=`date "+%A"`
date5=`date "+a"`
Record_count=`wc -l $file_name | tr -s ' ' | cut -d' ' -f2`
ls filename* |cut -c 1-10
Value=`expr $count+10`
Value=`expr string _numeral`
value=`expr $some_count \* 100 / $devider_count`

grep Command
The grep utility is used to search for generalized regular expressions occurring in Unix files
Syntax
grep [options] regexp [file[s]]

Common Options
-i ignore case
-c report only a count of the number of lines containing matches, not the
matches themselves
-v invert the search, displaying only lines that do not match
-n display the line number along with the line on which a match was found
-s work silently, reporting only the final status:
0, for match(es) found
1, for no matches
2, for errors
-l list filenames, but not lines, in which matches were found

grep Command

cat num.list
1 15 fifteen
2 14 fourteen
3 13 thirteen
4 12 twelve
5 11 eleven
6 10 ten
7 9 nine
8 8 eight
9 7 seven
10 6 six
11 5 five
12 4 four
13 3 three
14 2 two
15 1 one

grep Command
o
o
o
o
o
o
o
o

grep '15' num.list


grep -c '15' num.list
grep '1[125]' num.list
grep '^ ' num.list ( all lines that begin with a space)
grep -v abc ' num.list
grep 'te*' num.list
who | grep '^l
Cat filename | grep Pattern > outputfile

Wild Cards
o Wildcards are the shell's way of abbreviating filenames
o A number of characters are interpreted by the Unix shell
before any other action takes place. These characters
are known as wildcard characters. Usually these
characters are used in place of filenames or directory
names

Regular Expression
o A regular expression is a set of characters that specify a
pattern
o Regular expressions are used when you want to search
for specify lines of text containing a particular pattern

VARIABLE
o A variable is a character string to which we assign a
value. The value assigned could be a number, text,
filename, device, or any other type of data
o A variable is nothing more than a pointer to the actual
data. The shell enables you to create, assign, and delete
variables.
o The name of a variable can contain only letters ( a to z or
A to Z), numbers ( 0 to 9) or the underscore character
( _).

Variable declaration syntax

Vname=v_value
Vname=v_value$Vname
Vname= my name is $Vname
Vname=`any regular unix command`
Vname=`expr v_value`
Vname=`expr $a + $b`
Can be declared in a parameter file outside the Program

o
o

A single quote (') turns off special meaning of all


characters until the next single quote is found
Double quotes (") work almost like single quotes. The
difference is that double quoting allows the characters $
(dollar sign), ` (backquote), and \ (backslash) to keep
their special meanings
A backslash (\) turns off special meaning (if any) of the
next character. For example, \* is a literal asterisk, not a
filename wildcard

o
o

expr 79 \* 45
In the above example \* is a literal asterisk, not a
filename wildcard.
echo Hey! What's next? Mike's #1 friend has $$
echo "Hey! What's next? Mike's #1 friend has $$."

Special Character
o Any Character which has a meaning beyond its literal
meaning is called as Special characters
o #,?,* , >>,> ,/ etc are all special characters
o Back slash (\) is used to disable the special meaning of
special character and instead it will be treated as a literal
text character

Command Line Arguments


o command line argument is an argument sent to a
program being called
o Starts with $0 and increment $1,$2 and so on
o $0 is the name of the program
o $1 is the first argument specified while calling the script
o $# contains the total no of Arguments
o $* contains all the arguments in a string

Command Substitution
o Executing the Command and assign the output to a
variable
o Use back quotes(`) for Command substitution
o V_date=`date`
o lineCount=`wc -l $fileName | cut -c1-8`

I/O Redirection
o Most of the Commands take input from STDIN , Output the result to
STDOUT and output the errors To STD ERR
o STDIN is the standard input device(default ) and normally from the
keyboard, also it is designated as 0
o STDOUT is the standard output device (default) and normally it is the
monitor and is designated as 1
o STDERR is the Standard error output device (default) and normally it is
the monitor and is designated as 2
o < is used as input Redirection
o > is used as output Redirection
o >> is used to Append the Existing output file
o 2>/dev/null (Garbage Collector)

o /bin/mailx -s "Hi this is a mail funciton " hanumantha.raju@target.com <


Test_Mail.txt
o cat > filename
This is some text that I want in this file
^D
o echo "this is a new line" >>exsisting_file
o db2 "import from /dev/null of del replace into ADW.TEMP" >> ${logfile} 2>&1
o cat file2 >> file1
o cat file1 file2 > file3
o bad_command >>filename 2>&1 ( output and Error will be sent to the same
place where output goes

PIPES
o Pipes (|) are used to pass the output of one command , as input to
the other command
o command1 | command2
o Record_count=`wc -l Filename | tr -s ' ' | cut -d' ' -f2`

Logical Operators
o Logical Operators evaluate to True or False

if [ -f Filename -a -r Filename ]
then
echo " The File is readable"
else
echo " The File is not Readable "
fi

Relational Operators
o Relational allow you to make comparisons between two
expressions.

Arithmetic Operators

vi math.sh
#!/usr/bin/ksh
count=5
count=`expr $count + 1`
echo $count

If-then -else
if-then-else
if [ expr ] then
command-set-1
else
command-set-2
fi
If-then-else if

if [ expr ] then
command-set-1
elif [ expr]
command-set-2
else
Command-set 3
if

Das könnte Ihnen auch gefallen