0

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"
3
  • 2
    What you have is not unreasonable, but you probably should not execute the code unless it is built successfully. ie, gcc ... "$file".c && ./"$file". Commented Jun 29, 2022 at 16:11
  • Related? github.com/ryanmjacobs/c Commented Jun 29, 2022 at 16:15
  • 1
    There could be strong cybersecurity issues. You need to trust the human who gave you that C code. Commented Jun 29, 2022 at 16:16

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0
#!/bin/sh

gcc -Wall -Werror -Wextra "$1".c -o "$1" && "./$1" || echo "Unable to compile and run $1"

Compile with gcc and run a program if it is successfully compiled, otherwise show the message to user.

Run the script like

script_name file_name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.