Lab 4
Lab 4
Submitted By:
Talha Malik(21-CS-025)
Muhammad Jahangir(21-CS-089)
Basit Ali(21-CS-104)
Muhammad Alrayan(21-CS-107)
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
Output
Output
6 Simulate command/commands in Linux to print division of two numbers, let's
say 12
and 3?
Code
Output
Output
8 Take three numbers from user as input and print their sum.
Code
Output
Output
Output
12 Simulate command/commands in gedit that contains your present work
directory in script, execute the program and display its output.
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.