INDEX
S.No Name of Experiment Page No. Date Sign
1 Study of Unix/Linux general purpose utility command list (man, who,
cat, cd, cp, ps, ls, mv, rm, mkdir, rmdir, echo, more, date, time, kill,
history, chmod, chown, finger, pwd, cal, logout, shutdown etc.),
vi editor, .bashrc, /etc/bashrc and environment variables.
2 Write a shell script program to:
a) display list of user currently logged in;
b) to copy contents of one file to another.
3 Write a program using sed command to print duplicated lines of
Input.
4 Write a grep/egrep script to find the number of words character,
words and lines in a file.
5 Write an awk script to:
a) develop a Fibonacci series;
b) display the pattern of given string or number.
6 Write a shell script program to
a) display the process attributes;
b) change priority of processes;
c) change the ownership of processes;
d)to send back a process from foreground ;
e) to retrieve a process from background ;
f) create a Zombie process
7 Write a program to create a child process and allow the parent to
display “parent” and the child to display “child” on the screen.
8 Write a makefile to compile a C program.
9 Study to execute programs using gdb to utilize its various features like
breakpoints, conditional breakpoints.
Also write a shell script program to include verbose and xtrace debug
option for debugging.
10 Study to use ssh, telnet, putty, ftp, ncftp and other network tools.
Experiment 1) Study of Unix/Linux general purpose utility command list (man, who, cat, cd, cp, ps, ls, mv,
rm, mkdir, rmdir, echo, more, date, time, kill, history, chmod, chown, finger, pwd, cal, logout, shutdown
etc.), vi editor, .bashrc, /etc/bashrc and environment variables.
1. `man` : Stands for "manual"; displays manual pages for commands. - Syntax: `man [command]`.
2. `who` : Shows who is logged into the system. - Syntax: `who`.
3. `cat` : Concatenates files and displays them on the standard output. - Syntax: `cat [file]`.
4. `cd` : Changes the current directory. - Syntax: `cd [directory]`.
5. `cp`: Copies files or directories. - Syntax: `cp [source] [destination]`.
6. `ps` : Displays information about processes. - Syntax: `ps [options]`.
7. `ls`: Lists directory contents. - Syntax: `ls [options] [directory]`.
8. `mv`: Moves files or directories. - Syntax: `mv [source] [destination]`.
9. `rm` : Removes files or directories. - Syntax: `rm [file/directory]`.
10. `mkdir` : Creates dirsectories. - Syntax: `mkdir [directory]`.
11. `rmdir`: Removes directories. - Syntax: `rmdir [directory]`.
12. `echo` : Prints arguments to the standard output. - Syntax: `echo [arguments]`.
13. `more` : Displays the contents of a file one screen at a time. - Syntax: `more [file]`.
14. `date`: Displays or sets the system date and time. - Syntax: `date [options]`.
15. `time` : Measures the time it takes for a command to execute. - Syntax: `time [command]`.
16. `kill` : Sends signals to processes. - Syntax: `kill [options] [process ID]`.
17. `history` : Displays the command history list. - Syntax: `history [options]`.
18. `chmod` : Changes file permissions. - Syntax: `chmod [options] [mode] [file]`.
19. `chown` : Changes file ownership. - Syntax: `chown [options] [user:group] [file]`.
20. `finger` : Displays information about users. - Syntax: `finger [user]`.
21. `pwd` : Prints the current working directory. - Syntax: `pwd`.
22. `cal` : Displays a calendar. - Syntax: `cal [month] [year]`.
23. `logout` : Logs out the current user. - Syntax: `logout`.
24. `shutdown` : Shuts down or restarts the system. - Syntax: `shutdown [options] [time]`.
Vi Editor
- A text editor available in Unix/Linux systems.
- Two modes: Command mode and Insert mode.
- Basic operations:
- Switch to Command mode: `Esc`
- Switch to Insert mode: `i` (insert before cursor), `a` (append after cursor)
- Save changes: `:w`
- Quit without saving: `:q!`
- Save and quit: `:wq`
.bashrc
- A shell script that Bash runs whenever it is started interactively.
- Contains commands to set up the shell environment.
- Located in the user's home directory (`~/.bashrc`).
/etc/bashrc
- System-wide Bash configuration file.
- Applies settings globally to all users.
Environment Variables
- Variables that define the environment in which programs run.
- Examples:
- `PATH`: Directories to search for executable files.
- `HOME`: User's home directory.
- `USER`: Current user's username.
- Viewing: `echo $VARIABLE_NAME`.
- Setting: `export VARIABLE_NAME=value`.
- Persisting changes: Modify `.bashrc` for user-specific changes or `/etc/bashrc` for system-wide changes. Displays the
contents of a file one screen at a time. - Syntax: `more [file]`.
Experiment 2. Write a shell script program to: a) display list of user currently logged in; b) to copy
contents of one file to another.
a) display list of user currently logged in
#!/bin/bash
# Use the "who" command to get a list of logged-in users
users=$(who | awk '{print $1}')
# Print the list of usernames
echo "Currently Logged In Users:"
echo "$users"
b) to copy contents of one file to another.
#!/bin/bash
if [ $# -ne 2 ]; then # Check if two arguments are provided
echo "Usage: $0 <source_file> <destination_file>"
exit 1
fi
source_file="$1" # Source and destination files
destination_file="$2"
if [ ! -f "$source_file" ]; then # Check if source file exists
echo "Error: Source file '$source_file' does not exist."
exit 1
fi
cp "$source_file" "$destination_file" # Copy the content using 'cp' command
if [ $? -eq 0 ]; then # Check for successful copy
echo "Successfully copied contents of '$source_file' to '$destination_file'."
else
echo "Error: Failed to copy the file."
exit 1
fi
Experiment 3. Write a program using sed command to print duplicated lines of Input.
CODE :
sed -n 'G; s/\n/&&/; /^\([^\n]*\)\n\1$/P; s/.*\n//; h'
Code Explanation:
-n suppresses automatic printing of pattern space.
G appends a newline followed by the contents of the hold space to the pattern space.
s/\n/&&/ doubles each newline character, effectively duplicating the entire line.
/^\([^\n]*\)\n\1$/P checks if the line appears twice (duplicated) and prints only the first occurrence.
s/.*\n// removes everything except the first occurrence of the duplicated line.
h copies the pattern space to the hold space.
Experiment 4. Write a grep/egrep script to find the number of words character, words and lines in a file.
CODE :
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: $0 <filename>"
exit 1
fi
filename=$1
char_count=$(cat "$filename" | wc -m) # Counting characters
word_count=$(cat "$filename" | grep -o '\w\+' | wc -l) # Counting words
line_count=$(cat "$filename" | wc -l) # Counting lines
echo "Number of characters: $char_count"
echo "Number of words: $word_count"
echo "Number of lines: $line_count"
Experiment 5. Write an awk script to: a). develop a Fibonacci series; b) display the pattern of given string
or number.
a) Develop a Fibonacci Series
Code :
#!/bin/awk -f
function fibonacci(n) {
if (n <= 1)
return n;
else
return fibonacci(n-1) + fibonacci(n-2);
BEGIN {
printf("Enter the number of terms in Fibonacci series: ");
getline num_terms < "/dev/stdin";
printf("Fibonacci series up to %d terms:\n", num_terms);
for (i = 0; i < num_terms; i++) {
printf("%d ", fibonacci(i));
printf("\n");
}
b) display the pattern of given string or number.
#!/bin/awk -f
function display_pattern(input) { # Function to display pattern of given string or number
for (i = 1; i <= length(input); i++) {
pattern = substr(input, 1, i);
printf("%s\n", pattern);
BEGIN {
printf("Enter a string or number to display its pattern: ");
getline input < "/dev/stdin";
printf("Pattern of \"%s\":\n", input);
display_pattern(input);
}
Experiment 6. Write a shell script program to a) display the process attributes; b) change priority of
processes; c) change the ownership of processes; d) to send back a process from foreground ; e) to
retrieve a process from background ; f) create a Zombie process
a) display the process attributes
i) Prompt the user to enter a process ID
echo -n "Enter the process ID (PID): "
read pid
ii) Check if the entered PID exists
if [ ! -d "/proc/$pid" ]; then
echo "Process with PID $pid does not exist."
exit 1
fi
iii) Display process attributes
echo "Process attributes for PID $pid:"
echo "----------------------------------"
iv) Display process ID
echo -n "PID: "
cat "/proc/$pid/status" | grep "^Pid:"
v) Display parent process ID
echo -n "Parent PID: "
cat "/proc/$pid/status" | grep "^PPid:"
vi) Display process name
echo -n "Name: "
cat "/proc/$pid/status" | grep "^Name:"
vii) Display process state
echo -n "State: "
cat "/proc/$pid/status" | grep "^State:"
viii) Display process priority
echo -n "Priority: "
cat "/proc/$pid/status" | grep "^Priority:"
ix) Display process memory usage
echo -n "Memory Usage: "
cat "/proc/$pid/status" | grep "^VmRSS:"
x) Display process CPU time
echo -n "CPU Time: "
cat "/proc/$pid/status" | grep "^Cpus_allowed:"
xi) Display process start time
echo -n "Start Time: "
cat "/proc/$pid/status" | grep "^Start_time:"
xii) Display process command line
echo -n "Command Line: "
cat "/proc/$pid/cmdline" | tr '\0' ' '
xiii) Display process environment variables
echo "Environment Variables:"
cat "/proc/$pid/environ" | tr '\0' '\n'
exit 0
b) change priority of processes
#!/bin/bash
echo -n "Enter the process ID (PID): " # Prompt the user to enter a process ID (PID) read pid
if [ ! -d "/proc/$pid" ]; then # Check if the entered PID exists
echo "Process with PID $pid does not exist."
exit 1
fi
echo -n "Enter the new priority value (range: -20 to 19, default is 0): " # Prompt the user to enter the new priority
read priority
if [ "$priority" -lt -20 ] || [ "$priority" -gt 19 ]; then # Check if the entered priority is within the valid
range
echo "Invalid priority value. Priority must be in the range of -20 to 19."
exit 1
fi
renice "$priority" "$pid" # Change the priority of the process
if [ $? -eq 0 ]; then # Check if the renice command was successful
echo "Priority of process with PID $pid changed to $priority."
else
echo "Failed to change priority of process with PID $pid."
Fi
exit 0
c) change the ownership of processes
old_user="old_username" # Define the old and new user
new_user="new_username" # Get the process IDs owned by the old user
pids=$(pgrep -u "$old_user") # Change ownership of each process
for pid in $pids; do
echo "Changing ownership of process $pid from $old_user to $new_user..."
kill -STOP "$pid" # Stop the process temporarily
chown "$new_user" "/proc/$pid/" # Change ownership
kill -CONT "$pid" # Resume the process
echo "Ownership of process $pid changed successfully."
done
echo "Ownership of processes owned by $old_user changed to $new_user."
d) to send back a process from foreground
#!/bin/bash
echo -n "Enter the process ID (PID) to send to background: " # Prompt the user to enter the process ID (PID)
read pid
if ! kill -0 "$pid" 2>/dev/null; then # Check if the entered PID exists
echo "Process with PID $pid does not exist."
exit 1
fi
if bg "$pid" >/dev/null 2>&1; then # Send the process to the background
echo "Process with PID $pid sent to background successfully."
else
echo "Failed to send process with PID $pid to background."
Fi
exit 0
e) to retrieve a process from background
# Prompt the user to enter the process ID (PID)
echo -n "Enter the process ID (PID) to bring to foreground: "
read pid
if ! kill -0 "$pid" 2>/dev/null; then # Check if the entered PID exists
echo "Process with PID $pid does not exist."
exit 1
fi
if fg "$pid" >/dev/null 2>&1; then # Bring the process to the foreground
echo "Process with PID $pid brought to foreground successfully."
else
echo "Failed to bring process with PID $pid to foreground."
fi
exit 0
Experiment 7. Write a program to create a child process and allow the parent to display “parent” and the
child to display “child” on the screen
#!/bin/bash
child_process() { # Define a function to display "child"
echo "child"
echo "parent" # Display "parent"
(child_process) # Execute the function in a subshell to create a child process
exit 0
Experiment 8. Write a makefile to compile a C program.
# Makefile to compile a C program
# Compiler
CC = gcc
CFLAGS = -Wall -Wextra # Compiler flags
SRC = program.c # Source file
EXE = program # Executable file
all: $(EXE) # Default target
$(EXE): $(SRC) # Compile the program
$(CC) $(CFLAGS) -o $@ $^
clean: # Clean the generated files
rm -f $(EXE)
Experiment 9. Study to execute programs using gdb to utilize its various features like breakpoints, conditional
breakpoints. Also write a shell script program to include verbose and xtrace debug option for debugging.
1. Setting Up GDB:
- Ensure that GDB is installed on your system. If not, you can install it using your package manager (e.g., `sudo apt
install gdb` on Debian-based systems).
- Compile your C program with debugging symbols enabled. For example, use the `-g` flag with `gcc`: `gcc -g
program.c -o program`.
2. Executing Programs with GDB:
- Start GDB by running `gdb` followed by the name of the executable: `gdb program`.
- You can also start GDB and immediately run the program by using the `run` command: `gdb program --args
[arguments]`.
3. Breakpoints:
- Set breakpoints at specific lines of code using the `break` command: `break filename:linenumber`.
- You can also set breakpoints at function names: `break functionname`.
4. Conditional Breakpoints:
- Set conditional breakpoints using the `break` command followed by a condition: `break linenumber if condition`.
- For example: `break 10 if i == 5`.
5. Executing and Stepping Through Code:
- Use the `run` command to start execution of the program.
- Use the `next` (`n`) and `step` (`s`) commands to step through the code line by line.
6. Printing Variables and Expressions:
- Use the `print` (`p`) command followed by the variable or expression you want to print: `print variable`.
7. Examining Memory:
- Use the `x` command to examine memory: `x address`.
Now, let's write a shell script program that includes verbose and xtrace debug options for debugging. Here's an
example shell script named `debug.sh`:
#!/bin/bash
set -v # Enable verbose mode
set -x # Enable xtrace mode
echo "This is a debug message" # Your script commands go here
ls -l
Experiment 10. Study to use ssh, telnet, putty, ftp, ncftp and other network tools.
1. SSH (Secure Shell) :
- SSH is a protocol used to securely access and manage remote systems over an unsecured network.
- It provides strong encryption and authentication mechanisms to protect sensitive data during transmission.
- SSH clients, such as OpenSSH (command-line), PuTTY (Windows), and SecureCRT (multi-platform), establish
encrypted connections to SSH servers.
- SSH supports various authentication methods, including password authentication, public key authentication, and
keyboard-interactive authentication.
- It allows users to securely execute commands on remote systems, transfer files, and forward ports for secure
remote access.
2. Telnet :
- Telnet is a protocol used for remote terminal connection and communication over TCP/IP networks.
- It provides a bidirectional interactive text-oriented communication session between a client and a server.
- Telnet operates on port 23 by default and sends data, including usernames and passwords, in plaintext, making it
insecure.
- Due to security concerns, Telnet is rarely used for remote access and has largely been replaced by SSH.
3. PuTTY :
- PuTTY is a free and open-source terminal emulator, serial console, and network file transfer application primarily
used for SSH connections.
- It supports various network protocols, including SSH, Telnet, rlogin, and raw socket connections.
- PuTTY is lightweight, easy to use, and highly configurable, making it a popular choice for SSH clients on Windows
platforms.
- It provides features such as session management, SSH key management, and port forwarding for secure remote
access.
4. FTP (File Transfer Protocol) :
- FTP is a standard network protocol used for transferring files between a client and a server on a computer
network.
- It operates over TCP/IP and provides commands for uploading, downloading, renaming, and deleting files and
directories.
- FTP clients establish connections to FTP servers using port 21 for control information and port 20 for data
transmission.
- Although FTP is widely supported and easy to use, it lacks security features, such as encryption, making it
vulnerable to eavesdropping and data tampering.
5. ncftp :
- ncftp is a command-line FTP client that provides an interactive interface for transferring files between systems.
- It supports various FTP protocols, including FTP, FTPS (FTP over SSL), and SFTP (SSH File Transfer Protocol).
- ncftp offers features such as bookmarking, recursive file transfer, and directory synchronization, making it
suitable for both novice and advanced users.
- It is lightweight, efficient, and cross-platform, available for Unix-like operating systems and Windows.