Cencoroll
Cencoroll

Reputation: 37

Getting output of a c file into a bash variable

I have a c file that checks whether there is a ! in the argument given (ie ./a.out hi!) and return 0 if it does and 1 if it doesn't, which works as it's supposed to.

I need to make a bash shell file (.sh) that uses the c file to check if files in a directory contain the character, ie. if the directory has dogs!.sh fly!.c ring.txt, executing ./script.sh should return

dogs!.sh
fly!.c

But I have no idea how to do so? Can anybody help out?

Upvotes: 2

Views: 107

Answers (1)

Carl Norum
Carl Norum

Reputation: 224944

Your compile line:

gcc includes.c

Will produce an output program called a.out. You need to run that command, not try to execute your C source as a shell script, which is what your current script is doing. Example:

ret=$(./a.out ${file})

You don't need the ret, though, since your program has no output. Just check the exit value.

if [[ $? -eq 0 ]]

Editorial note: this answer assumes that you've copy/pasted something wrong when asking this question, since your error message shows alpha.c, but that's not mentioned anywhere else. And that you fix the syntax errors in your C program, too!

Upvotes: 3

Related Questions