Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shell Scripts and Functions

Status
Not open for further replies.

TSch

Technical User
Jul 12, 2001
557
0
0
DE
Hi folks,

I'm currently trying to figure out the possibilities of using functions within shell scripts.

Now I'm facing my first Problem :)

I'm trying to create a menu structure from where I can jump between different menues and always go back to my main menu ...

e.G.


Code:
#!/usr/bin/ksh

function main
{
echo "Please choose:"
echo "1) Do something"
echo "2) Do something else"
echo "3) Do something completely different"
echo ""
read $1

case $1 in
  1) function_1
  2) function_2
  3) function_3
esac
}

function function_1
{
echo "Something ..."
... perform certain actions
echo "Press any key to return to main menu"
read $1
main
}

function function_2
{
echo "Something else ..."
... perform certain actions
echo "Press any key to return to main menu"
read $1
main
}

function function_3
{
echo "Something completely different..."
... perform certain actions
echo "Press any key to return to main menu"
read $1
main
}

main

-> Now the problem is, that I keep calling new functions without quitting the old function I'd like to leave and therefore continue to consume more memory on my machine each time I call another function. e.G.: jumping from main to function_1 and from there to main. After that I got 2xmain function active as well as 1xfunction_1 active. Then I jump to function_3 and back to main and voila: 3xmain active, 1xfunction_1 active AND 1xfunction_3 active ...

What would be the best way to end an old function as soon as I jump to another function ?

Regards
Thomas
 
Hi

Use a loop and call main only in that :
Code:
[gray]#!/usr/bin/ksh[/gray]

[COLOR=darkgoldenrod]function main[/color]
{
  echo [green][i]"Please choose:"[/i][/green]
  echo [green][i]"1) Do something"[/i][/green]
  echo [green][i]"2) Do something else"[/i][/green]
  echo [green][i]"3) Do something completely different"[/i][/green]
  echo [green][i]"anything else) Do nothing ( Yeah, I mean Exit )"[/i][/green]
  [COLOR=chocolate]read[/color] choice

  [b]case[/b] [green][i]"$choice"[/i][/green] [b]in[/b]
    [purple]1[/purple][teal])[/teal] function_1 [teal];;[/teal]
    [purple]2[/purple][teal])[/teal] function_2 [teal];;[/teal]
    [purple]3[/purple][teal])[/teal] function_3 [teal];;[/teal]
    [teal]*)[/teal] [navy]choice[/navy][teal]=[/teal][purple]0[/purple] [teal];;[/teal]
  [b]esac[/b]

  [teal](([/teal] choice [teal]))[/teal] [teal]&&[/teal] {
    echo [green][i]"Press any key to return to main menu"[/i][/green]
    [COLOR=chocolate]read[/color]
  }

  [COLOR=chocolate]return[/color] [green][i]"$choice"[/i][/green]
}

[COLOR=darkgoldenrod]function function_1[/color]
{
  echo [green][i]"Something ..."[/i][/green]
  [gray]# ... perform certain actions[/gray]
}

[COLOR=darkgoldenrod]function function_2[/color]
{
  echo [green][i]"Something else ..."[/i][/green]
  [gray]# ... perform certain actions[/gray]
}

[COLOR=darkgoldenrod]function function_3[/color]
{
  echo [green][i]"Something completely different..."[/i][/green]
  [gray]# ... perform certain actions[/gray]
}

[b]until[/b] main[teal];[/teal] [b]do[/b] [teal]:;[/teal] [b]done[/b]
Tested with [tt]mksh[/tt].

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top