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


Comments

Popular posts from this blog

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