im realy new on python(started today) and i want to know how to write this script in python

#!/bin/bash


function press_enter
{
    echo ""
    echo -n "Press Enter to continue"
    read
    clear
}


selection=
until [ "$selection" = "0" ]; do
    echo ""
    echo "PROGRAM MENU"
    echo "1 - open xmms"
    echo "2 - open emacs"
    echo "3 - open gparted"
    echo "4 - open gedit"
    echo "5 - open firefox"
    echo "6 - open gaim"
    echo "7 - open kopete"
    echo "8 - open console"
    echo "0 - exit program"
    echo ""
    echo -n "Enter selection: "
    read selection
    echo ""
    case $selection in
        1 ) xmms ; press_enter ;;         
        2 ) emacs ; press_enter ;;
        3 ) sudo gparted ; press_enter ;;
        4 ) gedit ; press_enter ;;
        5 ) firefox ; press_enter ;;
        6 ) gaim ; press_enter ;;
        7 ) kopete ; press_enter ;;
        8 ) konsole ; press_enter ;;
        0 ) echo -n "are you sure you want to exit program? (y/n) >"
            read response
            if test $response = y ; then
                echo "exiting program"
                exit 0
            else
                echo
                if test $response = n ; then
                press_enter 
                sh my_first_menu
                fi    
            fi;;
        * ) echo please select valid number you idiot ; press_enter ;;             
     esac
done

and please no links

Hi jan1024188,
I'm also a newbie in python.But explain more on your code,so that I can help you.

Good luck.

I think bash is a script language on Linux computers. I don't use Linux (yet), so I can't test it, but the program flow is easy to follow. The first part is a menu from which you select, and the second part is a case statement that activates a slected program on Linux.

Python does not have a case statement, but you can use a series of if/elif condionals or a dictionary made to look like a case statement. To run an external program from Python, you can use os.system("program")

Here is a simplified version in Python:

import os

while True:
    print
    print "PROGRAM MENU"
    print "1 - open xmms"
    print "2 - open emacs"
    print "3 - open gparted"
    print "4 - open gedit"
    print "5 - open firefox"
    print "6 - open gaim"
    print "7 - open kopete"
    print "8 - open console"
    print "0 - exit program"
    print
    selection = input("Enter selection: ")
    if selection >= 0:
        break

if selection == 0:
    raise SystemExit
elif selection == 1: os.system("xmms")
elif selection == 2: os.system("emacs")
elif selection == 3: os.system("sudo gparted")
elif selection == 4: os.system("gedit")
elif selection == 5: os.system("firefox")
elif selection == 6: os.system("gaim")
elif selection == 7: os.system("kopete")
elif selection == 8: os.system("konsole")

jan@jan-desktop:~/bin$ python menu.py

PROGRAM MENU
1 - open xmms
2 - open emacs
3 - open gparted
4 - open gedit
5 - open firefox
6 - open gaim
7 - open kopete
8 - open console
0 - exit program

Enter selection: 2
jan@jan-desktop:~/bin$


it doesnt work like i wanted
after selecting something program exits....

Simply add the outer while loop to exit only on option zero:

import os

selection = 1
while selection != 0:
    while True:
        print
        print "PROGRAM MENU"
        print "1 - open xmms"
        print "2 - open emacs"
        print "3 - open gparted"
        print "4 - open gedit"
        print "5 - open firefox"
        print "6 - open gaim"
        print "7 - open kopete"
        print "8 - open console"
        print "0 - exit program"
        print
        selection = input("Enter selection: ")
        if 0 <= selection < 9:
            break
    
    if selection == 0:
        raise SystemExit
    elif selection == 1: os.system("xmms")
    elif selection == 2: os.system("emacs")
    elif selection == 3: os.system("sudo gparted")
    elif selection == 4: os.system("gedit")
    elif selection == 5: os.system("firefox")
    elif selection == 6: os.system("gaim")
    elif selection == 7: os.system("kopete")
    elif selection == 8: os.system("konsole")

Remember, Ene called her version simplified!

thanks anyway how to get exit like that

echo -n "are you sure you want to exit program? (y/n) >"
read response
if test $response = y ; then
echo "exiting program"
exit 0
else
echo
if test $response = n ; then
press_enter
sh my_first_menu
fi
fi;;

This will add the extra exit question ...

import os

selection = 1
while selection != 0:
    while True:
        print
        print "PROGRAM MENU"
        print "1 - open xmms"
        print "2 - open emacs"
        print "3 - open gparted"
        print "4 - open gedit"
        print "5 - open firefox"
        print "6 - open gaim"
        print "7 - open kopete"
        print "8 - open console"
        print "0 - exit program"
        print
        selection = input("Enter selection: ")
        if 0 <= selection < 9:
            break
    
    if selection == 0:
        quest = raw_input("are you sure you want to exit program? (y/n)")
        if quest == 'y':
            raise SystemExit
        else:
            selection = 1
    elif selection == 1: os.system("xmms")
    elif selection == 2: os.system("emacs")
    elif selection == 3: os.system("sudo gparted")
    elif selection == 4: os.system("gedit")
    elif selection == 5: os.system("firefox")
    elif selection == 6: os.system("gaim")
    elif selection == 7: os.system("kopete")
    elif selection == 8: os.system("konsole")

many thanks

how to add enter function in this script?

many thanks

how to add enter function in this script?

Very simple:

raw_input("Press Enter ...")  # wait till the Enter key is pressed

or ...

def press_enter():
    raw_input("Press enter to continue ...")

Python does not have a clear the screen function, but you could add:

print '\n' * 25

or:

import os
os.system('clear') # linux

instead of clear.

yes but how to add that function to this part

elif selection == 1: os.system("xmms")
elif selection == 2: os.system("emacs")
elif selection == 3: os.system("sudo gparted")
elif selection == 4: os.system("gedit")
elif selection == 5: os.system("firefox")
elif selection == 6: os.system("gaim")
elif selection == 7: os.system("kopete")
elif selection == 8: os.system("konsole")

You really don't need the function press_enter() there, since you are already executing your selected program.

oh ......i see (i was thinking about something else...sory)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.