Bca5semrec 1
Bca5semrec 1
(Autonomous)
Osmania University, Hyderabad.
CERTIFICATE
Department of Informatics
1
Index
Shell Scripts:
1) Shell Script To Demonstrate The Arithmetic Operations.
PHP Programs:
6) Program on data types
array.
2
SHELL SCRIPTING
Ex: vi filename.sh
3
1) Shell Script To Demonstrate The Arithmetic Operations.
sum=`expr $a + $b`
echo "sum of $a and $b is : $sum"
diff=`expr $a - $b`
echo "difference of $a and $b : $diff"
prdt=`expr $a \* $b`
echo "product of $a and $b : $prdt"
quot=`expr $a / $b`
echo "quotient of $a and $b : $quot"
rem=`expr $a % $b`
echo "remainder when $a is divided by $b : $rem"
Output:
enter any two numbers
10
5
sum of 10 and 5 is : 15
difference of 10 and 5 : 5
product of 10 and 5 : 50
quotient of 10 and 5 : 2
remainder when 10 is divided by 5 : 0
4
2) Shell Script To Test The Type Of A File.
if [ -f $fname ]
then
echo "regular file"
if [ -r $fname ]
then
echo "readable file"
fi
if [ -w $fname ]
then
echo "writable file"
fi
if [ -x $fname ]
then
echo "executable file"
fi
elif [ -d $fname ]
then
echo "directory file"
else
echo "fname not found "
fi
Output:
enter any file name
max.sh
regular file
readable file
writable file
5
3) Shell Script To Copy A File .
if [ $# -eq 0 ]
then
echo "enter source file"
read sfile
echo "enter destination file"
read dfile
cp $sfile $dfile
echo "file is copied"
fi
if [ $# -eq 1 ]
then
echo "enter destination file"
read dfile
cp $1 $dfile
echo "file is copied"
fi
if [ $# -eq 2 ]
then
cp $1 $2
echo "file is copied"
fi
Output:
enter source file
f1
enter destination file
f2
file is copied
6
4) Shell Script To Set File Permissions .
Output:
enter file name
odd.sh
enter user type (u/g/o)
g
enter the operation (+ or -)
+
enter file permission (r/w/x)
w
before changing:
-rw-r--r-- 1 bca2 bca2 136 May 6 13:44 odd.sh
after changing permission:
-rw-rw-r-- 1 bca2 bca2 136 May 6 13:44 odd.sh
7
5) Display the no. of lines in each of text file in a given dir
output:
8
PHP Programs
Output:
Hello World!
Hello World!
int(767)
float(14.763)
1
John
10
7. Program on while, do while and for loops
<?php
$a = 3;
while($a <= 5) {
echo "The number is: $a <br>";
$a++;
}
?>
<?php
$a = 3;
do {
echo "The number is: $a <br>";
$a++;
} while ($a <= 5);
?>
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
Output:
The number is: 3
The number is: 4
The number is: 5
The number is: 3
The number is: 4
The number is: 5
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10
11
8)Programs on array, associative array, and multi-dimensional array
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
<?php
$age = array("Sam"=>"32", "Ben"=>"37", "Josh"=>"41");
echo "Sam is " . $age['Sam'] . " years old.";
?>
<?php
echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";
echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br>";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";
?>
Output:
12
9)Program on functions and functions with arguments
<?php
function writeMsg() {
echo "Hello Friends!";
}
writeMsg(); // call the function
?>
<?php
function familyName($fname) {
echo "$fname Jonas.<br>";
}
familyName("Johny");
familyName("Alice");
familyName("Stanley");
familyName("Jim");
familyName("Sandy");
?>
Output:
Hello Friends!Johny Jonas.
Alice Jonas.
Stanley Jonas.
Jim Jonas.
Sandy Jonas.
13
10)Program on class and object
<html
<head><title>Books</title> </head>
<body>
<p>
<?php
class Books{
public function name(){
print "<b>Welcome to my books page <br />";
}
public function price(){
print "<b>500 rs <br /> <br />";
}
}
$obj = new Books();
echo $obj->name();
echo $obj->price();
?>
</p>
</body>
Output:
Welcome to my books page
500 rs
14
11)Program to set cookie
<?php
$cookie_name = "user";
$cookie_value = "Smith Joe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
//86400 = 1 day
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" .$cookie_name . "' is not set!";
} else {
echo "Cookie '" .$cookie_name . "' is set!<br>";
echo "Value is: " .$_COOKIE[$cookie_name];
}
?>
</body>
</html>
Output:
15
12)Program on session tracking
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Set session variables
$_SESSION["favcolor"] = "blue";
$_SESSION["favanimal"] = "dog";
echo "Session variables are set.";
?>
</body>
</html>
Output:
16