简体   繁体   English

在shell脚本中打破循环

[英]Breaking loop in shell script

The user inputs sentences for which the number of words has to be counted. 用户输入必须计算单词数量的句子。 If the number is greater than three the sentence should be considered Long . 如果数字大于3,则应将句子视为Long If three or fewer then Short . 如果三个或更少,那么

I know how to do that, but the rest of the task says make a loop so the user can input sentences until he inputs "n". 我知道该怎么做,但剩下的任务说是做一个循环,所以用户可以输入句子,直到他输入“n”。 The problem is I when I input "n" it counts it, but that "n" shouldn't be counted. 问题是当我输入“n”时它会计算它,但不应计算“n”。 (It should be a break statement.) How can I break that loop before counting the letter "n"? (它应该是一个break语句。)如何在计算字母“n”之前打破该循环?

#!/bin/bash
recenica=0
while [ $recenica != "n" ]
do
    echo Unesite recenicu:
    read recenica
    if [ $recenica = "n" ]; then # ->>>>> this doesn't work
        break
    fi  
    echo $recenica > datoteka
    br=$(wc -l datoteka | cut -c1-2)
    echo recenica ima $br rijeci
    if [ $br -gt 3 ] 
    then
        echo $recenica > Duge.Recenice.IB
    elif [ $br -le 3 ] 
    then
        echo $recenica > Kratke.Recenice.IB
    else
        echo Molimo unijeti nešto
    fi  
done

You need to use quotes around symbols that expand to more than one word (contain spaces)... this works: 你需要使用扩展到多个单词(包含空格)的符号周围的引号...这有效:

#!/bin/bash
recenica=0
while [ "$recenica" != "n" ] # koristiti dvostruke navodnike ovdje
do
    echo Unesite recenicu:
    read recenica
    if [ "$recenica" = "n" ]; then # koristiti dvostruke navodnike ovdje
        break
    fi  
    echo $recenica > datoteka
    br=$(wc -l datoteka | cut -c1-2)
    echo recenica ima $br rijeci
    if [ $br -gt 3 ] 
    then
        echo $recenica > Duge.Recenice.IB
    elif [ $br -le 3 ] 
    then
        echo $recenica > Kratke.Recenice.IB
    else
        echo Molimo unijeti nešto
    fi  
done

The problem with read is that it will read the entire line and place the contents in recenica . read的问题是它将读取整行并将内容放在recenica With one word this is fine for the test: 用一个词来说这对测试很好:

while [ $recenica != "n" ]

However, if you have read more than one word, then the test will fail due to [: too many arguments (as noted by amdn in his answer regarding quoting) 但是,如果你已经阅读了多个单词,那么由于[: too many arguments (由amdn在他关于引用的答案中指出),测试将失败

When you enter n you care only about the first word, so limit your test accordingly using parameter expansion/substring extraction : 当你输入n你只关心第一个单词,所以使用parameter expansion/substring extraction相应地限制你的测试:

while [ ${recenica%% *} != "n" ]; do

That allows you to let the while loop control the break logic reducing the entire structure of your code to: 这允许您让while循环控制break逻辑,从而将代码的整个结构减少到:

#!/bin/bash
recenica=0
while [ ${recenica%% *} != "n" ]
do
    echo Unesite recenicu:
    read recenica

    echo $recenica > datoteka
    br=$(wc -l datoteka | cut -c1-2)
    echo recenica ima $br rijeci

    if [ $br -gt 3 ] 
    then
        echo $recenica > Duge.Recenice.IB
    elif [ $br -le 3 ] 
    then
        echo $recenica > Kratke.Recenice.IB
    else
        echo Molimo unijeti nešto
    fi  
done

There are a number of ways to do this, but this approach is fine. 有很多方法可以做到这一点,但这种方法很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM