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:




No comments:

Post a Comment

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...