Linux Practical
Linux Practical
Linux Practical
No:6
Date:
Process Management commands
AIM:
To write and executes various process management commands using Linux OS.
COMMANDS
ps command
The ps command is used to display information about processes running on a Linux
system.
ps Display processes owned by the current user and associated
with the current terminal
ps -ef / -aux List currently running process in full format
ps -u <username> List process for specific user
ps -C <command> List process for given command
ps -p <PID> List process with given PID
ps --forest -C Print a process tree
sshd
& command
In Linux, the & symbol is not a standalone command, but an operator used to run
commands in the background.
sleep 10 & Running a command in the background.
mkdir Practical & pwd & Running multiple commands simultaneously.
nohup command
In Linux, the nohup command is used to run a command or script in the background
and makes it immune to hangup signals. The name "nohup" stands for "no hangup."
nohup man & Running a command in the background and
preventing hangup signals
nohup bash my_script.sh To run a command in the foreground.
nohup bash To redirect the output to the output.txt file.
my_script.sh>output.txt
nohup bash my_script.sh & To run a command in the background (with
fg ‘&’).
nohup bash -c 'cal && ls -l' To run multiple commands in the background.
ls Syntax: $ nohup bash -c 'commands'
cat nohup.out
kill command
1
In Linux, the kill command is used to send signals to processes, allowing you to
manage and control their execution.
Signal Name Signal Number Signal Use
SIGNULL 0 NULL, check access to PID
SIGHUP 1 Hangup
SIGINT 2 Interrupt
SIGQUIT 3 Quit
SIGKILL 9 Kill
SIGTERM 15 Terminate
SIGSTOP 24 Stop
SIGTSTP 25 Stop/pause the process
SIGCONT 26 Continue a stopped process
nice command
In Linux, the nice command is used to run a command with a specified priority level.
It allows you to adjust the scheduling priority of a process. The priority level ranges
from -20 (highest priority) to 19 (lowest priority).
nice -n 10 command Run a command with a lower priority.
nice -n -10 command Run a command with a higher priority.
nice -n command View the current priority of a process.
nice -n -20 command Run a command with maximum priority (-20).
nice command Run a command with default priority (0).
2
EXECUTION
MARK ALLOCATION
Submission (2)
Command / Program (5)
Execution (3)
Total (10)
RESULT
Thus the process management commands were written and executed successfully.
3
Ex.No:7
Date:
Device pattern commands
AIM:
To write and executes various device pattern commands using Linux OS.
COMMANDS
ls command
The ls command is used to list files and directories. It can be combined with wildcard
characters to match specific patterns.
ls ??? All three-character filenames.
ls *[abc]* All filenames that contain the characters 'a', 'b', or 'c'.
ls prefix* All filenames beginning with a particular string.
ls ca?? All filenames beginning with 'ca' and ending with two digits.
ls s*a* All filenames beginning with 's' and having 'a' somewhere.
4
EXECUTION
MARK ALLOCATION
Submission (2)
Command / Program (5)
Execution (3)
Total (10)
RESULT
Thus the device pattern commands were written and executed successfully.
5
Ex.No:8
Date:
Decrementing value using WHILE loop
AIM:
To write a shell script that accepts a numerical value N. Then display the
decrementing value of N till it reaches 0.
ALORITHM
1. Start the algorithm.
2. Accept a numerical value N as input.
3. Initialize a loop to iterate while N is greater than or
equal to 0.
4. Print the current value of N.
5. Decrement the value of N by 1.
6. End the loop.
7. Stop the algorithm.
PROGRAM
#!/bin/bash
while [ $N -ge 0 ]
do
echo $N
((N--))
done
8.
6
EXECUTION
MARK ALLOCATION
Submission (2)
Command / Program (5)
Execution (3)
Total (10)
RESULT
Thus the required shell script program was executed successfully.
7
Ex.No:9
Date:
Search a string and display it
AIM:
To write a shell script to search a string and display it.
ALORITHM
PROGRAM
#!/bin/bash
if [ -f "$file_name" ]; then
grep "$search_string" "$file_name"
else
echo "File does not exist."
fi
1.
8
EXECUTION
MARK ALLOCATION
Submission (2)
Command / Program (5)
Execution (3)
Total (10)
RESULT
Thus the required shell script program was executed successfully.
9
Ex.No:10
Date:
Merging Files into Destination File
AIM:
To write a shell script that allows users to merge the contents of two specified files
into a destination file.
ALORITHM
1. Start the algorithm.
2. Check if three command line arguments are provided.
3. If the number of arguments is not equal to 3, display an
error message and exit.
4. Store the command line arguments in variables.
5. Check if the destination file already exists.
6. If the destination file exists, display an error message
and exit.
7. Concatenate the contents of file1 and file2 into the
destination file.
8. Display a success message.
9. Stop the algorithm.
PROGRAM
#!/bin/bash
if [ $# -ne 3 ]; then
echo "Error: Invalid number of arguments. Usage: $0
<destination_file> <file1> <file2>"
exit 1
fi
destination_file=$1
file1=$2
file2=$3
if [ -e "$destination_file" ]; then
echo "Destination file '$destination_file' already exists.
Exiting."
exit 1
fi
2.
10
EXECUTION
MARK ALLOCATION
Submission (2)
Command / Program (5)
Execution (3)
Total (10)
RESULT
Thus the required shell script program was executed successfully.
11
Ex.No:11
Date:
Printing File Contents
AIM:
To write a shell script to print contents of file from given line number to next given
number of lines.
ALORITHM
1. Start the algorithm.
2. Accept the file name as input.
3. Accept the starting line number as input.
4. Accept the number of lines to print as input.
5. Check if the file exists.
6. If the file exists:
a. Use the sed command to print the desired lines.
b. The sed command should be constructed to specify the
line range from the starting line number to the next
num_lines lines.
c. Print the contents of the desired lines.
7. If the file does not exist, display an error message.
8. Stop the algorithm.
PROGRAM
#!/bin/bash
if [ -f "$file_name" ]; then
# Use sed command to print the desired lines
sed -n "${start_line},$((${start_line}+${num_lines}-1))p"
"$file_name"
else
echo "File does not exist."
fi
3.
12
EXECUTION
MARK ALLOCATION
Submission (2)
Command / Program (5)
Execution (3)
Total (10)
RESULT
Thus the required shell script program was executed successfully.
13
Ex.No:12
Date:
Printing Date Information
AIM:
To write a shell script that print out date information in this order: time, day of The
week, day number, year– that is like this. 21:18:00 IST Mon16 Aug2021.
ALORITHM
1. Start the algorithm.
2. Get the current date and time.
3. Format the date and time using specific format
specifiers:
%T: Displays the time in HH:MM:SS format.
%Z: Displays the time zone name.
%a: Displays the abbreviated day of the week.
%d: Displays the day of the month.
%b: Displays the abbreviated month name.
%Y: Displays the four-digit year.
4. Print the formatted date information in the desired
order.
5. Stop the algorithm.
PROGRAM
#!/bin/bash
echo "$current_date"
4.
14
EXECUTION
MARK ALLOCATION
Submission (2)
Command / Program (5)
Execution (3)
Total (10)
RESULT
Thus the required shell script program was executed successfully.
15
Ex.No:13
Date:
Basic Math Calculator
AIM:
To write a shell script to Develop a Basic Math Calculator using case statement.
ALORITHM
1. Start the algorithm.
2. Accept the first number, second number, and operator as
input.
3. Perform the calculation based on the operator using a
case statement:
If the operator is "+", add the two numbers.
If the operator is "-", subtract the second number
from the first number.
If the operator is "*", multiply the two numbers.
If the operator is "/", divide the first number by
the second number.
If the operator is invalid, display an error message
and exit.
4. Display the result to the user.
5. Stop the algorithm.
PROGRAM
#!/bin/bash
case $operator in
"+")
result=$(echo "$num1 + $num2" | bc)
;;
"-")
result=$(echo "$num1 - $num2" | bc)
;;
"*")
result=$(echo "$num1 * $num2" | bc)
;;
"/")
result=$(echo "scale=2; $num1 / $num2" | bc)
;;
*)
echo "Invalid operator!"
exit 1
;;
esac
echo "Result: $result"
16
EXECUTION
MARK ALLOCATION
Submission (2)
Command / Program (5)
Execution (3)
Total (10)
RESULT
Thus the required shell script program was executed successfully.
17
Ex.No:14 Multiple Choice Question with User
Date: Answer Validation
AIM:
To write a shell script that represents a multiple choice question, gets the user’s
Answer and report back whether the answer is right, wrong or not one of the
choices.
ALORITHM
1. Start the algorithm.
2. Define the question and choices.
3. Define the correct answer.
4. Prompt the question and display the choices.
5. Accept the user's answer.
6. Check the user's answer using a case statement:
a. If the user's answer is one of the choices (A, B, C, or
D):
i. If the user's answer matches the correct answer,
display "Correct answer!".
ii. If the user's answer is incorrect, display "Wrong
answer!".
b. If the user's answer is not one of the choices, display
"Not one of the choices."
7. Stop the algorithm.
PROGRAM
#!/bin/bash
correct_answer="B"
echo "$question"
for choice in "${choices[@]}"; do
echo "$choice"
done
18
;;
esac
EXECUTION
MARK ALLOCATION
Submission (2)
Command / Program (5)
Execution (3)
Total (10)
RESULT
Thus the required shell script program was executed successfully.
19
Ex.No:15 Shell Script for
Date: Checking File or Directory Type
AIM:
To write a shell script that takes a command line argument and reports on Whether it
is a directory, a file or something else.
ALORITHM
1. Start
2. Argument Check
3. Store Argument
4. Existence Check
If path doesn't exist, display a message and exit.
5. Directory Check
If path is a directory, display a message.
6. File Check
If path is a regular file, display a message.
7. Other Type Check
If path is not a directory or file, display a
message.
8. Exit
PROGRAM
#!/bin/bash
20
echo "$path is a regular file."
else
echo "$path is something else."
fi
EXECUTION
MARK ALLOCATION
Submission (2)
Command / Program (5)
Execution (3)
Total (10)
RESULT
Thus the required shell script program was executed successfully.
21