Jump to content

Programming a menu system


Recommended Posts

I'm wanting to try to program a menu system, so that I can have it do various things, such as change DNS servers, network interface IP addresses, program command line parameters, things like this. However, I'm not sure where to start. I'm thinking of python, so that I don't have to install php-cli or similar.

 

I could of course do bash scripting to do this also, but I'm trying to find a better way of doing it. Please note, this machine does not have X/Gnome/KDE or anything and so there are no GUI tools available for it - nor will it be using MCC since the distro could be anything.

 

Any ideas appreciated.

Link to comment
Share on other sites

OK, well I'm doing it in perl so far, and it's working. But now I want the option to get it to logout of my SSH session once finished, but I can't seem to get it to work. The particular section is:

 

if ($input eq "3")
{
	exec(exit);
}

 

but all that does is exits the perl program. I wanted it to end the SSH session as well, but I don't know how it will do it. When I type "exit" in a SSH session, it disconnects. I've even tried using logout, but same response.

 

Any ideas?

Link to comment
Share on other sites

OK, well I've managed to do this in perl now.

 

if ($input eq "3")
{
	exec("kill -HUP `pgrep -s 0 -o`");
}

 

that kills that particular session, closing the SSH connection so doest what I need it to do.

Link to comment
Share on other sites

Try Perl/TK, I have recently used it to write some small GUI app and I learned it in no time it's really easy.

Here are some links to tutorials I used:

 

http://www.bin-co.com/perl/perl_tk_tutorial/

 

http://www.ibm.com/developerworks/aix/library/au-perltkmodule/index.html

 

http://oreilly.com/catalog/mastperltk/chapter/ch15.html

Link to comment
Share on other sites

Thanks for the links tux99. I'm doing a console menu, rather than GUI stuff and I got it all written yesterday in perl :)

 

This is what I got so far:

 

#!/usr/bin/perl
##
# Author: Me
# Email: My email
# Web: My website
##

use Term::ANSIColor;
$input = "";

while(1)
{
system("clear");
print color("red"), "\nOption\t\tDescription\n";
print color("green"), "======\t\t========================\n";
print color("white"), "1\t\tOption 1\n";
print "2\t\tOption 2\n";
print "3\t\tReboot Appliance\n";
print "4\t\tShutdown Appliance\n";
print "5\t\tQuit\n\n";
print "Enter your choice: ";
chomp ($input = <STDIN>);

if ($input eq "5")
{
	exec("kill -HUP `pgrep -s 0 -o`");
}

elsif ($input eq "4")
{
	exec(poweroff);
}

elsif ($input eq "3")
{
	exec(reboot);
}

elsif ($input eq "1")
{
	print "\nDoing Option 1 stuff now:";
	chomp ($input = <STDIN>);
	open (TESTFILE, '>test.conf');
	print TESTFILE $input;
	print TESTFILE "\n";
	close (TESTFILE);
}

elsif ($input eq "2")
{
	print "\nDoing Option 2 stuff now: ";
	chomp ($input = <STDIN>);
	open (TESTFILE, '>test2.conf');
	print TESTFILE $input;
	close (TESTFILE);
}
}

Link to comment
Share on other sites

Oops, I guess I didn't read your first post accurately enough... :oops:

 

Anyway should you ever do anything with a GUI, then I highly recommend Perl/TK, it's very easy to learn, especially for someone who knows Perl already.

Edited by tux99
Link to comment
Share on other sites

The difference is:

 

— switch/case allow several comparison based on the same value.

— if/else allow several independant comparisons.

 

As a consequence, if you write (that's no particular language…):

 

if a+b == 5 then echo "five"

else-if a+b == 20 then echo "twenty"

else-if a+b == callFunction() then echo "other permited"

 

then the sentence “a+b†is computed for comparison three times, once for each comparison. Whereas if you write:

 

switch a+b:

case 5: echo "five", break

case 20: echo "twenty", break

case callFunction(): echo "other permited"

 

then the sentence “a+b†is computed only once, which can be a big improvement if instead of “a+b†you have a complex function call.

On the other hand, your language may not allow this last line of code I wrote in its syntax.

Besides, you can write something like:

 

if a == 5 then echo "first param is five"

else-if b == 10 then echo "second param is ten"

else echo "illegal combination of parameters"

 

which cannot be expressed with switch/case because the sentence being compared is different each time.

 

Yves.

Edited by theYinYeti
Link to comment
Share on other sites

Thank you for the clarification YinYeti. It's quite logical when you see it. Shouldn't the final statement in the switch loop be 'case: default', as a catch all other inputs?

I understand the style of syntax depends on the language being used but generalized there's not a lot of difference.

Link to comment
Share on other sites

  • 2 weeks later...

You're right :) although the “default†in case statements is no more mandatory than “else†is for if/then statements. It's good practice, but you may have a good reason for not using it.

 

Yves.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...