Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Script that execute C by Bash

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"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading