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 9096851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:57:38+00:00 2026-06-16T23:57:38+00:00

I’m sorry to be posting about another problem with the same script that I

  • 0

I’m sorry to be posting about another problem with the same script that I was working on yesterday.

Originally I had a problem with it cding to paths with spaces, though that is fixed now. The problem is that if a 3rd argument is given to the script, it is to search for it within the files it found earlier and then print the files location as well as the line number the term was found on.

For some reason grep isn’t liking paths to files that contain spaces (again right? -.-) even though I have double quoted the variable I am greping.

Does anyone have any ideas about how to fix it?

#!/bin/bash

path = $1 #1st arg is the path to be searched
regex = $2 #2nd arg is a regular expression
searchTerm = $3 #3rd arg is an optional search term
startDir = `pwd` #Stores the starting path
getDirs()
{ #Function to get the directories
    for i in "$1"
    ; do
        if [ -d "$i" ]; then
            echo "$i" >> temp.txt
            getDirs "$i"
        fi
    done
}

getFiles() {        # Function to get files matching the regex

    while IFS= read -r path; do # While there is a line, read it, backslash is not a delimiter
        cd "$path"

        temp=`ls -1 | grep "$regex"`    #List the contents of the dir. Store only files that match the regex

        for j in $temp
        do
            echo "$path/$j" # For every file stored, print its location
        done
        cd $startDir
    done < temp.txt # Read from temp.txt
}

searchFiles() { # Function to search within files


    for a in $output1 # For every file found
    do
        out=`grep -n "$searchTerm" "$a" | cut -d: -f 1` # Find the line numbers in which it is present, stop showing after 1st :
        for i in $out   # For every line found
        do
            echo "$a: line $i"  # Print the file location, and the line numbers of the terms
        done
    done
}

numArgs=$#

echo "$path" >> temp.txt
getDirs $path   # Getting directories to search

output1=`getFiles`

cd $startDir

if [ $numArgs == 3 ] # If a search term is specified
then
    searchFiles # Then search the files for it
else
    echo "$output1" # Otherwise, just print the location of the files
fi

rm temp.txt # Removing temporary files
exit 0
  • 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-16T23:57:40+00:00Added an answer on June 16, 2026 at 11:57 pm

    Your script has a LOT of problems including unquoted and incorrectly quoted variables. Here’s how your getFiles function need to be written at a minimum (there are other issues like whether grep is really necessary and use of echo but I’m not touching those so this highlights the serious problems):

    getFiles() {        # Function to get files matching the regex
    
        while IFS= read -r path; do # While there is a line, read it, backslash is not a delimiter
    
            if cd "$path"; then
    
                oIFS="$IFS" # save then reset and restore IFS to avoid work splitting on spaces, except newlines.
                IFS=$'\n' tempA=( $(ls -1 | grep "$regex") )    #List the contents of the dir. Store only files that match the regex
                IFS="$oIFS"
    
                for j in "${tempA[@]}"
                do
                    echo "$path/$j" # For every file stored, print its location
                done
                cd "$startDir"
    
            fi
    
        done < temp.txt # Read from temp.txt
    }
    

    Note that “temp” is now an array, not a string, so you can access the file names it contains one at a time and still have each of them quoted. I just renamed it tempA to make it obvious that it’s an Array.

    So, update your script to use arrays instead of strings to hold your file names as demonstrated above, get rid of the spaces around the assignments, quote all of your variables, use $(…) instead of backticks, and change grep -n "$searchTerm" "$a" | cut -d: -f 1 to awk -v st="$searchTerm" '$0~st{print NR}' "$a" then repost if you still have problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am confused How to use looping for Json response Array in another Array.
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

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.