LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   BASH: Return from function problem in search script (http://www.linuxquestions.org/questions/programming-9/bash-return-from-function-problem-in-search-script-904543/)

SilversleevesX 09-23-2011 12:01 AM

BASH: Return from function problem in search script
 
I'm not sure you could call it a real return either -- more of a courtesy return.

Here's the function I think is missing some lines:
Code:

function searchme () {
#Start the search       
echo -e "What folder do you wish to search?\n(space or 'top' -- current directory recursion)"
read tdir
if [[ ! -d "$tdir" ]] && [ "$tdir" == " " ] || [ "$tdir" == "top" ]; then tiptop; fi
if [[ ! -d "$tdir" ]]; then trapr ; fi
echo "Okay, give me the search string."
read tbool
echo -ne "\n$mydate Searched string $tbool in folder $tdir of parent folder $survey.\n" >> ~/searcher.log
cd "$tdir"
e=$(ls |grep $tbool)
ecount=$(ls -1 | grep $tbool | wc -l)
if [[ "$ecount" != "0" ]]; then
        echo "$ecount files found." >> ~/searcher.log
else
        echo "No results returned." >> ~/searcher.log
fi
echo "Checking..."
sleep 0.5
if [[ -z "$e" ]] || [[ "$ecount" == "0" ]];
then
        echo -ne "No files found with the search string $tbool in the folder $tdir.\nSorry.\n"
else
        ls *$tbool*
        echo "Done. Details logged."
fi
cd ../
}

It's the first part of the last if/then/fi conditional, just above the else, that I think needs a return command of some kind to follow it.

The operative part of the script, outside the several functions that make up most of it, reads like this:
Code:

read -e -p "Another search? {y/n} " goagain
if [[ "$goagain" != "n" ]]; then
        echo
        searchme
else
        echo "Exiting."
        exit 1
fi

There's the "courtesy" part -- asking if the user wants to run another search. As the whole script runs now, if no file matching the search string in the valid (or validated -- there's another function that checks for this) folder selected, then the script quits instead of returning to the part at the end.

What would be the best method of guaranteeing a return to "chute" the user down to that primary routine?

BZT

grail 09-23-2011 04:11 AM

I am gonna start with ... what the??

I know we have mentioned this before, but it seems you still like to use ls to get details instead of globbing??

Could you maybe bold or highlight:
Quote:

It's the first part of the last if/then/fi conditional, just above the else, that I think needs a return command of some kind to follow it
Why do you feel a return is necessary? what would you do with what is returned??

I presume the last snippet is in a while loop, so why do we need to return anything? Won't the 'Another search' question
just get asked until you say no?

I am thinking I am missing something :(

SilversleevesX 09-23-2011 04:17 PM

In using the term "return," I think I latched on to the wrong word altogether, here.

What I mean to ask is, how do I keep the script from quitting on the condition that no file matching the user's two criteria (folder and name, or part of the name) is found, but instead having it go back to the "Another search {y/n}" prompt in the main part of the script?

Almost apropos of nothing whatsoever here: I recall in old CLI BASIC, this might have been accomplished with an "if/then/else goto/end if" loop. What's the BASH equivalent?

BZT

crts 09-23-2011 04:32 PM

Hi,

from what I see so far the script will definitely exit after 'searchme' is called, regardless of the result. Do you want something like this?
Code:

while read -e -p "Another search? {y/n} " goagain;do
  if [[ "$goagain" != "n" ]]; then
        echo
        searchme
  else
        echo "Exiting."
        exit 1
  fi
done


SilversleevesX 09-23-2011 04:41 PM

Okay, ignore my last observation.

I just Google'd myself into a nodding acquaintance with the "until" loop.

Were I to replace the if/then/else/fi here
Code:

read -e -p "Another search? {y/n} " goagain
if [[ "$goagain" != "n" ]]; then
        echo
        searchme
else
        echo "Exiting."
        exit 1
fi

with something like
Code:

until [ "$goagain" == "n" ]
do
  searchme
  read -e -p "Another search? {y/n} " goagain
done

it should work, shouldn't it? (Of course, I'd also either delete or comment out that "searchme" on the line above the IF in the original script.)

BZT


All times are GMT -5. The time now is 09:54 PM.