Issue
What should a script that compiles and executes a C program look like? By condition, the script must be run with the following flags: gcc -Wall -Werror -Wextra -o
In my understanding, when running the script, I have to enter the name of the program file
% gcc -Wall -Werror -Wextra "file_name".c -o "file_name"
% ./"file_name"
Solution
The script can take the name of the program as an argument, which you access using $1
. Then substitute that for the file name in the commands.
#!/bin/sh
prog="$1"
if gcc -Wall -Werror -Wextra "$prog".c -o "$prog"
then "./$prog"
else
echo "$prog.c did not compile successfully"
fi
Answered By - Barmar Answer Checked By - David Goodson (WPSolving Volunteer)