Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8632297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:19:56+00:00 2026-06-12T09:19:56+00:00

I am trying to write a shell program that will search my current directory

  • 0

I am trying to write a shell program that will search my current directory (say, my folder containing C code), read all files for the keywords “printf” or “fprintf”, and append the include statement to the file if it isn’t already done.

I have tried to write the search portion already (for now, all it does is search files and print the list of matching files), but it is not working. Included below is my code. What am I doing wrong?

Code

EDIT: New code.

#!/bin/sh
#processes files ending in .c and appends statements if necessary

#search for files that meet criteria
for file in $( find . -type f )
do
    echo $file
    if grep -q printf "$file"
    then
        echo "File $file contains command"
    fi
done
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:19:57+00:00Added an answer on June 12, 2026 at 9:19 am

    To execute commands in a subshell you need $( command ). Notice the $ before the parenthesis.

    You don’t need to store the list of files in a temporary variable, you can directly use

    for file in $( find . ) ; do
        echo "$file"
    done
    

    And with

    find . -type f | grep somestring
    

    you are not searching the file content but the file name (in my example all the files which name contains “somestring”)

    To grep the content of the files:

    for file in $( find . -type f ) ; do
      if  grep -q printf "$file" ; then
        echo "File $file contains printf"
      fi
    done
    

    Note that if you match printf it will also match fprintf (as it contains printf)

    If you want to search just files ending with .c you can use the -name option

    find . -name "*.c" -type f
    

    Use the -type f option to list only files.

    In any case check if your grep has the -r option to search recursively

    grep -r --include "*.c" printf .
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a shell script that, when run, will set some environment
Im trying to write a program that runs some shell commands wit emulated user
I'm trying to write a shell program that, given an unsorted list of numbers
I'm trying to write a shell program that, given an argument, prints the name
I am trying to write a shell script that looks if a particular program
I m trying write code that after reset set up rrpmax as 3000. It
Trying to write app for service technicians that will display open service calls within
Trying to write a couple of functions that will encrypt or decrypt a file
I am trying to write a shell script to delete all the sub directories
I am trying to write shell script to read the value from /sys/class/net/eth0/carrier but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.