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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scripting problem 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
My script displays as I hoped it would, but when i get the sub menu's up (select options 2 or 3, on first menu displayed) but doesn't function, here is my script: I have tried a few different things but dont seem to b getting anywhere. The sub menus are not functioning, can ne1 help????

amenu () {
clear
echo "\t\t\tMy Personal Menu"
echo
echo "\t\tPlease Select:"
echo
echo "\t\t\t 1. Directory display"
echo "\t\t\t 2. File functions"
echo "\t\t\t 3. Directory functions"
echo
echo "\t\t\t 0. Exit"
echo Select by pressing a number and then ENTER ;
}

PressEnter () {
echo Press Enter
read x
}

DirectoryDisplay () {
ls -l|more
PressEnter
}

Filefunctions () {
clear
echo "\t\t\tFile functions Menu"
echo
echo "\t\tPlease Select:"
echo
echo "\t\t\t 5. Delete file"
echo
echo "\t\t\t 0. Return to main menu"
echo Select by pressing a number and then ENTER ;
PressEnter
}

Deletefile () {
print -n "Enter file name to be deleted: "
read name
if [ -f $name ]
then
rm $name
echo "file: $name has been deleted"
else
echo "this is not a file"
fi
PressEnter
}

Directoryfunctions () {
clear
echo "\t\t\tDirectory functions Menu"
echo
echo "\t\tPlease Select:"
echo
echo "\t\t\t 6. Delete directory"
echo "\t\t\t 7. Change directory"
echo
echo "\t\t\t 0. Return to main menu"
echo Select by pressing a number and then press ENTER ;
PressEnter
}

Deletedirectory () {
print -n "Enter directory name to be deleted: "
read name
if [ -d $name ]
then
echo "directory: $name has been deleted"
else
echo "this is not a directory"
PressEnter
}

Changedirectory () {
print -n "Enter directory name to be deleted: "
read name
if [ -d $name ]
then
cd $name
else
echo "$name is not a directory"
}

while true
do

amenu

read answer

case $answer in
1) DirectoryDisplay ;;
2) Filefunctions ;;
3) Directoryfunctions ;;
5) Deletefile ;;
6) Deletedirectory ;;
7) Changedirectory ;;

0) break ;;
esac

done
clear
 
I think the problem is with your menu structure. You have 3 levels of menus, but you can't call the 3rd level from the 2nd level. Note that you can call the 3rd level from the 1st level of menu, but options 5, 6, and 7 aren't shown from the 1st level.

So you have 2 options. Eliminate the 2nd level of menus (Filefunctions and Directory functions. To do this, just update the amenu function to add options 5, 6, and 7. The other option is to fix the 2nd level of menus to call the correct options.

Add this code to Filefunctions
Code:
read answer 
case $answer in 
    0) amenu ;; 
    5) Deletefile ;; 
esac
Add this code to Directoryfunctions
Code:
read answer 
case $answer in 
    0) amenu ;; 
    6) Deletedirectory ;; 
    7) Changedirectory ;;
esac
 
Hi Mak,
See this modified script .. hope this is what you were looking for. I just added one more function and modified an existing one. It's working fine.

#!/bin/sh

MainMenu () {
clear
echo -e "\t\t\tMy Personal Menu"
echo -e
echo -e "\t\tPlease Select:"
echo -e
echo -e "\t\t\t 1. Directory display"
echo -e "\t\t\t 2. File functions"
echo -e "\t\t\t 3. Directory functions"
echo -e
echo -e "\t\t\t 0. Exit"
echo -e Select by pressing a number and then ENTER ;
}

PressEnter () {
echo -e Press Enter
read
}

DirectoryDisplay () {
ls -l|more
PressEnter
}

Deletefile () {
echo -n "Enter file name to be deleted: "
read name
if [ -f $name ]
then
rm $name
echo -e "file: $name has been deleted"
else
echo -e "this is not a file"
fi
PressEnter
}

Deletedirectory () {
echo -n "Enter directory name to be deleted: "
read name
if [ -d $name ]
then
echo -e "directory: $name has been deleted"
else
echo -e "this is not a directory"
fi
PressEnter
}

Changedirectory () {
echo -n "Enter directory name to be deleted: "
read name
if [ -d $name ]
then
cd $name
else
echo -e "$name is not a directory"
fi
}

SelectedChoice () {
choice=$1;
case $choice in
5) Deletefile;;
6) Deletedirectory;;
7) changedirectory;;
0) return;;
esac

}
Directoryfunctions () {
clear
echo -e "\t\t\tDirectory functions Menu"
echo -e
echo -e "\t\tPlease Select:"
echo -e
echo -e "\t\t\t 6. Delete directory"
echo -e "\t\t\t 7. Change directory"
echo -e
echo -e "\t\t\t 0. Return to main menu"
echo -e Select by pressing a number and then press ENTER ;
read choice
SelectedChoice $choice
}

Filefunctions () {
clear
echo -e "\t\t\tFile functions Menu"
echo -e
echo -e "\t\tPlease Select:"
echo -e
echo -e "\t\t\t 5. Delete file"
echo -e
echo -e "\t\t\t 0. Return to main menu"
echo -e Select by pressing a number and then ENTER ;
read choice
SelectedChoice $choice
}

while true
do
MainMenu
read answer

case $answer in
1) DirectoryDisplay ;;
2) Filefunctions ;;
3) Directoryfunctions ;;
0) break ;;
esac

done
clear

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top