Posts

Showing posts from October, 2024

Long questions

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

Shorts question

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" 13 Display elements of  an 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 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 15.Demonstrate the use of $$,$0,$*,$@  #!/bin/bash # $$ - Process ID of the script echo "Process ID of this script:...