微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Bash脚本循环分析

如何解决Bash脚本循环分析

该bash脚本要重复“输入学生姓名以获取分数:”并在要求再次输入姓名时输入空白时停止,该怎么办?目前,它接受名称,并且仅返回一次分数。我正在尝试使用while循环重复“ whilecontinue = 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。