Sunday, March 17, 2024

Programs 4 to 10 B.Sc Computer Science BCA Linux and Shell Programming - Lab Bharathiar University

 

B.Sc Computer Science 

Linux and Shell Programming - Lab  

Bharathiar University 

4.Write a shell script for displaying current date, user name, file listing and directories by getting user choice. 

Source Code


while true; do

echo "Select an option:"
echo "1. Display current date"
echo "2. Display current user name"
echo "3. List files in the current directory"
echo "4. List directories in the current directory"
echo "5. Exit"
echo "Enter your choice: "
read choice

case $choice in
    1)  echo "Current date: $(date)"
      echo;;
    2)  echo "Current user name: $(whoami)"
        echo;;
    3)  echo "Files in the current directory:"
        ls -l | grep "^-"
        echo;;
    4)  echo "Directories in the current directory:"
        ls -l | grep "^d"
        echo;;
    5)  echo "Exiting..."; exit;;
    *)  echo "Invalid choice. Please enter a valid option (1/2/3/4)." ;;
esac
done

Sample OUTPUT:









5. Write a shell script to implement the filter commands.
 

Source Code


text="apple banana grape orange
apple grape lemon
banana lemon orange
grape lemon orange"

# Using grep to filter lines containing a specific word
echo "Lines containing the word 'grape':"
echo "$text" | grep 'grape'

echo

# Using sort to sort lines alphabetically
echo "Sorted lines:"
echo "$text" | sort

echo

# Using wc to count the number of words, lines, and characters
echo "Word count:"
echo "$text" | wc -w
echo "Line count:"
echo "$text" | wc -l
echo "Character count:"
echo "$text" | wc -c
echo

# Using tr to translate characters to uppercase
echo "Text converted to uppercase:"
echo "$text" | tr '[:lower:]' '[:upper:]'

SAMPLE OUTPUT:



 6. Write a shell script to remove the files which has file size as zero bytes.

 

Source Code


# Find files with size zero and remove them
find . -type f -size 0 | while read file; do
    rm "$file"
    echo "Removed: $file"
done


SAMPLE OUTPUT:





7. Write a shell script to find the sum of the individual digits of a given number.
  

Source Code


sum_of_digits() {
    number=$1
    sum=0
   
    # Loop through each digit of the number
    while [ $number -gt 0 ]; do
        # Extract the last digit
        digit=$((number % 10))
        # Add the digit to the sum
        sum=$((sum + digit))
        # Remove the last digit from the number
        number=$((number / 10))
    done

    # Print the sum of digits
    echo "Sum of digits: $sum"
}

# Prompt user for input
echo "Enter a number: "
read num

# Call the function with the provided number
sum_of_digits $num


SAMPLE OUTPUT:




8. Write a shell script to find the greatest among the given set of numbers using command line arguments.
  

Source Code


# Check if no arguments are provided
if [ $# -eq 0 ]; then
    echo "Please Enter: $0 <number1> <number2> ... <numberN>"
    exit 1
fi

# Initialize variable to store the maximum value
max=$1

# Loop through all command-line arguments
for num in "$@"; do
    # Compare each number with the current maximum
    if [ $num -gt $max ]; then
        max=$num
    fi
done

# Print the maximum value
echo "The greatest number is: $max"


SAMPLE OUTPUT:



9. Write a shell script for palindrome checking.

  
  

Source Code


# Main program starts here

# Prompt the user to enter the string
    echo "Enter the string: "
    read str
    reversed=$(echo "$str" | rev)
    if [ "$str" = "$reversed" ]; then
        echo "The string '$str' is a palindrome."
    else
        echo "The string '$str' is not a palindrome."
    fi



SAMPLE OUTPUT:




10. Write a shell script to print the multiplication table of the given argument using for loop.

  
  

Source Code


# Check if argument is provided
if [ $# -eq 0 ]; then
    echo "Please Enter: <number>"
    exit 1
fi

# Store the number from command-line argument
number=$1

# Print the multiplication table
echo "Multiplication table of $number:"
for (( i = 1; i <= 10; i++ )); do
    # Perform multiplication
    result=$((number * i))
    # Print the result
    echo "$number x $i = $result"
done



SAMPLE OUTPUT:




Saturday, March 16, 2024

Program 2 B.Sc Computer Science BCA Linux and Shell Programming - Lab Bharathiar University - Write a shell script to show the following system configuration

 

B.Sc Computer Science 

Linux and Shell Programming - Lab  

Bharathiar University 

Program  2. Write a shell script to show the following system configuration :

a. currently logged user and his log name
b. current shell , home directory , Operating System type , current Path setting , current
working directory
c. show currently logged number of users, show all available shells
d. show CPU information like processor type , speed 
e. show memory information


Source Code

echo "Currently logged user and his log name:"
echo " Username: $(whoami)"
echo " Login name: $(logname)"
echo

echo " Current shell: $SHELL"
echo " Home directory: $HOME"
echo " Operating System type: $(uname -s)"
echo " Current Path setting: $PATH"
echo " Current working directory: $(pwd)"
echo

echo " Currently logged users: $(who | wc -l)"
echo " Available shells: $(cat /etc/shells)"
echo

echo " Processor type: $(uname -p)"
echo " Processor speed:"
echo " $(cat /proc/cpuinfo | grep "MHz")"
echo
echo "Memory information:"
echo "$(free -h)"



SAMPLE OUTPUT:




Program 3 B.Sc Computer Science BCA Linux and Shell Programming - Lab Bharathiar University - Write a Shell Script to implement the following: pipes, Redirection and tee commands

 

B.Sc Computer Science 

Linux and Shell Programming - Lab  

Bharathiar University 

Program  3.  Write a Shell Script to implement the following: pipes, Redirection and tee commands


Source Code

# Demonstrate pipes
echo "Adding two numbers with use of pipes"
echo "Enter two numbers to perform addition:"
read a
read b
echo "Addition of $a and $b is:"
echo "$a + $b" | bc
echo

# Define a filename
filename="output.txt"

# Demonstrate redirection
echo "Redirection to output.txt file:" > $filename
echo "Hello, world!" >> $filename
echo "Goodbye, world!" >> $filename

echo "Contents of output.txt file:"
cat $filename
echo

# Demonstrate tee command
echo "Display and Writes to file using tee command:"
echo "Hai friend!" | tee -a $filename
echo "How are you?" | tee -a $filename

echo
# Display the contents of the file
echo "Contents of $filename after tee command:"
cat $filename


SAMPLE OUTPUT:



Program 1 B.Sc Computer Science BCA Linux and Shell Programming - Lab Bharathiar University - Write a shell script to stimulate the file commands: rm, cp, cat, mv, cmp, wc, split, diff.

 

B.Sc Computer Science 

Linux and Shell Programming - Lab  

Bharathiar University 

Program 1-  Write a shell script to stimulate the file commands: rm, cp, cat, mv, cmp, wc, split, diff. 


Source Code

# Create sample files
echo "Welcome all of you." > f1.txt
echo "Thanks for coming" > f2.txt

# Display the contents of file 1
echo "Contents of file 1:"
cat f1.txt
echo

# Display the contents of file 2
echo "Contents of file 2:"
cat f2.txt
echo

# Copy file 1 to file3
cp f1.txt f3.txt
echo "file 1 copied to file 3."

# Display the contents of file 3
echo "Contents of file 3:"
cat f3.txt
echo

# Rename file 3 to sample.txt
mv f3.txt sample.txt
echo "file 3 renamed to sample.txt."

# Display the contents of sample.txt
echo "Contents of sample.txt:"
cat sample.txt
echo

# Compare file 1 and sample.txt
cmp -s f1.txt sample.txt
if [ $? -eq 0 ]; then
    echo "file 1 and sample.txt are identical."
else
    echo "file 1 and sample.txt are different."
fi
echo

# Display the word count of file 1
echo "Word count of file 1: $(wc f1.txt)"
echo

# Split file 1 into two parts
split -n 2 f1.txt file_
echo "file 1 split into two parts: file_aa and file_ab."
echo "Contents of file_aa:"
cat file_aa
echo

echo "Contents of file_ab:"
cat file_ab
echo

# Display the differences between file1.txt and file2.txt
diff f1.txt f2.txt
echo

# Clean up: Remove temporary files
rm f1.txt f2.txt sample.txt file_aa file_ab
echo "Temporary files removed."


Sample OUTPUT:




Sunday, March 10, 2024

Program 10 B.Sc Computer Science with Data analytics Linux and Shell Programming - Lab Bharathiar University - Write a shell script to find the factorial of given integer.

 

B.Sc Computer Science  with Data analytics

Linux and Shell Programming - Lab  

Bharathiar University 

Program 10-  Write a shell script to find the factorial of given integer. 


Source Code


# Function to calculate factorial
factorial() {
    num=$1
    fact=1
    while [ $num -gt 1 ]; do
        fact=$((fact * num))
        num=$((num - 1))
    done
    echo $fact
}

# Main program starts here

# Prompt the user to enter the number
    echo "Enter a number to find factorial: "
    read n

# Calculate the factorial of the given integer
result=$(factorial $n)
echo "The factorial of $n is: $result"


Sample OUTPUT:
 


Saturday, March 9, 2024

Program 9 B.Sc Computer Science with Data analytics Linux and Shell Programming - Lab Bharathiar University - Write a shell script to check whether given string is palindrome or not.

 

B.Sc Computer Science  with Data analytics

Linux and Shell Programming - Lab  

Bharathiar University 

Program 9-  Write a shell script to check whether given string is palindrome or not. 


Source Code


# Main program starts here


# Prompt the user to enter the string

    echo "Enter the string: "

    read str

    reversed=$(echo "$str" | rev)

    if [ "$str" = "$reversed" ]; then

        echo "The string '$str' is a palindrome."

    else

        echo "The string '$str' is not a palindrome."

    fi



Sample OUTPUT:



Program 8 B.Sc Computer Science with Data analytics Linux and Shell Programming - Lab Bharathiar University - Write a shell script to find the GCD of two given numbers.

 

B.Sc Computer Science  with Data analytics

Linux and Shell Programming - Lab  

Bharathiar University 

Program 8-  Write a shell script to find the GCD of two given numbers. 


Source Code


 # Function to find the GCD using the Euclidean algorithm

gcd() {

    dividend=$1

    divisor=$2


    while [ $divisor -ne 0 ]; do

        remainder=$((dividend % divisor))

        dividend=$divisor

        divisor=$remainder

    done


    echo "$dividend"

}


# Main program starts here


# Prompt the user to enter the first number

    echo "Enter the first number: "

    read num1


    # Prompt the user to enter the second number

    echo "Enter the second number: "

    read num2

   

# Find the GCD of the two numbers

result=$(gcd $num1 $num2)

echo "The GCD of $num1 and $num2 is: $result"


Sample Output:



Thursday, March 7, 2024

Program 7 B.Sc Computer Science with Data analytics Linux and Shell Programming - Lab Bharathiar University - Write a Shell script to print the first n Fibonacci numbers

 

B.Sc Computer Science  with Data analytics

Linux and Shell Programming - Lab  

Bharathiar University 

Program 7- Write a Shell script to print the first n Fibonacci numbers


Source Code


# Function to print the first n Fibonacci numbers
print_fibonacci() {

    # Initialize variables for the first two Fibonacci numbers
    num1=0
    num2=1
    count=0

    # Print the first two Fibonacci numbers
    echo "The first $n Fibonacci numbers are:"
    echo "$num1"
    echo "$num2"

    # Calculate and print the remaining Fibonacci numbers
    while [ $count -lt $(($n - 2)) ]; do
        next=$((num1 + num2))
        echo "$next"
        num1=$num2
        num2=$next
        count=$((count + 1))
    done
}

# Main program starts here
# Prompt the user to enter the number
    echo "Enter the a number of fibonacci series to print: "
    read n

# Check if n is a positive integer
if  [[ "$n" -lt 1 ]]; then
    echo "Error: Please provide a positive integer."
    exit 1
fi

# Print the first n Fibonacci numbers
print_fibonacci $n


Sample Output:


Tuesday, March 5, 2024

Program 6 B.Sc Computer Science with Data analytics Linux and Shell Programming - Lab Bharathiar University - Write a Shell script to determine whether a given number is a prime number or not.

 


B.Sc Computer Science  with Data analytics

Linux and Shell Programming - Lab  

Bharathiar University 

Program 6- Write a Shell script to determine whether a given number is a prime number or not.

Source Code



# Function to check if a number is prime
is_prime() {
    if [ $1 -le 1 ]; then
        echo "$1 is not a prime number."
        exit 0
    fi
   
    d=2
    while [ $d -lt $1 ]; do
        if [ $(($1 % $d)) -eq 0 ]; then
            echo "$1 is not a prime number."
            exit 0
        fi
        d=$((d + 1))
    done
    echo "$1 is a prime number."
}

# Main program starts here

# Prompt the user to enter the number
    echo "Enter the number: "
    read number
   
# Check if the number is prime
is_prime $number


Sample output:



Program 5 B.Sc Computer Science with Data analytics Linux and Shell Programming - Lab Bharathiar University -Write a shell script (small calculator) that adds, subtracts, multiplies and divides the two given numbers.

 

B.Sc Computer Science  with Data analytics

Linux and Shell Programming - Lab  

Bharathiar University 

Program 5- -Write a shell script (small calculator) that adds, subtracts, multiplies and divides the two given numbers. 

Source Code



# Function to perform addition
addition() {
    echo "$num1 + $num2" | bc
}

# Function to perform subtraction
subtraction() {
    echo "$num1 - $num2" | bc
}

# Function to perform multiplication
multiplication() {
    echo "$num1 * $num2" | bc
}

# Function to perform division
division() {
    if [ "$num2" -eq 0 ]; then
        echo "Error: Division by zero!"
    else
        echo "scale=2; $num1 / $num2" | bc
    fi
}

# Main script starts here

# Prompt the user to enter the first number
echo "Enter the first number: "
    read num1

    # Prompt the user to enter the second number
    echo "Enter the second number: "
    read num2
       
while true; do
    # Prompt the user to select the operation
    echo "Select the operation:"
    echo "1. Addition"
    echo "2. Subtraction"
    echo "3. Multiplication"
    echo "4. Division"
    echo "5. Exit"
    echo "Enter your choice: "
    read choice

       
    case $choice in
        1) echo "Addition: $(addition)";;
        2) echo "Subtraction: $(subtraction)";;
        3) echo "Multiplication: $(multiplication)";;
        4) echo "Division: $(division)";;
        5) echo "Exiting..."; exit;;
        *) echo "Invalid choice";;
    esac
done


SAMPLE OUTPUT:




Program 12 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 12 Implement a telephone directory using files

  BCA Object Oriented Programming using C++ Practical Madras University  Program 12  Implement a telephone directory using files SOURCE CODE...