Created
January 20, 2025 09:48
-
-
Save prashant1k99/54160762ceba4ceb55237a96cd366cd1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the script I use to compile and delete the residue of my C code