Bash脚本:无限循环执行不起作用

时间:2018-04-14 06:42:41

标签: shell unix

我的代码只运行一次然后停止。我希望它是无限的。请帮忙。

#!/bin/bash
while true
    do
    read -p "make a choice (order/quit) " order
    case $order in
        order )    break;;
        quit )     exit;;
        * ) echo "is not a valid."; exec bash "$0";
    esac
done
       read -p " your name :  " name
       read -p "your telephone?  " phone

printf "======================================================\n"

echo Hello $name:

printf "======================================================\n"

echo Your telephone is $phone

1 个答案:

答案 0 :(得分:0)

你没有循环名称,电话提示和回应,所以如果你想要无限次的迭代,你需要在整个脚本周围添加一个循环(无限循环)。您应该将错误报告给标准错误(以下代码中的>&2)。并且exec bash "$@"是一种不寻常且不必要的做事方式,虽然它可能有效(但它会使内部while循环变得多余)。

#!/bin/bash

while true
do
    while true
    do
        read -p "Make a choice (order/quit) " order
        case $order in
        (order) break;;
        (quit)  exit;;
        (*)     echo "$order is not a valid." >&2;;
        esac
    done

    read -p "Your name?  " name
    read -p "Your phone? " phone
    printf "======================================================\n"
    echo Hello $name:
    printf "======================================================\n"
    echo Your telephone is $phone
done