LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-23-2011, 12:01 AM   #1
SilversleevesX
Member
 
Registered: May 2009
Posts: 181
Blog Entries: 9

Rep: Reputation: 15
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

Last edited by SilversleevesX; 09-23-2011 at 12:04 AM. Reason: Misspelling in 2nd code block
 
Old 09-23-2011, 04:11 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,412

Rep: Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874
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
 
1 members found this post helpful.
Old 09-23-2011, 04:17 PM   #3
SilversleevesX
Member
 
Registered: May 2009
Posts: 181
Blog Entries: 9

Original Poster
Rep: Reputation: 15
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

Last edited by SilversleevesX; 09-23-2011 at 04:26 PM.
 
Old 09-23-2011, 04:32 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
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
 
Old 09-23-2011, 04:41 PM   #5
SilversleevesX
Member
 
Registered: May 2009
Posts: 181
Blog Entries: 9

Original Poster
Rep: Reputation: 15
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

Last edited by SilversleevesX; 09-23-2011 at 04:52 PM.
 
  


Reply

Tags
bash, find, function, return, search


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash function return rejeep Programming 15 08-24-2007 04:21 AM
getting function return values in a Bourne script? ocicat Programming 5 08-01-2007 07:01 AM
sh script: problem with return value in function jhwilliams Linux - Software 1 06-20-2007 11:47 AM
return value of function in script ramesh_manu Red Hat 1 02-18-2007 01:05 PM
search function (bash script) LYK Programming 2 05-27-2004 10:51 AM


All times are GMT -5. The time now is 11:23 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration