Register
It is currently Mon Oct 20, 2014 9:47 pm

how do I avoid functions in functions


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Mon Jan 19, 2009 6:17 pm   

Joined: Mon Jan 19, 2009 5:58 pm
Posts: 1
I know C64 basic really well (am I getting old?), but am a newb in bash.

Function seems like a gosub in basic. I need something more like a goto.

Anyway here is part of my script, you will see how I am calling a function from within a function (even the current function) how can I avoid this with something that will work.

Here it is.

Code:
function searchproduct {
clear
echo Search product
echo ""
read -p "Enter product id / description / type / manufacturer: " PRS
clear
echo "ID   |Description      |Category   |Manufacturer   |Price"
grep $PRS $DATFILE
echo ""
read -p "1 - Main menu | 2 - New search | 3 - Print results" x
menu () case $x in
1)
menu
;;
2)
searchproduct
;;
3)
printproduct
;;
esac
}


Many Thanks


Top
 Profile  
 PostPosted: Mon Mar 16, 2009 2:43 am   

Joined: Wed Mar 04, 2009 8:38 am
Posts: 2
Well, in my opinion you ought to structure your code a bit differently. The menu really hasn't got much to do with the searchproduct-function, and should be run as the main loop.. Like this:
Code:
#!/bin/bash

DATAFILE="foo.bar"

function searchproduct {
  clear
  echo Search product
  echo ""
  read -p "Enter product id / description / type / manufacturer: " PRS
  clear
  echo "ID   |Description      |Category   |Manufacturer   |Price"
  if [ -f $DATAFILE ]
  then
    grep "$PRS" $DATAFILE
  else
    echo "Datafile \"$DATAFILE\" not found."
    echo; echo "Exiting.."
    exit 1
  fi
}

function menu {
  echo ""
  echo "1 - Main menu | 2 - New search | 3 - Print results | Q - Quit"
  read -p "Command> " x
  case $x in
    1)
      return
      ;;
    2)
      searchproduct
      ;;
    3)
      printproduct
      ;;
  esac
}

while [ "$x" != "q" ] && [ "$x" != "Q" ]
do
  menu
done

echo


Top
 Profile  
 PostPosted: Thu Mar 19, 2009 1:01 am   

Joined: Tue Dec 11, 2007 9:32 pm
Posts: 2
Are you giving us a test? or are we taking a test for you?


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP