USP LAB Syllabus

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

SEMESTER LTPC

UNIS SHELL PROGRAMMING LAB


(Common to CSE & IT)
Total Contact Hours:
Prerequisites:

COURSE OBJECTIVES
OBJ1 To understand and execute fundamental Unix commands for directory and file
handling, basic file operations, and viewing/editing files.
OBJ2 To gain proficiency in text manipulation and sorting commands, including `echo`,
`printf`, `sort`, `uniq`, `cut`, `paste`, and `split`.
OBJ3 To learn to retrieve user and system information and manage file permissions to
ensure security.
OBJ4 To develop skills in using networking commands and managing processes,
including monitoring disk usage, configuring network interfaces, and handling
system processes.
OBJ5 To write and execute shell scripts to automate tasks, manipulate text with `grep`,
`sed`, and `awk`, and develop Korn Shell scripts for various utilities and
applications.

WEEK-1
1. Directory and File Handling Utilities:
▪ Display the current working directory using the pwd command.
▪ Create a directory named lab1 using the mkdir command.

▪ Navigate into the lab1 directory using the cd command.

▪ Create three subdirectories named dir1, dir2, and dir3 inside lab1 using the mkdir
command.
▪ List the contents of the lab1 directory using the ls command.
2. Basic File Operations:

▪ Create three files named file1.txt, file2.txt, and file3.txt inside dir1 using the touch
command.
▪ Copy file1.txt to dir2 and rename it to file1_copy.txt using the cp command.
▪ Move file2.txt from dir1 to dir3 using the mv command.

1|Page
▪ Remove file3.txt from dir1 using the rm command.
3. Viewing and Editing Files:
▪ Use the cat command to concatenate file1.txt and file1_copy.txt and display the result.
▪ Use the more command to view the contents of file1.txt.

▪ Use the less command to view the contents of file1_copy.txt.

▪ Use the head command to display the first 10 lines of file1.txt.

▪ Use the tail command to display the last 10 lines of file1.txt.

4. Text Manipulation and Sorting:


▪ Use the echo command to append "Hello, World!" to file1.txt.
▪ Use the printf command to add formatted text to file1.txt.

▪ Use the sort command to sort the contents of file1.txt and save the sorted output to
sorted_file1.txt.
▪ Use the uniq command to remove duplicate lines from sorted_file1.txt.
5. Cut, Paste, and Split Commands:

▪ Use the cut command to extract the first column from sorted_file1.txt and save it to
column1.txt.

▪ Use the paste command to combine column1.txt and sorted_file1.txt into a new file
pasted_file.txt.
▪ Use the split command to split file1.txt into pieces of 5 lines each.
6. User and System Information:
▪ Display the username of the current user using the whoami command.
▪ Display the system information using the uname -a command.

▪ Display the current date and time using the date command.

▪ Display the users currently logged in using the who command.

▪ Display detailed information about a user using the finger command.

7. Security by File Permissions:


▪ Create a file named permissions_test.txt using the touch command.
▪ Change its permissions to read-only for the owner and no permissions for others using
the chmod command.
▪ Display the permissions of permissions_test.txt using the ls -l command.
▪ Change the ownership of permissions_test.txt to a different user (requires superuser
permissions) using the chown command.
▪ Change the group of permissions_test.txt to a different group using the chgrp command.

2|Page
WEEK-2
1. Disk Usage and File System Management:
▪ Use the du command to display the disk usage of the lab1 directory.
▪ Use the df command to display the available disk space on all mounted filesystems.

▪ Mount a temporary file system to the /mnt directory using the mount command.

▪ Unmount the temporary file system using the umount command.

2. Networking Commands:
▪ Display the hostname of the machine using the hostname command.
▪ Use the ping command to check the connectivity to google.com.

▪ Display the network interface configuration using the ifconfig or ip command.

▪ Use the nslookup command to find the IP address of a given domain name.

▪ Use the netstat command to display all active network connections.

3. Process Management:
▪ List all currently running processes using the ps command.
▪ Kill a process by its PID using the kill command.

▪ Display the current system's uptime using the uptime command.

▪ Display the current user's scheduled tasks using the at command.

▪ Set a process to run with a lower priority using the nice command.

WEEK-3
1. Standard Streams and Redirection:
▪ Write a command to redirect the output of the ls command to a file named ls_output.txt.
▪ Append the output of the date command to the same ls_output.txt file.

▪ Redirect the standard error of a non-existent command ls non_existing_file to a file


named error_output.txt.
2. Pipes and Filters:
▪ Use a pipe to count the number of lines in the /etc/passwd file.

▪ Combine the cat and sort commands using a pipe to display the sorted contents of a
file named data.txt.

▪ Use the tee command to simultaneously display the output of the who command on
the terminal and save it to a file named who_output.txt.
3. Sorting and Counting:
▪ Sort the contents of unsorted.txt and save the sorted output to sorted.txt.

3|Page
▪ Use the uniq command to remove duplicate lines from sorted.txt and save the output
to unique_sorted.txt.

▪ Count the number of lines, words, and characters in unique_sorted.txt using the wc
command.
4. Command Substitution and Quotes:

▪ Use command substitution to store the output of the date command in a variable and
then display that variable.

▪ Demonstrate the difference between single quotes (') and double quotes (") by
creating a variable that contains a wildcard character (*) and displaying its value using both
types of quotes.
5. Job Control:

▪ Start a long-running process (e.g., sleep 1000) in the background and list all
background jobs.
▪ Bring the background job to the foreground and then stop it using ctrl+z.
▪ Resume the stopped job in the background using the bg command and then terminate
it using the kill command.
6. Aliases and Shell Customization:
▪ Create an alias named ll for the command ls -la and demonstrate its use.
▪ Remove the alias ll and verify it has been removed.

▪ Customize the shell prompt to display the current directory followed by a $ sign.

7. Variables and Environment Customization:


▪ Create a local variable named LOCAL_VAR and assign it a value. Display its value.

▪ Export an environment variable named GLOBAL_VAR and assign it a value. Display


its value.

▪ Demonstrate the use of predefined environment variables like PATH, HOME, and
USER by displaying their values.

WEEK-4 (grep)
1. Basic Grep Usage:
Write a program that creates a file named `sample.txt` and fills it with random text. Use the
`grep` command to search for lines containing the word "example" and print those lines.
2. Counting Lines:
Create a file named lines.txt with random lines of text. Write a program using `grep`
options to:

4|Page
▪ Display the count of blank lines.
▪ Display the count of non-blank lines.

3. Regular Expression Search:


Write a program that searches for a pattern that starts with any character followed by
"test" and ends with any character in a file named `regex_test.txt`. Use `grep` to find and
display matching lines.
4. Case Insensitive Search:
Create a file named `case.txt` with various lines of text in mixed case. Write a program
using `grep` to search for the word "linux" ignoring case sensitivity and display the
matching lines.
5. Display Line Numbers:
Write a program that creates a file named `numbered.txt` and fills it with random text.
Use `grep` to search for lines containing the word "data" and display those lines along
with their line numbers.
6. Search and Invert Match:
Write a program that creates a file named `invert.txt’ with random lines of text. Use
`grep` with the `-v` option to display all lines that do not contain the word "error".

WEEK-5 (grep)
1. Extended Grep (egrep):
Write a program that creates a file named `extended.txt` with random lines of text. Use
`egrep` to search for lines containing the pattern "abc|def" and display those lines.
2. Character Set Matching:
Create a file named `char_set.txt` with various lines of text. Write a program using `grep`
to search for lines that start with any lowercase letter and contain the word "pattern".
Display the matching lines.
3. Fixed String Search (fgrep):
Write a program that creates a file named `fixed.txt` with random lines of text. Use
`fgrep` to search for the literal string "[ABC]123" and display the matching lines.
4. Pattern Matching with Line Anchors:
Create a file named anchors.txt with lines starting and ending with different words. Write
a program using `grep` to:

▪ Search for lines that start with the word "start".


▪ Search for lines that end with the word "end".

5|Page
5. Search for Multiple Patterns:
Write a program that creates a file named multi_pattern.txt with various lines of text. Use
`grep` to search for lines containing either "foo" or "bar" and display the matching lines.
6. Piping Commands:
Write a program that lists all files in the current directory and pipes the output to `grep` to
display only the files with names starting with "log".

WEEK-6 (sed)
1. Basic Sed Operations:
Write a script that creates a file named input.txt with lines of text. Use `sed` to replace the
first occurrence of the word "Hello" with "Hi" in the file and display the modified content.
2. Multiple Substitutions:
Create a file named `multi_sub.txt` with several lines of text. Write a `sed` script to:
▪ Replace "apple" with "orange".

▪ Replace "banana" with "grape".

▪ Perform these substitutions on specific lines as well as globally in the file.

3. Address Ranges:
Write a script that creates a file named `range.txt` with random lines of text. Use `sed` to
print lines between the 3rd and the 7th line inclusive.
4. Conditional and Unconditional Branching:
▪ Create a file named branch.txt with multiple lines. Write a `sed` script to:

✓ Replace the first occurrence of "Admin" with "Supervisor".

✓ Use an unconditional branch to loop through and print remaining lines.

✓ Use a conditional branch to handle specific patterns.

5. Line Numbering:
Write a script that creates a file named `numbered_lines.txt` with random lines of text.
Use `sed` to display the file content with line numbers prefixed to each line.
6. Using Hold Space:
▪ Create a file named `hold_space.txt` with lines of text. Write a `sed` script to:

✓ Copy the first line to the hold space.

✓ Append the contents of the hold space to the end of the file.

WEEK-7 (sed)
1. Inserting and Appending Text:

6|Page
Write a script that creates a file named insert_append.txt with lines of text. Use sed to:
▪ Insert "Start" before the second line.
▪ Append "End" after the fourth line.

2. Substitution with Flags:


Create a file named `sub_flags.txt` with lines of text. Write a `sed` script to:
▪ Replace all occurrences of "cat" with "dog" globally.
▪ Replace the first occurrence of "mouse" with "rat" in each line.

▪ Print the lines where substitutions have occurred.

3. Reading and Writing Files:


Write a script that creates two files: `file1.txt` and `file2.txt` with random lines of text.
Use `sed` to:

▪ Read the contents of `file2.txt` and insert it into `file1.txt` after the first line.
▪ Write specific lines from `file1.txt` to a new file `output.txt`.

4. Transform Command:
Create a file named `transform.txt` with text containing vowels. Write a `sed` script to
transform all lowercase vowels to uppercase vowels in the file and display the result.
5. Branching with Labels:
Write a script that creates a file named branch_labels.txt with multiple lines. Use sed to:
▪ Replace the word "old" with "new" only if it is not at the beginning of a line.
▪ Implement branching with labels to handle specific patterns and conditions.

6. Delete and Substitute Commands:


Create a file named delete_substitute.txt with lines of text. Write a sed script to:
▪ Delete the first character of each line.
▪ Delete the last but one character of each line.

▪ Swap the first and second words in each line.

WEEK-8 (awk)
1. Basic AWK Usage:
Write a script to create a file named `data.txt` with random lines of text. Use an AWK
command to print all lines containing the word "example".
2. Field Separator:
Create a file named `fields.txt` where fields are separated by commas. Write an AWK
script to print the second and fourth fields of each line.
3. Pattern Matching:

7|Page
Write a script that creates a file named `match.txt` with lines of text. Use an AWK
command to print lines where the third field matches the regular expression `[A-Za-z]+`.
4. Using Variables:
Create a file named variables.txt with numerical data in each line. Write an AWK script
to calculate and print the sum and average of the numbers in the second field.
5. BEGIN and END Blocks:
Write a script that creates a file named `begin_end.txt` with random data. Use an AWK
script with BEGIN and END blocks to print a header before processing the file and a
footer after processing the file.
6. String Functions:
Create a file named `strings.txt` with various strings. Write an AWK script to use the
length, substr, and toupper functions to process and print the strings.

WEEK-9 (awk)
1. Arithmetic Operations:
Write a script to create a file named `arithmetic.txt` with numerical data. Use an AWK
script to perform arithmetic operations on the fields and print the results.
2. Conditional Statements:
Create a file named `conditions.txt` with student names and their scores in three subjects.
Write an AWK script to print "Pass" if the average score is 50 or above, otherwise print
"Fail".
3. Loops:
Write a script that creates a file named loops.txt with multiple lines of data. Use an AWK
script to iterate over each field of the file and print each field value.
4. Associative Arrays:
Create a file named `words.txt` with lines of text. Write an AWK script to count the
frequency of each word and print the word along with its count.
5. Formatted Output:
Write a script that creates a file named `formatted.txt` with numerical data. Use an AWK
script with the printf function to print the data in a formatted table.
6. Using System Commands:
Create a file named `system.txt` with user information. Write an AWK script that pipes
the contents of the file to another command (e.g., sort) and processes the sorted data.

8|Page
WEEK-10 (Korn Shell Scripting)
1. Directory or File Check:
Write a shell script that takes a command line argument and reports whether it is a
directory or a file.
2. Uppercase Conversion:
Write a shell script that accepts one or more file names as arguments and converts all of
them to uppercase, provided they exist in the current directory.
3. Gross Salary Calculation:
Write a shell script that computes the gross salary of an employee according to the
following rules:

▪ If the basic salary is less than 1500, then HRA = 10% of the basic and DA = 90% of
the basic.
▪ If the basic salary is 1500 or more, then HRA = Rs.1500 and DA = 98% of the basic.
4. String Operations:
Write a shell script to perform the following string operations:
▪ Extract a substring from a given string.
▪ Find the length of a given string.

5. Login Notification:
Write a shell script that takes a login name as a command-line argument and reports when
that person logs in.

WEEK-11 (Korn Shell Scripting)


1. Greeting Based on Time:
Write a shell script which will greet you “Good Morning,” “Good Afternoon,” “Good
Evening,” and “Good Night” according to the current time.
2. Binary Search:
Write a shell script to search for a given number using a binary search.
3. Terminal Lock:
Write a shell script to lock the terminal. The script should prompt for a password to
unlock the terminal.
4. String Analysis:
Write a shell script to find the number of vowels, consonants, digits, whitespaces, and
special characters in a given string.
5. Mathematical Tables:

9|Page
Write a shell script to generate the multiplication table of a given number.

WEEK-12 (Korn Shell Scripting)


1. Reverse Numbers:
Write a shell script to display the reverse of a given number.
2. File Handling Menu:
Write an interactive shell program that offers the user a choice of copying, removing,
renaming, or linking files. The program should prompt the user for the necessary
information, such as file names and new names.
3. Fibonacci Series:
Write a shell script which will display the Fibonacci series up to a given number of terms.
4. Palindrome Check:
Write a shell script to display if a given string is a palindrome from the command-line
arguments.
5. Factorial Calculation:
Write a shell script to find the factorial of a given number.

COURSE OUTCOMES
Students are able
CO1 To demonstrate the ability to use Unix commands for directory navigation, file
manipulation, and basic system operations.
CO2 To execute advanced text processing tasks, including sorting, searching, and
modifying file contents using command-line tools.
CO3 To efficiently gather system and user information, manage user permissions, and
perform security operations through Unix commands.
CO4 To apply networking commands to check connectivity, configure network settings,
and use process management commands to monitor and control system processes.
CO5 To develop and implement shell scripts for automating tasks, including text
manipulation, system monitoring, and user-defined operations, showcasing an
understanding of scripting logic and syntax.

10 | P a g e

You might also like