Showing posts with label echo. Show all posts
Showing posts with label echo. Show all posts

Saturday, 7 July 2012

Shell script - 2

Shell script - 2
To go to Shell script Session click here
To go to UNIX basic commands click here

Its time to learn operators


 Arithmetic Operator:
           
Symbol
Meaning
+
Addition
-
Minus(subtract)
*
Multiply
/
Divide
%
Modulus
=
Assignment
==
Equal to
!=
Not equal to

In the above the three given in red will be used with comparison and others will be used with expr.

Relation Operators/Comparative operators for numbers/integers:

Operator
Meaning
Usage Example
-eq
Equality condition
[$a –eq $b]
-ne
Not equal condition
[$a –ne $b]
-gt
Greater than condition
[$a –gt $b]
-lt
Less than Condition
[$a –lt $b]
-ge
Greater than equal to condition
[$a –ge $b]
-le
Lesser than equal to condition
[$a –le $b]

Boolean Operators:
Symbol/operator
Meaning
Usage Example
!
Not/Negation
[! [$a –eq $b]] if[$a –eq $b] evaluates to true ! ll make it false and vice versa
-o
OR
[ [$a –eq $b ] –o [-a –eq $c] that is any one condition need to be true
-a
AND
[ [$a –eq $b ] –a [-a –eq $c] that is both condition should be true


Operators used with Strings
Symbol/Operator
Meaning
Usage Example
=
Checks if both strings are equal
[ $str1 = $str2 ]
!=
Checks if both strings are not equal
[ $str1 != $str2 ]
-z
Checks for length is zero
[ $str1 –z $str2 ]
-n
Checks for length is not zero
[ $str1 –n $str2 ]

Files Operators

Symbol/Operator
Meaning
Usage Example
-b file
Checks if the file is block special file
[ -b $filename ]
-c file
Checks if the files is char special file
[ -c $filename ]
-d file
Checks if the file is directory
[ -d $filename ]
-e file
Check for file existence
[ -e $filename ]
-f file
Check if the file is ordinary file
[ -f $filename ]
-g file
Check if the file group id is set
[ -g $filename ]
-p file
Check if the file is a named pipe
[ -p $filename ]
-t file
Check if the file descriptor is open
[ -t $filename ]
-u file
Checks if the files set user id is set
[ -u $filename ]
-r file
Checks if the file is readable
[ -r $filename ]
-s file
Checks if the file size is greater than 0
[ -s $filename ]
-w file
Checks if the file is writable
[ -w $filename ]
-x file
Checks if the file is Executable
[ -x $filename ]

Example 6:
#!/bin/sh
filename=$1
if [-r filename] then
echo "file is readable"
elif [-x filename] then
echo "file is executable"
elif [-w filename] then
echo "file is writable"
else
echo "ERRROR: $filename HAS PERMISSION PROB"
fi


To go to next session click here don't forget to put your feed back as comments which encourages me to do this and you can also post your questions or solve/give answers for others questions, Thanks in advance friends.....




Shell script - 1


Shell script - 1:

          This tutorial will give an over view and a good idea how to write shell script, I think speaking of history like other tutorials is mere waste so I directly starting with details.

If you need any reference or if you are not aware of UNIX basic commands please go through it by clicking here it will be so useful.

#! The she bang

            All shell scripts start with this #! Line this is called she bang it tell the what shell script we are going to use and its location(interpreter location)
            Example 1:
                        #!/bash/sh
            For further reference refer http://en.wikipedia.org/wiki/Shebang_(Unix)

# Comment
            The # is used for commenting the a line
            Example 2:
                        #Adding two numbers

Variables:
            Variable name can contain only, (a to z, A to Z, 0 to 9 and _)


Defining and Assigning values:
            An variable can be defined and assigned as follows
                        Var1=10
                        Var2=”jai”

Variable Types:

When a shell is running, three main types of variables are present:

Local Variables: A local variable is a variable that is present within the current instance of the shell. It is not available to programs that are started by the shell. They are set at command prompt.

Environment Variables: An environment variable is a variable that is available to any child process of the shell. Some programs need environment variables in order to function correctly. Usually a shell script defines only those environment variables that are needed by the programs that it runs.

Shell Variables: A shell variable is a special variable that is set by the shell and is required by the shell in order to function correctly. Some of these variables are environment variables whereas others are local variables.

Command line arguments and some special symbols with $

Example program 3:
#!/bin/sh
echo "file name: $0"
echo "first argument: $1"
echo "Second argument: $2"
echo "Number of arguments: $#"
echo "All the arguments individually double quoted: $@"
echo "Process number of previous command:$!"
echo "Process number of CURRENT SHELL: $$"
echo "All the arguments double quoted: $*"



Out put:
Testhome/l685360# ./test jaya chandran
file name: ./test
first argument: jaya
Second argument: chandran
Number of arguments: 2
Given argument is: jaya chandran
Process number of previous command:
Process number of CURRENT SHELL: 20775420
All the arguments: jaya chandran
Testhome/l685360#

Arrays:

Example 4:
#!/bin/sh
name[0]="jayachandran"
name[1]="Mohan"
name[2]="Devi"
name[3]="Shiva"
name[4]="Ranjith"
echo ${name[@]}
echo ${name[*]}
Output
Testhome/l685360# ./test
Mohan Devi Shiva Ranjith
Mohan Devi Shiva Ranjith
Testhome/l685360#

Arithmetic and Relation operators:
The bourne shell does not do any arithmetic it uses external programs expr and awk to the arithmetic

expr
Example 5:
#!/bin/sh
val1=2
val1=`expr $val1 + 10`
echo "value of val1 aft adding 10 is : $val1"
            Out put:
                        Testhome/l685360# ./test
value of val1 aft adding 10 is : 12
Testhome/l685360#

In the above program we can see usage of expr
1.                          Any arithmetic expression with symbols should be separated by space.( $val1 + 10 )
2.                          Any expr should be given in between `` only.

To go to next session click here don't forget to put your feed back as comments which encourages me to do this and you can also post your questions or solve/give answers for others questions, Thanks in advance friends.....