Posts

16.Write a shell script to illustrate the use of pipe command and change system date.

 #!/bin/bash # Check if a filename was provided if [ $# -ne 1 ]; then     echo "Usage: $0 filename"     exit 1 fi # Assign the filename to a variable filename=$1 # Check if the file exists if [ ! -f "$filename" ]; then     echo "Error: File '$filename' not found!"     exit 1 fi # Use wc to count lines, words, and characters lines=$(wc -l < "$filename") words=$(wc -w < "$filename") chars=$(wc -c < "$filename") # Print the results echo "File: $filename" echo "Lines: $lines" echo "Words: $words" echo "Characters: $chars"

10.Write a shell script to count the number words,lines,character from given file accept file name from commandline.

 #!/bin/bash # Check if a filename was provided if [ $# -ne 1 ]; then     echo "Usage: $0 filename"     exit 1 fi # Assign the filename to a variable filename=$1 # Check if the file exists if [ ! -f "$filename" ]; then     echo "Error: File '$filename' not found!"     exit 1 fi # Use wc to count lines, words, and characters lines=$(wc -l < "$filename") words=$(wc -w < "$filename") chars=$(wc -c < "$filename") # Print the results echo "File: $filename" echo "Lines: $lines" echo "Words: $words" echo "Characters: $chars"

3.Write a shell script to print below pattern 1 1 2 1 1 3 3 1

   #!/bin/bash # Number of rows for the pattern rows=3 # Function to calculate binomial coefficient binomial_coefficient() {     n=$1     k=$2     res=1     for (( i=0; i<k; i++ )); do         res=$((res * (n - i) / (i + 1)))     done     echo $res } # Generate Pascal's Triangle for (( i=0; i<rows; i++ )); do     for (( j=0; j<=i; j++ )); do         # Print binomial coefficient         echo -n "$(binomial_coefficient $i $j) "     done     echo "" # Move to the next line done

2.Write a shell script to compile all C files and delete those files having errors.

  #!/bin/bash # Compile each .c file in the current directory for file in *.c; do     # Check if there are any .c files     if [ ! -e "$file" ]; then         echo "No .c files found."         exit 1     fi  # Try to compile the .c file     gcc "$file" -o "${file%.c}.out"  # Check if the compilation was successful     if [ $? -ne 0 ]; then         echo "Compilation failed for $file. Deleting the file."         rm "$file"     else         echo "Compiled $file successfully."     fi done

1.Write a shell script to print below pattern

   #!/bin/bash # Number of rows for the pattern rows=4 # Loop through each row for ((i=0; i<rows; i++)); do     # Print numbers from 0 to i     for ((j=0; j<=i; j++)); do         echo -n "$j "     done     # Move to the next line after each row     echo done

18.Write a shell script to delete all empty lines from any file

 #!/bin/bash # Check if a file name was provided if [ -z "$1" ]; then     echo "Usage: $0 filename"     exit 1 fi # Remove empty lines from the specified file sed -i '/^$/d' "$1" echo "Empty lines removed from $1."

19.Write a shell script to change case of string.

  #!/bin/bash # Read input from the user read -p "Enter a string: " input # Prompt for case conversion choice echo "Choose an option:" echo "1. Convert to UPPERCASE" echo "2. Convert to lowercase" read -p "Enter choice (1 or 2): " choice # Change case based on user choice if [ "$choice" -eq 1 ]; then     output=$(echo "$input" | tr '[:lower:]' '[:upper:]')     echo "Converted to UPPERCASE: $output" elif [ "$choice" -eq 2 ]; then     output=$(echo "$input" | tr '[:upper:]' '[:lower:]')     echo "Converted to lowercase: $output" else     echo "Invalid choice. Please enter 1 or 2."     exit 1 fi

20.Write a shell script to delete all text files from the directory

  #!/bin/bash # Specify the directory (or use the current directory) dir=${1:-.} # Confirm deletion with the user read -p "Are you sure you want to delete all .txt files in the directory $dir? (y/n): " confirm if [[ $confirm == [yY] ]]; then     # Delete all .txt files in the specified directory     rm -f "$dir"/*.txt     echo "All .txt files deleted from $dir." else     echo "Operation canceled." fi

14.Write a shell script to reverse array elements.

  #!/bin/bash arr=("apple" "banana" "cherry" "date") # Function to reverse the array reversed=() for ((i=${#arr[@]}-1; i>=0; i--)); do     reversed+=("${arr[i]}") done echo "Reversed array elements:" for element in "${reversed[@]}"; do     echo "$element" done

13.Write a shell script to elements of array

  #!/bin/bash # Correct array initialization arr=("apple" "banana" "cherry" "date") # Print array elements echo "Array elements:" for element in "${arr[@]}"; do     echo "$element" done

12.Write a shell to reverse a string

  #!/bin/bash read -p "Enter a string: " input # Reverse the string reversed=$(echo "$input" | rev) echo "Reversed string: $reversed"