linux - Bash脚本循环分析
问题描述
我必须对这个 bash 脚本做什么才能让它重复“输入学生姓名以获得他们的分数:”并在用户再次输入姓名时输入空格时停止?目前它只接受名称并返回一次分数。我正在尝试使用while循环来重复“while continue=0”的过程。我知道这很简单,我只是卡住了。
这是我的代码
continue=0
while [[ continue -eq 0 ]]; do
echo " "
echo "Enter a student's name to get their score: "
read sName
echo "Searching for $sName's score"
length=${#names[@]}
start=0
end=$((length -1))
while [[ $start -le $end ]]; do
mid=$((start + ((end - start)/2)))
midName=${names[mid]}
if [[ $midName > $sName ]]; then
end=$((end-mid-1))
elif [[ $midName < $sName ]]; then
start=$((mid+1))
else
echo "${scores[$mid]}"
exit 0
fi
done
done
解决方案
这是一个简化示例,说明如何实现您想要的:
#!/bin/bash
while :
do
echo "Enter a student's name to get their score: "
read name
if [[ -z $name ]]
then
break
fi
# got name, continue processing
done
推荐阅读
- android - 如何绘制圆角位图?
- javascript - Vue js - 多个组件,每个组件都有不同的初始选定值
- c# - 来自弹出表单的 ajax 数据发布未命中控制器方法 ASP.NET MVC
- android - React 原生版本不匹配:原生版本:0.61.5,javascript 版本:0.57.7
- ios - SwiftUI NavigationLink 文本单击不起作用
- javascript - AES-256(CBC):Java 中的加密/JavaScript 中的解密(CryptoJS)
- python - PyTorch 中的 autograd 微分示例 - 应该是 9/8?
- firebase - 颤动中的Firebase分页
- ruby-on-rails - Rails Administrate gem:如何按“发布时间”对记录进行排序?
- angular - 发送到子组件的输入值错误