0% found this document useful (0 votes)
20 views8 pages

linux_lab_pgms

Uploaded by

Sheethal P P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views8 pages

linux_lab_pgms

Uploaded by

Sheethal P P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Operating System with Linux (PMCAC12)

Operating System with Linux laboratory Manual


(PMCAC12)

Steps to write and execute the Linux script:

Step 1: open the terminal and type “vi pgm_name.sh”


Step 2: when the vi editor is opened press “i” for write mode
Step 3: type the program, and at the end press “esc” key and then “:wq” to save and quit from vi
editor.
Step 4: then in the terminal type “chmod +x pgm_name.sh” to make the script executable.
Step 5: then at last type “./pgm_name.sh” and get the output.

Programs:
1. Write a script to find the factorial of a number using loops
Source code:
#!/bin/bash

echo -n "Enter a number: "


read num
factorial=1
# Check if the number is valid (non-negative)
if [ $num -lt 0 ]; then
echo "Factorial of a negative number is undefined."
exit 1
fi

# Calculate the factorial using a for loop


for (( i=1; i<=num; i++ ))
do
factorial=$((factorial * i))
done
echo "The factorial of $num is $factorial"

Output:

Case 1:
Enter a number: 4
The factorial of 4 is 24
Case 2:
Enter a number: -6
Factorial of a negative number is undefined.

Prof. Sheethal P P, Asst. Professor, Dept. of MCA, EWIT 1


Operating System with Linux (PMCAC12)

2. Write a script to check if a given year is a leap year.

Source code:
#!/bin/bash

echo -n "Enter a year: "


read year

# Check if the input is valid (positive integer)


if ! [[ "$year" =~ ^[0-9]+$ ]]; then
echo "Invalid input. Please enter a valid year."
exit 1
fi

# Check leap year conditions


if (( (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 )); then
echo "$year is a leap year."
else
echo "$year is not a leap year."
fi

Output:

case 1:
Enter a year: 2026
2026 is not a leap year.
case 2:
Enter a year: 2016
2016 is a leap year.

Prof. Sheethal P P, Asst. Professor, Dept. of MCA, EWIT 2


Operating System with Linux (PMCAC12)

3. Write a script to sum the digits of a given number using a while loop

source code:

#!/bin/bash

echo -n "Enter a number: "


read num

# Check if the input is valid (non-negative integer)


if ! [[ "$num" =~ ^[0-9]+$ ]]; then
echo "Invalid input. Please enter a non-negative integer."
exit 1
fi

sum=0

# Loop to calculate the sum of digits


while [ $num -gt 0 ]; do
digit=$((num % 10)) # Extract the last digit
sum=$((sum + digit)) # Add the digit to the sum
num=$((num / 10)) # Remove the last digit
done

echo "The sum of the digits is $sum"

Output:

Enter a number: 123


The sum of the digits is 6

Prof. Sheethal P P, Asst. Professor, Dept. of MCA, EWIT 3


Operating System with Linux (PMCAC12)

4. Write a script to reverse a string using a for loop.

Source code:
#!/bin/bash

read -p "Enter a string: " input

reversed=""

# Get the length of the input string


length=${#input}

# Use a for loop to iterate through the string in reverse


for (( i=$length-1; i>=0; i-- ))
do
# Append each character to the reversed string
reversed="$reversed${input:$i:1}"
done

echo "Reversed string: $reversed"

Output:

Enter a string: demo


Reversed string: omed

Prof. Sheethal P P, Asst. Professor, Dept. of MCA, EWIT 4


Operating System with Linux (PMCAC12)

5. Write a script to count the number of lines, words, and characters in a file.

Source code:

#!/bin/bash

# Check if a file is provided as an argument


if [ $# -eq 0 ]; then
echo "Usage: $0 <filename>"
exit 1
fi

# Assign the filename from the first argument


filename=$1

# Check if the file exists


if [ ! -f "$filename" ]; then
echo "Error: File '$filename' not found."
exit 1
fi

# Use wc (word count) command to get the counts


lines=$(wc -l < "$filename")
words=$(wc -w < "$filename")
characters=$(wc -m < "$filename")

# Display the results


echo "File: $filename"
echo "Number of lines: $lines"
echo "Number of words: $words"
echo "Number of characters: $characters"

Steps to create and execute the file:


step 1 : create a file, for eg: filename.txt, using command “cat>filename.txt”

step 2: type a word or line or sentence in that file and click CTRL + C.

cat>demo1.txt
hello, welcome to linux programming
^C

step 3: now execute the script file by “ chmod +x pgm_name.sh”


step 4: run the script using “./pgm_name.sh filename.txt”

Output:
File: demo1.txt
Number of lines: 1
Number of words: 5
Number of characters: 36

Prof. Sheethal P P, Asst. Professor, Dept. of MCA, EWIT 5


Operating System with Linux (PMCAC12)

6. Write a script to check if a file exists and whether it is readable, writable, and executable.

Source code:

#!/bin/bash

# Check if a file is provided as an argument


if [ $# -eq 0 ]; then
echo "Usage: $0 <filename>"
exit 1
fi

filename=$1

# Check if the file exists


if [ -e "$filename" ]; then
echo "File '$filename' exists."

# Check if the file is readable


if [ -r "$filename" ]; then
echo "File '$filename' is readable."
else
echo "File '$filename' is not readable."
fi

# Check if the file is writable


if [ -w "$filename" ]; then
echo "File '$filename' is writable."
else
echo "File '$filename' is not writable."
fi

# Check if the file is executable


if [ -x "$filename" ]; then
echo "File '$filename' is executable."
else
echo "File '$filename' is not executable."
fi
else
echo "File '$filename' does not exist."
fi

Output:

File 'demo1.txt' exists.


File 'demo1.txt' is readable.
File 'demo1.txt' is writable.
File 'demo1.txt' is not executable.

Prof. Sheethal P P, Asst. Professor, Dept. of MCA, EWIT 6


Operating System with Linux (PMCAC12)

7. Write a script to display the current date, username, and hostname.

Source code:

#!/bin/bash

# Get the current date


current_date=$(date)

# Get the username


current_user=$(whoami)

# Get the hostname


current_host=$(hostname)

# Display the information


echo "Current Date: $current_date"
echo "Username: $current_user"
echo "Hostname: $current_host"

Output:

Current Date: Sunday 15 December 2024 09:40:49 AM IST


Username: user
Hostname: pp-s-ubuntu

Prof. Sheethal P P, Asst. Professor, Dept. of MCA, EWIT 7


Operating System with Linux (PMCAC12)

8. Write a script to search for a specific word in a file and print the line containing that word.

Source code:

#!/bin/bash

# Check if the correct number of arguments is provided


if [ $# -ne 2 ]; then
echo "Usage: $0 <filename> <word>"
exit 1
fi

# Assign arguments to variables


filename=$1
word=$2

# Check if the file exists


if [ ! -f "$filename" ]; then
echo "Error: File '$filename' not found."
exit 1
fi

# Search for the word in the file


grep -n "$word" "$filename"

# Check the exit status of grep to determine if the word was found
if [ $? -eq 0 ]; then
echo "Search complete."
else
echo "The word '$word' was not found in the file '$filename'."
fi

Steps to create and execute the file:


step 1 : create a file, for eg: filename.txt, using command “cat>filename.txt”

step 2: type a word or line or sentence in that file and click CTRL + C.

cat>demo1.txt
hello, welcome to linux programming
^C

step 3: now execute the script file by “ chmod +x pgm_name.sh”


step 4: un the script using “./pgm_name.sh filename.txt <word>”

Output:

1:hello, welcome to linux programming


Search complete.

Prof. Sheethal P P, Asst. Professor, Dept. of MCA, EWIT 8

You might also like