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

  • Home
  • SEARCH
  • 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 7820437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:17:19+00:00 2026-06-02T07:17:19+00:00

When I use grep in TEST (line 18) it works perfectly. When I do

  • 0

When I use grep in TEST (line 18) it works perfectly. When I do the exact same command, but inside the for-loop (line 40), it does not work. Why? I’ve bent my brain on this problem too long now.

#!/bin/bash
clear           

sourcelist=`ls *.c`
headerlist=`ls *.h`
FILE="headers.txt"

TEST="receive_elevator_data" #this is one typical entry in headers.tex
echo "TEST"
grep $TEST *.c -n

MODE="h"

if [ "$MODE" = "h" ]
then
echo "Searching.."
#for entry in $(cut -f 1 $FILE)
for entry in `cat $FILE`
    do
    echo "Looking at entry in $FILE: "
    echo $entry
    echo "Press any button to search, <s> for skip, <e> to exit"
    read -e INPUT2
    if [ "$INPUT2" = "s" ]
    then
        continue
    fi
    if [ "$INPUT2" = "n" ]
    then
        exit 1 #exit shell script
    else
        grep -n "${entry}" ${sourcelist}
    fi
done
fi

Just to clarify: every line in headers.txt has strings like “test”, i.e no space in the lines. What headers.txt really contains is a list of function names extracted from all headerfiles in a C project 🙂 There was no returned error in my previous code, it was just the grep command in the loop that wouldn’t run. What I want to do, is to search through all C files for each function in headers.txt, and prompt me before each search

  • 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-02T07:17:22+00:00Added an answer on June 2, 2026 at 7:17 am

    grep probably fails because a filename contains spaces.

    dont use capital letter vars. those are better used by the environment.
    never try to parse ls output. use globbing instead.
    if you’re writting bash, then prefer [[ over [.
    do not cat file to read it. instead read it in a while loop.
    always quote your variables. they’re also easier found if grouped.
    treat a list of files, as an array.
    if you have to use `` prefer $() which can be nested, and also quote it (foo="$(cmd ..)")

    #!/bin/bash
    clear           
    
    sourcelist=(*.c)
    headerlist=(*.h)
    file="headers.txt"
    test="receive_elevator_data" # this is one typical entry in headers.tex
    mode="h"
    
    echo "searching test"
    grep -n "$test" "${sourcelist[@]}"
    
    if [[ "$mode" == "h" ]]; then
        echo "Searching.."
        while read -r entry; do
            printf "Looking at entry in %s: %s\n" "$file" "$entry"
            read -p "Press any button to search, <s> for skip, <e> to exit" -e answer
            [[ "$answer" = "s" ]] && continue
            [[ "$answer" = "e" ]] && exit 1 # exit shell script
            grep -n "$entry" "${sourcelist[@]}"
        done < "$file"
    fi
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We can use grep to return the lines with matched words, but how to
I code in vim. I use git; and love git grep. Does anyone have
How can I use Microsoft findstr to grep an item from a single line?
windows xp, gnu grep from http://gnuwin32.sourceforge.net/packages/grep.htm I have file with line breaks: test 123
Obviously I dont get the way grep works in R. If I use grep
I'm trying to use grep with -v for invert-match along with -e for regular
On Linux, I often use grep with context to find usage information in man
I'm trying to use ack-grep as a replacement for grep + find in Emacs
:vimgrep looks like a really useful thing. Here's how to use it: :vim[grep][!] /{pattern}/[g][j]
I usually use the following pipeline to grep for a particular search string and

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.