Shell脚本中的多任务菜单

时间:2017-08-06 15:04:57

标签: bash shell scripting

Shell脚本中的多任务菜单

我有一个Shell脚本(test.sh)来完成菜单中的4个任务。任务是清理终端历史记录,下载文件夹,垃圾箱或退出。我到目前为止的代码是:

#!/bin/bash

function menu {
clear
echo "[1] Clean Terminal"
echo ""
echo "[2] Clean Downloads"
echo ""
echo "[3] Clean Trash"
echo ""
echo "[x] Exit"
echo ""

read -p "Option > " option

}

function terminal {
    clear
    cd
    cat /dev/null > .bash_history
    echo "Terminal history is now cleared!"
}

function downloads {
     clear
     cd
     rm -r Downloads/*
     echo "Downloads directory is now cleared!"
}

function trash {
     clear
     cd
     rm -rf ~/.local/share/Trash/*
     echo "Trash is now cleared!"
}

function terminate {
     exit
}

end_script=0
while [ "${end_script}" != 1 ];do
    menu
    case ${option} in
    1)
        terminal
    ;;

    2)
        downloads
    ;;

    3)
        trash
    ;;


    x)

        terminate
    ;;

    *)
        clear
        echo "unknown usage!"
    ;;
    esac
done

执行脚本的样子:

[1] Clean Terminal

[2] Clean Downloads

[3] Clean Trash

[x] Exit

Option > 

问题是什么:

我遇到的问题是,此菜单仅接受来自用户的单个输入(一次只能输入1个数字)。我希望能够做的是输入多个输入,然后将它们分成这样(1,2)或(1 2 3)。

我到目前为止所做的......

我已经就这个问题集思广益了。我想到这样做的一种方法是为所有数字变体创建单独的函数并添加代码,但对于较大的菜单来说这将非常耗时,并且它看起来不是很合理。

2 个答案:

答案 0 :(得分:1)

试试这个:

#!/bin/bash

while true; do
  read -p "Option > " options
  for option in ${options//,/ }; do   # replace all , by white spaces
   case ${option} in
    1) echo "function terminal";;
    2) echo "function downloads";;
    3) echo "function trash";;
    x) echo "terminate"; exit;;
    *) echo "unknown usage!";;
    esac
  done
done

阅读提示时的示例用法:5,3, 2 1 x

输出:

unknown usage!
function trash
function downloads
function terminal
terminate

答案 1 :(得分:0)

您可以解析用户输入,遍历每个输入的选项和 在每次迭代中检查case。看看这个简单的例子:

#!/bin/bash

read -p "Option > " option
for i in $option; do echo an option: $i; done

用法:

$ ./ex.sh
Option > 1 2 3
an option: 1
an option: 2
an option: 3

所以你的整个脚本就像这样(我添加了一些调试消息和 sleep行):

#!/bin/bash

function menu {
clear
echo "[1] Clean Terminal"
echo ""
echo "[2] Clean Downloads"
echo ""
echo "[3] Clean Trash"
echo ""
echo "[x] Exit"
echo ""

read -p "Option > " option

}

function terminal {
    clear
    cd
    cat /dev/null > .bash_history
    echo "Terminal history is now cleared!"
    sleep 5
}

function downloads {
     clear
     cd
     rm -r Downloads/*
     echo "Downloads directory is now cleared!"
     sleep 5
}

function trash {
     clear
     cd
     rm -rf ~/.local/share/Trash/*
     echo "Trash is now cleared!"
     sleep 5
}

function terminate {
     exit
}

end_script=0
while [ "${end_script}" != 1 ];do
    menu
    echo ADDED BY ME: option is $option
    sleep 5
    for i in $option
    do
    echo ADDED BY ME: current option is $i
    case ${i} in
        1)
        terminal
        ;;

        2)
        downloads
        ;;

        3)
        trash
        ;;
        x)

        terminate
        ;;

        *)
        clear
        echo "unknown usage!"
        ;;
    esac
    done
done