Linux Lab Record
Linux Lab Record
(AUTONOMOUS)
(Affiliated to Bharathiar University, An ISO Certified Institution, Reaccredited at the
“A+” level with CGPA of 3.53 out of 4 by NAAC)
COIMBATORE – 641 049
PRACTICAL RECORD
LINUX PROGRAMMING LAB
REGISTER. NO
BAT25
………………………… …………………………
Staff In-Charge HOD
………………………… …………………………...
Internal Examiner External Examiner
INDEX
PAGE
SL.NO DATE TITLE SIGNATURE
NO
01 IMPLEMENTING VI EDITOR 01
03 DISPLAY CALENDAR 07
IMPLEMENTING COMMAND-LINE
04 10
ARGUMENTS
IMPLEMENTING VARIOUS
05 13
CONTROL STRUCTURES
06 IMPLEMENTING FUNCTION 17
IMPLEMENTING FILE
07 20
MANAGEMENT
IMPLEMENT DIRECTORY
08 23
MANAGEMENT
09 IMPLEMENTING PIPES 27
10 IMPLEMENTING FILTERS 30
IMPLEMENTING VI EDITOR
AIM:
To Write a Shell script to implement text and file system processing commands using VI editor
ALGORITHM:
Step 1: Display the message "Enter the file name: " to prompt the user for input
Step 2: Read the user's input and store it in the variable `inp_f`.
Step 3: Set the variable `op_f` to store the output file name as "email_add.txt".
Step 4: Use the `grep` command with the `-E` flag (extended regular expression) to search for
patterns in the input file (`$inp_f`) that match the regular expression for email addresses.
Step 7: Save the extracted email addresses to the output file (`$op_f`) using the redirection
operator `>`.
Step 9: Display the contents of the output file (`$op_f`) using the `cat` command
CODING
echo "enter the name:"
read inp_f
op_f="email_add.txt"
if [! -f" $inp _f”]; then
echo "file not found: $inp_f"
exit 1
fi
if [! -f"$op_f”]; then
touch "$op_f"
fi
grep -E-o "[[: alnum:]._%+-] + @[[:alnum:].-]+\.[[:alpha:]]{2,4}" "$inp_f" > " $op_f "
echo "extracted email addresses:"
cat "$op_f"
OUTPUT
AIM:
To Write a shell script to read user input and perform various operations.
ALGORITHM:
Step 1: Display the message "Enter the first number: " to prompt the user for the first number,
and store the input in the variable `num1`.
Step 2: Display the message "Enter the second number: " to prompt the user for the second
number, and store the input in the variable `num2`.
Step 3: Display the message "Select an operation: " followed by a list of available operations
(Addition, Subtraction, Multiplication, and Division).
Step 4: Prompt the user for their choice of operation, and store the input in the variable
`choice`.
Step 5: Use a `case` statement to check the value of `choice` and perform the corresponding
operation accordingly: - If `choice` is 1, calculate the addition. If `choice` is 2, calculate the
subtraction
If `choice` is 3, calculate the multiplication If `choice` is 4, calculate the division If `choice`
is none of the above (invalid choice), display the message "Invalid choice"
Step 6: Display the result of the operation
CODING
read choice
case $choice in
1)
result=$(($num1+$num2)) ;;
2) result=$(($num1-$num2));;
3) result=$(($num1*$num2));;
4) result=$(($num1/$num2));;
*)
AIM:
To Write a shell script to display calendar
ALGORITHM:
Step 1: Display the message "Enter Month (MM): " to prompt the user for a month, and store
the input in the variable `month`.
Step 2: Display the message "Enter Year (YYYY):" to prompt the user for a year, and store the
input in the variable `year`.
Step 3: Use the `cal` command with the specified month and year arguments (`$month` and
`$year`) to generate a calendar for the given month and year.
Step 4: The output of the `cal` command will be displayed on the console, showing the calendar
for the provided month and year.
CODING
AIM:
To Write a shell script to implement the usage of command line arguments.
ALGORITHM:
Step 1: Display the message "The name of the script file is $0" to show the name of the script
being executed.
Step 2: Display the message "Total number of arguments passed to the script = $#" to show
the total number of arguments passed to the script.
Step 3: If there are any arguments passed to the script (i.e., `$#` is greater than 0)
a. Display the message "List of arguments:" to indicate that the following lines will display the
arguments.
b. Use a `for` loop to iterate through all the arguments.
Step 4: If no arguments are provided Display the message "No argument provided to the
script."
Step 5: Define a function `isArgumentPresent` that takes one argument `1`:
a. Check if the argument passed to the function is greater than 0.
b. If true, return a success code 0 (indicating that arguments are present).
c. If false, return a failure code 1 (indicating that no arguments are present).
Step 6: Call the `isArgumentPresent` function, passing the number of arguments `$#` as an
argument.
Step 7: Capture the returned code from the function in the variable `returned Code`.
Step 8: Check the value of `returned Code`: - If it is 0, display "Arguments present!" to indicate
that there are arguments passed to the script.
If it is 1, display "Arguments not present!" to indicate that there are no arguments passed to the
script.
CODING
AIM:
To Write a shell script to implement various control structures.
ALGORITHM:
Step 1: Define a function `cal_sum_for()` that takes a number `num` as input:
a. Initialize a variable `sum` to 0.
b. Use a `for` loop to iterate from 1 to `num` (inclusive).
c. Inside the loop, add the value of `i` to the `sum`.
d. After the loop, display the message "Sum using for loop: ".
Step 2: Define another function `cal_sum_while()` that takes a number `num` as input:
Use a `while` loop with the condition `(i <= num)`.
a. Inside the loop, add the value of `i` to the `sum`.
b. Increment the value of `i` by 1 in each iteration.
c. After the loop, display the message "Sum using while loop: ".
Step 3: Define a third function `cal_sum_case()` that takes a number `num` as input:
a. Initialize a variable `sum` to 0.
b. Use a `case` statement to check the value of `num`:
c. Inside the `case` statement, use a `for` loop to iterate from 1 to `num`
Inside the loop, add the value of `i` to the `sum`. –
After the loop, display the message "Sum using case: ". -
Step 4: If `num` is not a positive integer, display the message "invalid input
CODING
cal_sum_for () {
local num=$1
local sum=0
for ((i=1; i<= num; i++)); do
((sum+=i))
done
echo "sum using for loop: "$sum
}
cal_sum_while () {
local num=$1
local sum=0
local i=1
while ((i<=num)); do
((sum+=i))
((i++))
done
echo "Sum using while loop: $sum"
}
cal_sum_case () {
local num=$1
local sum=0
case $num in
[0-9] | [1-9] | [0-9] *)
for ((i=1; i<=num; i++)); do
((sum+=i))
done
echo "sum using case: $sum"
;;
*)
echo "invalid input"
;;
esac
}
echo "Enter a number"
read input_num
if((input_num>0)); then
cal_sum_for "$input_num"
cal_sum_while "$input_num"
cal_sum_case "$input_num"
else
echo "invalid"
fi
OUTPUT
AIM:
To write a shell script to implement functions in Linux
ALGORITHM:
Step 1: Read a number from the user and store it in the variable `num`.
Step 2: Check if `num` is equal to 0. If true, print 1
Step 3: If `num` is not 0, call the `factorial` function with `num` as an argument
Step 4: Define a function `factorial` that takes one argument, `product`, which represents the
number for which the factorial
Step 5: Check if the value of `product` is less than or equal to 2.
Step 6: If `product` is greater than 2, calculate `f` as `product - 1`.
Step 7: Recursively call the `factorial` function with the value of `f` and store the result in `f`.
Step 8: Print the factorial value stored in `f`.
Step 9: Display the factorial of the given number using recursion
CODING
factorial ()
{
product=$1
if((product<=2)); then
echo $product
else
f=$((product-1))
f=$(factorial $f)
f=$((f*product))
echo $f
fi
}
echo "enter the number:"
read num
if ((num==0)); then
echo 1
else
factorial $num
fi
OUTPUT
AIM:
To write a shell script to demonstrate working with files in Linux.
ALGORITHM:
Step 1: Create `myfile.txt` with the content "my name is Alice" using `echo` and display it.
Step 2: Append "I am 22 years old" to `myfile.txt` using `echo`and display it.
Step 3: Copy `myfile.txt` to `new_file.txt` using `cp` and display it.
Step 4: Rename `new_file.txt` to `renamed_file.txt` using `mv`.
Step 5: Display the contents of `renamed_file.txt`.
Step 6: Delete `renamed_file.txt` using `rm`.
Step 7: List the directory contents to confirm changes
CODING
ls
echo "my name is shajan">myfile.txt
echo "file created"
cat myfile.txt
ls
echo "im 22 years old">> myfile.txt
cat myfile.txt
cp myfile.txt new_file.txt
echo "file copied"
cat new_file.txt
ls
mv new_file.txt renamed_file.txt
echo "file renamed"
cat new_file.txt
ls
rm renamed_file.txt
echo "file deleted"
ls
OUTPUT
AIM:
To write a shell script to demonstrate working with directories in Linux.
ALGORITHM:
Step 1: Create directories dir1, dir2, and dir3 using `mkdir`.
Step 2: Print "3 directories created" and list contents with `ls`.
Step 3: Change to dir1, create file1.txt, print "file created," and list contents.
Step 4: Navigate back to the parent directory.
Step 5: Change to dir2, create file2.txt, print "file created," and navigate back.
Step 6: Change to dir1, rename file1.txt to new_file.txt, print "file renamed," and list contents.
Step 7: Remove new_file.txt and print "file removed."
Step 8: Navigate back, remove dir1, and print "directory removed."
Step 9: List contents to confirm changes.
CODING
#make directory
mkdir dir1
mkdir dir2
mkdir dir3
echo "3 directory created"
ls
#change to directory
cd dir1
echo "directory changed"
#create a file in directory
touch file1.txt
echo "file created"
ls
cd ..
echo "directory changed"
cd dir2
echo "directory changed"
touch file2.txt
echo "file created"
cd ..
echo "directory changed"
cd dir1
echo "directory changed"
#rename the file
mv file1.txt new_file.txt
echo "file renamed"
ls
#remove file
rm new_file.txt
echo"file removed"
cd ..
echo "directory changed"
rmdir dir1
echo "directory removed"
ls
OUTPUT
AIM:
To Write a shell script to demonstrate working with pipes
ALGORITHM:
Step 1: Initialize the `data` variable with the example text.
Step 2: Use a pipe (`|`) to pass the output of `echo "$data"` to the `grep` command, filtering
lines containing the word and displaying them.
Step 3: Use pipes to pass the output of `echo "$data"` to the `wc` command multiple times to
count the number of lines, words, and characters in the text and display the results separately.
Step 4: Use a pipe to pass the output of `echo "$data"` to the `tr` command, transforming all
characters to uppercase, and display the modified text.
Step 5: Use a pipe to pass the output of `echo "5 + 3"` to the `bc` command, performing a
simple arithmetic calculation and store the result.
Step 6: Define a custom data processing function `custom_process` that reads lines from the
pipe and prints lines starting with the letter 'L'.
Step 7: Use a pipe to pass the output of `echo "$data"` to the custom processing function,
`custom_process`, which processes the input text and prints lines starting with 'L'.
CODING
AIM:
To Write a shell script to demonstrate working with filters
ALGORITHM:
Step 1: Create and specify the input file as "demo.txt."
Step 2: Use the `grep` command to filter lines containing the word "Alice"
Step 3: Use the `awk` command to filter lines based on a specific pattern
Step 4: Use the `grep -v` command to exclude lines containing the word "Charlie"
Step 5: Use the `sed` command to replace occurrences of "Alice" with "Alicia" in the input file
Step 6: Use the `sort` command to sort the data in the input file in ascending order.
Step 7: Use the `wc -l` command to count the number of lines in the input file and display the
total line count, providing a basic count of data
CODING
input_file="demo.txt"
echo "Line containing 'Alice':"
grep "Alice" $input_file
echo -e"\nLine with ages less than 30:"
awk '$2<30 {print}' $input_file
echo -e"\nLine excluding 'charlie':"
grep -v "charlie" $input_file
echo -e"\nData with 'Alice' replaced by 'Alicia':"
sed 's/Alice/Alicia/' $input_file
echo -e"\nData sorted by age(ascending):"
sort -k2 -n $input_file
line_count=$(wc -l <"$input_file")
echo -e “: $line_count"
OUTPUT