0% found this document useful (0 votes)
71 views10 pages

Lab 2 Tasks

1. The document describes 5 tasks for a Linux lab assignment, including practicing Linux commands like cp, mv, and rm, writing C programs and shell scripts, and a program to calculate grades. 2. The student provides responses for most tasks, including the syntax for common Linux commands and running 2 sample C programs to calculate post-increment and pre-increment. 3. For the grade calculation program, the student writes a C program that takes marks for 2 subjects as input and prints the grade earned according to ranges, to simulate the Bahria University grading system.

Uploaded by

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

Lab 2 Tasks

1. The document describes 5 tasks for a Linux lab assignment, including practicing Linux commands like cp, mv, and rm, writing C programs and shell scripts, and a program to calculate grades. 2. The student provides responses for most tasks, including the syntax for common Linux commands and running 2 sample C programs to calculate post-increment and pre-increment. 3. For the grade calculation program, the student writes a C program that takes marks for 2 subjects as input and prints the grade earned according to ranges, to simulate the Bahria University grading system.

Uploaded by

Saqib Aziz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

M.

SAQIB AZIZ (65146) 02-131192-031

Bahria University,
Karachi Campus

LAB NO.2
TASK NO OBJECTIVE
1 Practice all the Linux commands discussed in this lab while taking
assistance using the man command. Write the complete syntax used for
utilizing the cp, mv and rm commands in Linux shell.
2 Write the C programs provided in this lab and generate their outputs
over Linux environment (provide snapshot).
3 Write a C program on the Linux environment that takes your marks as
an input and display your grades accordingly to that followed at Bahria
University. Limit your program to a maximum of five subjects. Use the
suitable logical operator(s), i.e. and (&&), or (||), not (!), if required.
4 Write a shell script to display your address over multiple lines.
5 Write a shell script that would traverse among any three directories that
are placed under the /home directory. While moving from one directory
to another, the script should display the name of the current working
directory and list the content within that directory, including the hidden
files.

Submitted On:
18/03/2021
(Date: DD/MM/YY

LAB 2

LINUX COMMANDS AND SHELL SCRIPTING

OS LAB 2 BSE 4A
[Link] AZIZ (65146) 02-131192-031

Question # 01:
Practice all the Linux commands discussed in this lab while taking assistance using the
man command. Write the complete syntax used for utilizing the cp, mv and rm
commands in Linux shell.

1. CP Command: Copies a file or directory.

2. rm Command: Removes a File or Directory.

3. mv command: moves a file or directory.

4. mkdir command: makes a new directory.

OS LAB 2 BSE 4A
[Link] AZIZ (65146) 02-131192-031

5. rmdir command: removes a directory.

6. whoami command: displays User name.

7. cal command: Shows calendar with current date.

8. sleep command: the machine does nothing for a specific time.

OS LAB 2 BSE 4A
[Link] AZIZ (65146) 02-131192-031

9. pwd command: gives current working directory.

 Question # 02:
Write the C programs provided in this lab and generate their outputs over Linux
environment (provide snapshot).

INPUT:
1)
#include <stdio.h>
int main()
{
int x, n = 10, z;
x = n++;
z = ++n;
printf("The value of x = %d\n", x);
printf("The value of z = %d\n", z);
return 0;
}

2)
#include <stdio.h>
#define LIMIT 50
int main()
{
int age;
printf("Hello, please enter your age!\n");

OS LAB 2 BSE 4A
[Link] AZIZ (65146) 02-131192-031

scanf("%d", &age);
if(age < LIMIT)
{
printf("Your age is %d.\n", age);
printf("Still young!!\n");
}
else if(age == LIMIT)
{
printf("Your age is %d.\n", age);
printf("Almost there.\n");
}
else
{
printf("Your age is %d.\n", age);
printf("You are a senior!\n");
}

return 0;
}

OUTPUT:
1)

2)

OS LAB 2 BSE 4A
[Link] AZIZ (65146) 02-131192-031

 Question # 03:
Write a C program on the Linux environment that takes your marks as an input and
display your grades accordingly to that followed at Bahria University. Limit your
program to a maximum of five subjects. Use the suitable logical operator(s), i.e. and
(&&), or (||), not (!), if required.

INPUT:
#include <stdio.h>
int main()
{
int num;
int num2;
printf("\nEnter your marks of English\n ");
scanf("%d",&num);
printf("\nEnter your marks of Computer Programming\n ");
scanf("%d",&num2);

// for 1st subject


if(num >= 85 && num<=100){
printf(" \nYou got A grade in English\n"); // printing outputs
}
else if ( num >=80 && num<=84){ // Note the space between else & if

OS LAB 2 BSE 4A
[Link] AZIZ (65146) 02-131192-031

printf("\n You got A- grade in English\n");


}
else if ( num >=75 && num <=79){
printf(" \nYou got B+ grade in English\n");
}
else if ( num >=70 && num<=74){
printf(" \nYou got B- grade in English\n");
}
else if ( num >=65 && num<=69){
printf(" \nYou got B grade in English\n");
}
else if ( num >=60 && num<=64){
printf(" \nYou got C+ grade in English\n");
}
else if ( num >=55 && num<=59){
printf(" \nYou got C grade in English\n");
}
else if ( num >=50 && num<=54){
printf(" \nYou got D grade in English\n");
}
else if ( num <50){
printf(" \nYou Failed in English\n");
}
// For 2nd Subject
if(num2 >= 85 && num2<=100){
printf(" \nYou got A grade in Computer Programming\n"); // printing outputs
}

OS LAB 2 BSE 4A
[Link] AZIZ (65146) 02-131192-031

else if ( num2 >=80 && num2<=84){ // Note the space between else & if
printf(" \nYou got A- grade in Computer Programming\n");
}
else if ( num2 >=75 && num2 <=79){
printf(" \nYou got B+ grade in Computer Programming\n");
}
else if ( num2 >=70 && num2<=74){
printf(" \nYou got B- grade in Computer Programming\n");
}
else if ( num2 >=65 && num2<=69){
printf(" \nYou got B grade in Computer Programming\n");
}
else if ( num2 >=60 && num2<=64){
printf(" \nYou got C+ grade in Computer Programming\n");
}
else if ( num2 >=55 && num2<=59){
printf(" \nYou got C grade in Computer Programming\n");
}
else if ( num2 >=50 && num2<=54){
printf(" \nYou got D grade in Computer Programming\n");
}
else if ( num2 <50){
printf(" \nYou Failed in Computer Programming\n");
}

return 0;
}

OS LAB 2 BSE 4A
[Link] AZIZ (65146) 02-131192-031

OUTPUT:

 Question # 04:
Write a shell script to display your address over multiple lines.

 Question # 05:
Write a shell script that would traverse among any three directories that are placed
under the /home directory. While moving from one directory to another, the script
should display the name of the current working directory and list the content within
that directory, including the hidden files.

OS LAB 2 BSE 4A
[Link] AZIZ (65146) 02-131192-031

OS LAB 2 BSE 4A

You might also like