Showing posts with label korn shell tutorials. Show all posts
Showing posts with label korn shell tutorials. Show all posts

Why grep is used? options of grep

Why grep is used? options of grep
Search for the occurence of a pattern in a file: grep 'pattern' file. If one just wants to know how often soemthing occurs in a file, then: grep -c 'pattern file. This can be used in a script like:
if [[ $(grep -c 'pattern' file) != 0 ]];then ......;fi. The condition is fullfilled if the pattern was found.
Grep Options
SYNTAX
grep "Search String" [filename]
grep [-e pattern] [file...]
grep [-f file] [file...]

Korn shell Tutorials :Read input from user,Files

Korn shell Tutorials :Read input from user,Files
Read in a Variable
From a user we read with: read var. Then the users can type something in. One should first print something like: print -n "Enter your favorite haircolor: ";read var; print "". The -n suppresses the newline sign.
Read into a File Line for Line
To get each line of a file into a variable iteratively do:
{ while read myline;do
# process $myline
done } < filename
To catch the output of a pipeline each line at a time in a variable use:
last |sort
{
while read myline;do
# commands
done }

Korn shell Tutorials :Pipes and Coprocesses

Korn shell Tutorials :Pipes and Coprocesses
PIPES
For a serial processing of data from one command to the next do:
command1|command2|command3 ...
e.g. last |awk '{print $1}' |sort -u.
Coprocesses
One can have one background process with which one can comunicate with read -p and print -p. It is started with command |&. If one uses: ksh & then this shell in the background will do everything for us even telnet and so on: print -p "telnet hostname".

Korn shell Tutorials :Data Redirection

Korn shell Tutorials :Data Redirection
General
Data redirection is done with the follwoing signs: > >> < <<. Every program has at least a
standardinput, standardoutput and standarderroroutput. All of these can be redirected.
Command Output to File
For writing into a new file or for overwriting a file do: command > file
For appending to a file do: command >> file
Standard Error Redirection
To redirect the error output of a command do: command 2> file
To discard the error alltogether do: command 2>/dev/null
To put the error to the same location as the normal output do: command 2>&1
File into Command
If a program needs a file for input over standard input do: command < file
Combine Input and Output Redirection
command < infile > outfile
command < infile > outfile 2>/dev/null
Commands into Program ( Here Document )
Every unix command can take it's commands from a text like listing with:
command EOF
input1
input2
input3
EOF
From eof to eof all is feeded into the above mentioned command.

Korn shell Tutorials :Writing Functions

Korn shell Tutorials :Writing Functions
Description
A function (= procedure) must be defined before it is called, because ksh is interpreted at run time.
It knows all the variables from the calling shell except the commandline arguments. But has it's
own command line arguments so that one can call it with different values from different places in
the script. It has an exit status but cannot return a value like a c funcition can.
Making a Function
One can make one in either of the following two ways:
function foo {
# commands...
}
foo(){
# commands...
}
Calling the Function
To call it just put it's name in the script: foo. To give it arguments do: foo arg1 arg2 ...
The arguments are there in the form of $1...$n and $* for all at once like in the main code.
And the main $1 is not influenced bye the $1 of a particular function.
Return
The return statement exits the function imediately with the specified return value as an exit status.

Korn shell Tutorials :Regular Expressions

Korn shell Tutorials :Regular Expressions
Ksh has it's own regular expressions.
Use an * for any string. So to get all the files ending it .c use *.c.
A single character is represented with a ?. So all the files starting with any sign followed bye 44.f can be fetched by: ?44.f.
Especially in ksh there are quantifiers for whole patterns:
?(pattern) matches zero or one times the pattern.
*(pattern) matches any time the pattern.
+(pattern) matches one or more time the pattern.
@(pattern) matches one time the pattern.
!(pattern) matches string without the pattern.
So one can question a string in a variable like: if [[ $var = fo@(?4*67).c ]];then ...

Korn shell Tutorials :Manipulating the variables

Korn shell Tutorials :Manipulating the variables
Removing something from a variable
Variables that contain a path can very easily be stripped of it: ${name##*/} gives you just the filename.
Or if one wants the path: ${name%/*}. % takes it away from the left and # from the right.
%% and ## take the longest possibility while % and # just take the shortest one.
Replacing a variable if it does not yet exits
If we wanted $foo or if not set 4 then: ${foo:-4} but it still remains unset. To change that we use:
${foo:=4}
Exiting and stating something if variable is not set
This is very important if our program relays on a certain vaiable: ${foo:?"foo not set!"}
Just check for the variable
${foo:+1} gives one if $foo is set, otherwise nothing.