bash 脚本 tput 出现错误

bash 脚本 tput 出现错误

我需要找出这个 tput 菜单出了什么问题。

#!/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
 }

我已经在 If/Then 语句中做到了这一点,并且有效,但我正在尝试学习使用 tput。

if/then (正在工作) #!/bin/bashclear echo "主菜单" echo "1. 监视现有进程" echo "2. 将密码复制到/home目录" echo "3. Ping本地主机" echo "4.出口 ”

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

答案1

#!/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
 }

我提供了一种类似且实用的方法来执行此操作(还有其他一些颜色):

#!/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

三个主要问题:

  • 您的函数语法略有偏差。有三种方式声明函数,我认为我所展示的方式是最干净、最简单的方式。
  • 非常重要的;;在 case 语句的行尾
  • 并且循环内没有默认颜色。这不会破坏脚本,但它使您尝试使用错误文本完成的任务变得更容易。

答案2

首先,您需要纠正基本代码中的一些小错误。

  • 每个 case 语句都需要终止分号 ;;
  • 选项 3 末尾有一个不必要的括号,必须将其删除
  • 如所示,整个脚本是在函数 main_menu 中定义的,但从未调用该函数。添加仅包含文本的单独行主菜单它将执行函数中的代码。

如果 tput color 语句不起作用,请尝试使用 setab 和 set setaf。不同的是setb和setf只支持8种颜色,而setab、setaf支持256种颜色。取决于您的终端,这些更改在伴侣终端中对我来说效果很好。

相关内容