Bashスクリプトtputでエラーが発生しました。 [閉じる]

Bashスクリプト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 .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

ベストアンサー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

3つの主な質問:

  • 関数の構文が少し外れています。持つ3つの方法関数を宣言するには、私が示した方法が最もきれいで簡単な方法のようです。
  • 非常に重要;;;Case 文の行末で
  • そしてループの内側には基本色はありません。これにより、スクリプトが破損することはありませんが、間違ったテキストでやりたいことが簡単になります。

おすすめ記事