0

I need to figure out what is wrong with this tput menu.

#!/bin/bash

tput setb 3
tput clear

function main_menu 
{
option=0
until [ "$option" = "4" ]; do
echo "  1.) Monitor existing processes "
echo "  2.) Copy passwd to /home directory "
echo "  3.) Ping local host "
echo "  4.) Exit "

echo -n "Enter choice:"
read option
echo ""
case $option in

    1 ) ps aux;echo "The list has been successfully generated!";
    2 ) cp /etc/passwd /home;echo "The passwd file has been copied to your home directory.";
    3 ) ping -c 4 127.0.0.1;echo "You have completed pinging localhost" );
    4 ) exit;; 
    * ) tput setf 4;echo "Please enter between number 1 and 4";tput setf 4;

esac
done
 }

I've made it in the If/Then statement, and that works but I am trying to learn on using tput.

if/then ( is working) #!/bin/bash clear echo "Main Menu " echo "1. Monitor existing processes " echo "2. Copy passwd to /home directory " echo "3. Ping local host " echo "4. Exit "

read num 

if [ $num -eq 1 ]   
then ps aux
        echo "The list has been successfully generated! "

elif [ "$num" -eq 2 ]
then cp /etc/passwd /home
        echo "The passwd file has been copied to your home directory."

elif [ "$num" -eq 3 ]
then ping -c 4 127.0.0.1
        echo "You have completed pinging localhost"

elif [ "$num" -eq 4 ]
then clear

fi
5
  • 1
    It is not entirely clear what you are asking. Feb 23, 2015 at 4:02
  • Not sure if this of value to you but when writing/learning I use bash -x nameofscript.sh. This should help debug the issue.
    – user610209
    Feb 23, 2015 at 4:22
  • @cremfraiche sorry if it doesn't make sense. I am a beginner to bash scripting. I'm trying to accomplish same task as in 2nd script with tput from the first script. For some reason I am unable to get it to work.
    – Hp88
    Feb 23, 2015 at 4:32
  • @user510209 thanks for the tip. I'm stuck in one of the assignments from linux class.
    – Hp88
    Feb 23, 2015 at 4:33
  • I gave you a working solution, however the problem was not in your tput usage, but other syntax errors throughout. Feb 23, 2015 at 7:53

2 Answers 2

0
#!/bin/bash

tput setb 3         # if you set these inside the loop, you won't have to mess with color as much later on.
tput clear

function main_menu     # this can be done a better way. slightly incorrect syntax as-is. also, you define main_menu but never call the function.
{
option=0       # not neded
until [ "$option" = "4" ]; do    # quotes not needed
echo "  1.) Monitor existing processes "       # this can all be cleaned up
echo "  2.) Copy passwd to /home directory "
echo "  3.) Ping local host "
echo "  4.) Exit "

echo -n "Enter choice:"
read option    # the echo prompt and read can be shortened with read -p
echo ""        # quotes not needed
case $option in

    1 ) ps aux;echo "The list has been successfully generated!";     # all of the case entries need to end in double semi-colons
    2 ) cp /etc/passwd /home;echo "The passwd file has been copied to your home directory.";
    3 ) ping -c 4 127.0.0.1;echo "You have completed pinging localhost" );   # there is an unnecessary ")" here 
    4 ) exit;; 
    * ) tput setf 4;echo "Please enter between number 1 and 4";tput setf 4;   # I think you might be trying to set the warning red, but both of your tput commands set color to red.
esac
done
 }

I offer a similar and functional way to do this (with some other colors as well):

#!/bin/bash

tput clear

main_menu()
{
until [ $option = 4 ]; do
tput setb 2
tput setf 5
read -p "  
1.) Monitor existing processes
2.) Copy /etc/passwd to /home directory
3.) Ping local host
4.) Exit
Enter choice: " option
echo 
case $option in

    1) ps aux;echo "The list has been successfully generated!";;
    2) cp /etc/passwd /home;echo "The passwd file has been copied to your home directory.";;
    3) ping -c 4 127.0.0.1;echo "You have completed pinging localhost" ;;
    4) exit;; 
    *) tput setf 4; echo "Please enter between number 1 and 4\n\n";;

esac
done
}

main_menu

The three main issues:

  • Your function syntax is slightly off. There are three ways to declare functions, IMO the way I have shown is the cleanest and simplest way.
  • The very important ;; at the end of lines in case statements
  • And not having your default colors inside the loop. This one wouldn't break the script, but it makes what you are trying to accomplish with the error text easier.
8
  • wow thanks for your help, I've been slamming my head trying to figure out how to make it work. I made it work by using if/then but not this way. This looks much cleaner and nicer. Time to learn this way. Thanks again. This is a great community with knowledgable people.
    – Hp88
    Feb 23, 2015 at 7:55
  • @Hp88 You are very welcome, I hope this helps you learn some new techniques. If my answer works for you, please select it as the correct answer, thanks. Feb 23, 2015 at 8:05
  • @Hp88 Also, please be aware when I first posted this answer there was an accidental space before the last bracket that would give an error. I have edited the answer to remove it. Feb 23, 2015 at 8:10
  • until [ option should be until [ "$option"
    – Costas
    Feb 23, 2015 at 9:00
  • Bash is not python so """ do not make a sense: one " is enough as "" mean nothing
    – Costas
    Feb 23, 2015 at 9:06
0

first off you need to correct a couple of minor errors in the basic code.

  • each case statement needs to be terminated with two semicolons ;;
  • Option 3 has an unnecessary parenthesis towards the end which must be removed
  • The whole of the script, as presented, is defined in the function main_menu but this function is never called. Add a separate line containing just the text main_menu which will execute the code in the function.

if the tput color statements don't work try using setab and set setaf. The difference is that setb and setf support only 8 colors while setab, setaf support 256 colors. Depends on your terminal, These changes work fine for me in mate-terminal.

0

Not the answer you're looking for? Browse other questions tagged .