0% found this document useful (0 votes)
14 views16 pages

Bca5semrec 1

The document contains a certificate for completing a UNIX programming lab along with programs written in Shell scripting and PHP. It includes 12 Shell scripts demonstrating arithmetic operations, file permissions etc. and 12 PHP programs on data types, loops, arrays, classes, cookies and sessions.

Uploaded by

Thomas Shelby
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views16 pages

Bca5semrec 1

The document contains a certificate for completing a UNIX programming lab along with programs written in Shell scripting and PHP. It includes 12 Shell scripts demonstrating arithmetic operations, file permissions etc. and 12 PHP programs on data types, loops, arrays, classes, cookies and sessions.

Uploaded by

Thomas Shelby
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

NIZAM COLLEGE

(Autonomous)
Osmania University, Hyderabad.

CERTIFICATE

Department of Informatics

This is to certify Mr/Ms_____________________ _____________bearing


Roll No _________________ ___student of BCA V Semester has completed
UNIX Programming Lab Record work during the year
2021-2022.

Internal Examiner External Examiner

Head of the Department

1
Index

Shell Scripts:
1) Shell Script To Demonstrate The Arithmetic Operations.

2) Shell Script To Test The Type Of A File.

3) Shell Script To Copy A File

4) Shell Script To Set File Permissions .

5) Display the no. of lines in each of text file in a given dir

PHP Programs:
6) Program on data types

7) Program on while, do while and for loops

8) Programs on array, associative array, and multi-dimensional

array.

9) Program on functions and functions with arguments

10) Program on class and object

11) Program to set cookie

12) Program on session tracking

2
SHELL SCRIPTING

Shell scripts allow input/output, manipulation of variables, and a


powerful flow-of-control and iteration constructs for programming.

➢ To create a shell script, you need to write it in a text file using an


editor like vi.

Ex: vi filename.sh

➢ we can use a sequence of commands to write the shell programs.

➢ We can execute the script file in either of the ways of follows:


1) sh filename.sh
2) chmod u+x filename
3) ./filename

3
1) Shell Script To Demonstrate The Arithmetic Operations.

echo "enter any two numbers"


read a
read b

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.

echo "enter any file name"


read fname

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 .

echo "enter file name"


read fname

echo "enter user type (u/g/o)"


read utype

echo "enter the operation (+ or -)"


read opr

echo "enter file permission (r/w/x)"


read per

echo "before changing:"


ls -l $fname

chmod $utype$opr$per $fname

echo "after changing permission:"


ls -l $fname

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

echo "Enter Dir name"


read dirn
list=`ls $dirn`
for fn in $list
do
cn=`wc -l $dirn'/'$fn`
echo "No of lines in $fn is $cn"
done

output:

8
PHP Programs

6. Program on data types


<!--PHP String-->
<?php
$a = "Hello World!";
$b = 'Hello World!';
echo $a;
echo "<br>";
echo $b;
?>
<!--PHP Integer-->
<?php
$c = 767;
echo "<br>";
var_dump($c);
?>
<!--PHP Float-->
<?php
$d = 14.763;
echo "<br>";
var_dump($d);
?>
<!--PHP Boolean-->
<?php
echo "<br>";
$a = true;
$b = false;
echo $a;
echo "<br>";
echo $b;
?>
<!--PHP Object-->
<?php
class Student {
function Student() {
$this->name = "John";
}
9
}
// create an object
$Daniel = new Student();
// show object properties
echo "<br>";
echo $Daniel->name; ?>

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:

I like Volvo, BMW and Toyota.Sam is 32 years old.V: In stock: o, sold: l.


B: In stock: M, sold: W.
T: In stock: o, sold: y.

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:

Cookie 'user' is set!


Value is: Smith Joe

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:

Session variables are set.

16

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy