嵌套 while 循环(Linux 中的 bash 脚本)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26541891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
nested while loops (bash scripting in linux)
提问by DAT BOI
I'm having trouble with my nested while loops. My problem is that my inner while loop only executes once but my outer loop is able to continue to repeat. Here's my code:
我的嵌套 while 循环有问题。我的问题是我的内部 while 循环只执行一次,但我的外部循环能够继续重复。这是我的代码:
while [[ "$input" != "exit" && "$input" != "Exit" && "$input" != "EXIT" ]]
do
echo 'Enter "name" to find files by name'
read input
if [[ "$input" = "name" || "$input" = "Name" || "$input" = "NAME" ]]; then
while [[ "$a" != "f" && "$a" != "s" ]]
do
read -p "Want to search inside only the current folder (f) or include subfolders (s)" a
if [ "$a" = "f" ]; then
echo 'f'
elif [ "$a" = "s" ]; then
echo 's'
elif [[ "$a" != "f" && "$a" != "s" ]]; then
echo 'you have entered an invalid option, please try again'
fi
done
fi
done
Here's a sample output:
这是一个示例输出:
Enter "name" to find files by name
输入“name”以按名称查找文件
Enter "size" to find files with a specified size
输入“size”查找指定大小的文件
Enter "date" to find files by date
输入“日期”以按日期查找文件
Enter "string" to find files containing a particular string
输入“string”以查找包含特定字符串的文件
Enter "exit" to exit the script.
输入“exit”退出脚本。
name
姓名
Want to search inside only the current folder (f) or include subfolders (s)t
只想在当前文件夹内搜索 (f) 或包括子文件夹 (s)t
you have entered an invalid option, please try again
您输入的选项无效,请重试
Want to search inside only the current folder (f) or include subfolders (s)s
只想在当前文件夹 (f) 内搜索或包括子文件夹 (s)
s
秒
Enter "name" to find files by name
输入“name”以按名称查找文件
Enter "size" to find files with a specified size
输入“size”查找指定大小的文件
Enter "date" to find files by date
输入“日期”以按日期查找文件
Enter "string" to find files containing a particular string
输入“string”以查找包含特定字符串的文件
Enter "exit" to exit the script.
输入“exit”退出脚本。
name # right here is where my inner loop failed to run but I don't see why
name # 这里是我的内部循环无法运行的地方,但我不明白为什么
Enter "name" to find files by name
输入“name”以按名称查找文件
Enter "size" to find files with a specified size
输入“size”查找指定大小的文件
Enter "date" to find files by date
输入“日期”以按日期查找文件
Enter "string" to find files containing a particular string
输入“string”以查找包含特定字符串的文件
Enter "exit" to exit the script.
输入“exit”退出脚本。
回答by tripleee
Your code does not seem to match the printout you present. It's also not clear what your problem is. But the code has the distinct problem that it compares the value of input
before reading it. You need to read the input, then compare it to something. Fortunately, this is not hard to do, because you can have multiple commands in the while
condition. The other problem seems to be that you never break out of the inner loop.
您的代码似乎与您提供的打印输出不匹配。也不清楚你的问题是什么。但是代码有一个明显的问题,它input
在阅读之前比较了 的值。您需要读取输入,然后将其与某些内容进行比较。幸运的是,这并不难做到,因为while
条件中可以有多个命令。另一个问题似乎是你永远不会跳出内部循环。
The following is also refactored to use the more-elegant case
statement instead of the repetitive comparisons.
下面也重构为使用更优雅的case
语句而不是重复的比较。
while read -p 'Enter "name" to find files by name' input
[[ ! $input =~ [Ee]xit|EXIT ]]
do
case $input in
[Nn]ame | NAME )
while read -p "Want to search inside only the current folder (f) or include subfolders (s)" a
case $a in
f)
echo 'f'
break ;; # success: break out of while loop
s)
echo 's'
break ;;
*)
echo 'you have entered an invalid option, please try again' ;;
esac
done ;;
esac
done