Need this in an hour You are tasked with writing a script that will find all the
ID: 3703987 • Letter: N
Question
Need this in an hour
You are tasked with writing a script that will find all the C function given a directory. Your script will have to recursively look through the directory and find all .c files and output the functions contained within them.
Given the directory structure:
Your code should retrieve all functions from:
* mysql_bridge.c
* postgres_bridge.c
* main.c
* db_adapter.c
Your script should output "Unable to find any functions" if no source files were encountered.
Specifically, the only source files that should be search for are .c files (headers are ignored)
This will be an opportune time to utilise regular expressions in your script. Your task should execute within Ed's linux environment. If it fails to execute properly, you will receive no marks for this task.
Your script requires using taking a command line argument which should be the root of the project.
Example 1:
Example 2: Bad directory
Example 3: Unable to find functions
Explanation / Answer
CODE:
findfn.sh
if [ $# -ne 1 ]
then
echo "USGAE: $BASH_SOURCE <project folder>"
exit 1
fi
Dir=$1
count=0
## if directory is passed
if [ -d "${Dir}" ] ; then
Files=`find $Dir -name "*.c"`
# for each C files in directory.
for file in $Files
do
echo $file
echo `grep -r "^(w+) +w+ *([w,-*]*)" $file`
temp_count=`grep -r "^(w+) +w+ *([w,-*]*)" $file | wc -l`
count=`expr $count + $temp_count`
done
if [ $count -eq 0 ]
then
echo "unable to find any funntions"
fi
else
echo "$Dir is not a directory"
OUTPUT:
$ ./findfn.sh ~/temp/
/home/rojindal/temp/main.c
int main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.