Skip to content

Instantly share code, notes, and snippets.

@prashant1k99
Created January 20, 2025 09:48
Show Gist options
  • Save prashant1k99/54160762ceba4ceb55237a96cd366cd1 to your computer and use it in GitHub Desktop.
Save prashant1k99/54160762ceba4ceb55237a96cd366cd1 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Check if a filename was provided
if [ $# -eq 0 ]; then
echo "Error: Please provide a C source file name."
echo "Usage: $0 <filename.c>"
exit 1
fi
# Store the input filename
SOURCE_FILE="$1"
# Check if the file exists
if [ ! -f "$SOURCE_FILE" ]; then
echo "Error: File '$SOURCE_FILE' does not exist."
exit 1
fi
# Extract the filename without extension
EXECUTABLE="${SOURCE_FILE%.*}"
# Compile the program
gcc -o "$EXECUTABLE" "$SOURCE_FILE"
# Check if compilation was successful
if [ $? -ne 0 ]; then
echo "Compilation failed."
exit 1
fi
# Run the program
# echo "Running $EXECUTABLE:"
./"$EXECUTABLE"
# Store the exit status of the program
EXIT_STATUS=$?
# Remove the executable
rm "$EXECUTABLE"
# Exit with the original program's exit status
exit $EXIT_STATUS
@prashant1k99
Copy link
Author

This is the script I use to compile and delete the residue of my C code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment