I'm trying to write a Bash script that will be run in a folder containing multiple C source files. One of these will contain main(), and will also contain a multiline comment at the top that describes the file/program and its features.
What I have so far is:
#! /bin/bash
echo "Grepping for main *.c file"
file=$(grep -l main *.c)
echo "the file is ${file}"
comment=$(perl -lne 'print if (/\/\*/ .. /\*\//)' ${file})
echo ${comment}
Which tells me which file contains the main() (in the test case, it's main.c). The perl function then outputs (what I think to be) incorrectly. Instead of just searching in the file, it outputs
Grepping for main *.c file
the file is main.c
/$RECYCLE.BIN /Applications /Backup /Extra /Extra (from old Mac) 2 /Library /Network /OS X Install Data /System /System Volume Information /Users /Volumes /bin /boot CMakeLists.txt build comment.sh main.c main.dSYM makefile tasksheet.pdf Student Number: n9999999 CMakeLists.txt a1_n9999999 build comment.sh main.c main.dSYM makefile tasksheet.pdf...
followed by the titles of the contents of the folder, with the actual multiline comment scattered throughout the output.
If I try and cat main.c it outputs correctly, but if in the bash script I do echo cat main.c or cat ${file} i get a similar type of garbage output. What is causing it? I have a feeling it is to do with the fact that a multiline comment contains /*, which in Unix is 'everything in the root directory', but is there a way around this?
Edit: Running that perl command perl -lne 'print if (/\/\*/ .. /\*\//)' main.c in the terminal outputs the intended result.
cat main.cfrom the script gives different results thancat main.cfrom the command-line? If so, something is very wrong here; I suspect missing information in the question.Davids-Mac-Pro:Assignment 1 David$ cat main.c /* ** Student Number: n90000000 ** Name: David ** Extra functionality: ** List of functionality. */ // Standard Includes #include <stdio.h> #include <stdbool.h> #include <string.h>With a bash script containingecho $(cat main.c):/$RECYCLE.BIN /Applications /Backup (redacted) CMakeLists.txt a1_n9999999 build comment.sh main.c main.dSYM makefile tasksheet.pdf Student Number: (redacted) List of functionality. build/ main.dSYM/ // Standard Includes #include <stdio.h> #include <stdbool.h> #include <string.h>mainis not really sensitive enough. A file containing the wordremainingor any of the many other words containing the sequence of letters m-a-i-n will be matched. However, that's largely tangential to the issue at hand.grep -l -w mainto search for the word 'main'. However, that would pick up 'the main idea is …' and so on, in a comment. I'd probably exploit knowledge of how you format yourmain()function, usinggrep -l -E '^int main\((void|int argc, char \*\*argv)\)$to pick up eitherint main(void)orint main(int argc, char **argv)— they're the only signatures I ever use, and I keep the type on the same line as the function. If you put theinton the previous line, dropintfrom the match. You might still run into a 'main in a comment' problem, but…