0% found this document useful (0 votes)
12 views

Lab 4

This document is a lab report submitted by 4 students for their Operating Systems lab. It details 13 commands or tasks simulated in Linux to demonstrate basic shell scripting skills. These include printing system variables, redirection operators, arithmetic operations, taking user input, creating simple scripts to perform calculations and display directory information, and understanding exit statuses of commands. The conclusion states that the lab helped provide an understanding of shell scripting fundamentals like creating/running scripts, variables, arithmetic, and exit statuses.

Uploaded by

Jahangir Awan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lab 4

This document is a lab report submitted by 4 students for their Operating Systems lab. It details 13 commands or tasks simulated in Linux to demonstrate basic shell scripting skills. These include printing system variables, redirection operators, arithmetic operations, taking user input, creating simple scripts to perform calculations and display directory information, and understanding exit statuses of commands. The conclusion states that the lab helped provide an understanding of shell scripting fundamentals like creating/running scripts, variables, arithmetic, and exit statuses.

Uploaded by

Jahangir Awan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Course: CS-303 Operating Systems Lab Report 4

Lab Title: Basic Shell scripting using gedit

Submitted To: Sir Hasnain

Submitted By:

Talha Malik(21-CS-025)

Muhammad Jahangir(21-CS-089)

Basit Ali(21-CS-104)

Muhammad Alrayan(21-CS-107)

Department of Computer Science HITEC University Taxila


1 Write 5 system variables of Linux along with their function?
PATH: The PATH variable specifies a colon-separated list of directories where the shell looks for
executable files when a command is entered. It plays a crucial role in command execution, allowing
you to run programs without specifying their full paths.
1. HOME: The HOME variable contains the path to the current user's home directory. This variable
is often used by applications and scripts to reference the user's home directory for configuration
files and data storage.
2. USER or LOGNAME: These variables typically store the username of the currently logged-in
user. They are helpful in identifying the user who is executing commands or scripts.
3. SHELL: The SHELL variable stores the path to the user's default shell. It helps in identifying the
user's preferred shell, which is used for interactive command-line sessions.
4. PWD: The PWD variable holds the current working directory (the directory where you are
currently located). It can be used to display or manipulate the current directory within scripts or
commands.

2 What are different types of operator used in linux terminal?


In the Linux terminal, you we use various types of operators to perform operations like redirection, piping,
conditional execution, and more. Here are some of the different types of operators used in Linux terminal
commands:

1. Redirection Operators:
• >: Redirects the standard output of a command to a file, overwriting the file if it
already exists.
• >>: Redirects the standard output of a command to a file, appending the output to
the file if it already exists.
• <: Redirects the standard input of a command from a file.
• 2>: Redirects the standard error (stderr) of a command to a file.

1. Pipe Operator:
• svbg
• |: Pipes the standard output of one command as the standard input of another
command, allowing you to chain commands together.
2. Command Separator Operators:
• ;: Separates multiple commands on a single line, allowing you to run them
sequentially.
• &&: Executes the second command only if the first command succeeds (returns a
zero exit status).
• ||: Executes the second command only if the first command fails (returns a non-zero
exit status).
3. Background Operator:
• &: Allows you to run a command in the background, freeing up the terminal for
other tasks.
4. Subshell Operator:
• (): Executes a group of commands within a subshell. This can be useful for scoping
variables or controlling the execution of multiple commands.
5. Logical Operators (in conditional statements):
• &&: Used to combine multiple conditions. The second condition is evaluated only
if the first one is true.
• ||: Used to combine multiple conditions. The second condition is evaluated only if
the first one is false.
• !: Negates a condition, making it true if it was false and vice versa.
6. Wildcard Operators:
• *: Matches any sequence of characters in filenames (e.g., *.txt matches all .txt files).
• ?: Matches a single character in filenames.
• [ ]: Defines character classes for pattern matching (e.g., [0-9] matches any single
digit).
7. Escape Operator:
• \: Escapes special characters, allowing you to use them as literal characters (e.g., \$
to represent a dollar sign).

3 Simulate following commands in Linux and note down the exit status:
a. $ expr 1 + 3
b. $ echo $?
c. $ echo Welcome
d. $ echo $?
e. $ wildwestcanwork?
f. $ echo $?
g. $ date
h. $ echo $?
i. $ echon $?
j. $ echo $?

File
Output

4 Simulate command/commands in Linux to define variable x with value 20 and


print it on screen. Check exit status.

Output

5 Simulate command/commands in Linux to define variable x with value OS


and print it
on screen. Check exit status.
Code

Output
6 Simulate command/commands in Linux to print division of two numbers, let's
say 12
and 3?
Code

Output

7 Simulate command/commands in Linux to define two variable x=20, y=5 and


then to print mod of x and y (i.e. x%y)
Code

Output
8 Take three numbers from user as input and print their sum.
Code

Output

9 Simulate command/commands in gedit to make a simple calculator in which


addition, multiplication, subtraction and division on two user defined
variables is calculated.
Code
Output

10 Simulate command/commands in gedit that contains today’s date in script,


execute the program and display its output.
Code

Output

11 Simulate command/commands in gedit that contains contents of your home


directory in script, execute the program and display its output.

Output
12 Simulate command/commands in gedit that contains your present work
directory in script, execute the program and display its output.

13 Simulate the following commands in Linux and state which of the


command is correct and why for printing your home directory location,
a)$ echo $HOME
OR
(b)$ echo HOME
echo HOME This is because $HOME is a shell environment variable that stores the path to the current
user's home directory. When you use $HOME with the $ symbol, the shell substitutes it with the actual
home directory path. So, running echo $HOME will display the correct home directory location.

Conclusion
Shell scripting is a powerful way to automate tasks and interact with our Linux system. It's essential for
system administrators and developers. After this lab, we have a basic understanding of how to create,
execute, and manage shell scripts, set file permissions, work with environment variables, perform
arithmetic operations, and handle command exit statuses. Remember to practice and experiment with
these concepts to reinforce your understanding of shell programming in Linux. It's a valuable skill that
can save our time and effort when working with Linux systems.

You might also like