代码之家  ›  专栏  ›  技术社区  ›  Leo Thomas

循环case语句而不退出shell脚本

  •  0
  • Leo Thomas  · 技术社区  · 7 年前

    我有一个简单的shell脚本来选择diff任务并执行它。 如果选择选项1,它将执行该任务并退出脚本。

    但我希望脚本等待用户输入另一个任务并执行它,直到用户手动退出。

    假设用户首先选择选项1并获得输出。接下来,用户希望选择选项2,并应获得输出。直到他手动退出脚本。

    #!/bin/sh
    echo "For Fruits [1] "
    echo "For Vegetables [2] "
    echo "For drinks [3] "
    read output
    case $output in 
    1)
    cat 1.txt | tr , '\n' | sort >epackP.txt 
    cat 2.txt | tr " " '\n'| /usr/xpg4/bin/awk -F "T000" '{print $2}'|/usr/xpg4/bin/awk -F "+" '{print $1}'|sed '/^$/d' | sort >symmP.txt
    diff -ibw 1.txt 2.txt
    if [ $? -eq 0 ]; then
       echo "********** Files matched :D **********"  
    else
       echo "********** Mismatch found :( **********"
     fi
    ;;
    2)
    cat 3.txt | tr , '\n' | sort >epackP.txt 
    cat 4.txt | tr " " '\n'| /usr/xpg4/bin/awk -F "T00" '{print $2}'|/usr/xpg4/bin/awk -F "+" '{print $1}'|sed '/^$/d' | sort >symmP.txt
    diff -ibw 3.txt 4.txt
    if [ $? -eq 0 ]; then
       echo "********** Files matched :D **********"  
    else
       echo "********** Mismatch found :( **********"
    fi
    ;;
    3)
    diff -ibw 5.txt 6.txt 
    if [ $? -eq 0 ]; then
       echo "********** Files matched :D **********"  
    else
       echo "********** Mismatch found :( **********"
     fi
       ;;
    *)
    echo "retry"
    esac
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   biniow    7 年前
    while true; do  
     read output
     case $output in 
     1) ...
     2)
     3) break
     esac
    done
    

    详细类似示例: Bash-Begginers-Guide