【问题标题】:Palindrome checker using user input and while loop in bash在 bash 中使用用户输入和 while 循环的回文检查器
【发布时间】:2021-12-30 07:31:07
【问题描述】:

我正在尝试制作一个 bash 脚本来检查一个单词是否是我的家庭作业的回文,但我无法让我的 while 循环正常工作。脚本应该继续运行并要求回文,直到用户给出回文。有经验的人可以帮我解决这个问题,也许可以解释我做错了什么。

#!/bin/bash

if [ $# -le 2 ]           
then 
    echo "Enter the word"
    read input                  
fi 
echo $input > temp         
reverse=`rev temp`     
echo $reverse              


while [ ! $input==$reverse ]
do  echo "Not a palindrome"
    read input 

done
echo "it is a palindrome"    
rm temp

【问题讨论】:

  • == 周围缺少空格,最好引用参数,以防有人输入包含空格的单词。

标签: linux bash loops while-loop palindrome


【解决方案1】:

应该这样做:

#!/bin/bash
set -euo pipefail;

while true; do
    echo -n "Enter the word: ";
    IFS=$'\n\t ' read -d$'\n' -r inputword restofsentence;
    if [[ -n "${inputword}" && -z "${restofsentence}" ]] ; then
        reverse="$( rev <( echo "${inputword}" ) )";
        if [[ "${reverse}" == "${inputword}" ]] ; then
            echo "it is a palindrome";
            exit 0;
        else
            echo "Not a palindrome";
            echo "";
            echo "";
        fi;
    fi;
done;

我不确定$#(cli 参数的数量)在您的示例中提供了什么功能,因为您没有使用 cli 参数;所以我把那部分省略了。


至于它的作用:

set -euo pipefail;

有效地启用“严格模式”。使 BASH 不再是一种糟糕的脚本语言。它在此脚本中实际上并没有太大的影响,但默认情况下始终在每个脚本中都有它是一种很好的做法。

while true; do

开始一个循环并一直重复它。

echo -n "Enter the word: ";

-n 禁止在句子末尾使用通常的换行符。

IFS=$'\n\t '

显式设置内部字段分隔符。这意味着 bash 对单词的解释是,换行符 (\n)、制表符 (\t) 和空格 (' ') 上的输入“分开”;通常这是默认值,但手动设置 IFS 总是好的做法,无论是基于每个脚本,还是为每个 read-like 命令。

read -d$'\n' -r inputword restofsentence;

将用户输入读取到分隔符换行符 (-d$'\n'),即回车键。不允许将反斜杠转义 -r 用于多行输入等内容。输入行根据之前的IFS拆分成单词;之后第一个词进入inputword,潜在的第二/第三/...-词进入restofsentence。

if [[ -n "${inputword}" && -z "${restofsentence}" ]] ; then

我们希望inputword 有一个值(非空),我们希望restofsentence 是空的。如果不是这样,我们只需让while-loop 重复并再次请求输入。这样我们就不会处理空输入(因为inputword 会是空的),也不会处理多字输入(因为restofsentence 不会是空的)。

reverse="$( rev <( echo "${inputword}" ) )";

我们将inputword 回显到一个虚拟文件中,并将其传递给rev 程序,echos 是相反的值。我们将其存储在变量 reverse 中。

我们没有使用真正的文件(它存储在文件系统中,而不是仅在内存中)这一事实使我们不必在以后清理它。

if [[ "${reverse}" == "${inputword}" ]] ; then

我们比较 inputword 和 reverse 是否相同,如果它们相同,那么我们正在处理回文。

exit 0;

一旦我们成功回文,我们就退出了(退出代码0 表示没有错误)。


程序(特别是while-loop)不断重复,直到找到成功的回文。

【讨论】:

    【解决方案2】:

    试试这个:

    #!/bin/bash
    
    while :; do
        read -p "Enter a word: "
    
        if [[ ${REPLY} == "$(rev <(printf %s "${REPLY}"))" ]]; then
            echo "It is a palindrome."
            break
        fi
    
        echo "Not a palindrome."
    done
    

    或者,它可以像这样更简单地完成,但这不正确,因为理论上“这是一个回文”。即使循环中断不是由正确的输入而是其他错误引起的,仍然会发送。

    #!/bin/bash
    
    while read -p "Enter a word: "; [[ ${REPLY} != "$(rev <(printf %s "${REPLY}"))" ]]; do
        echo "Not a palindrome."
    done
    
    echo "It is a palindrome"
    

    顺便说一句,它使用Process Substitution。

    可以创建一个反转字符串的函数,这应该是微不足道的,但它是可选范围。

    【讨论】:

      【解决方案3】:

      首先让它工作。
      你想检查一个论点。给定参数后,将其分配给输入。引用$input 避免出现特殊字符和空格的问题。在== 周围添加空格,使其在循环中再次比较和计算反向。我还使用了$(rev temp),它比 backtics 效果更好。

      #!/bin/bash
      if [ $# -ne 1 ]
      then
          echo "Enter the word"
          read input
      else
        input="$1"
      fi
      echo "$input" > temp
      reverse=$(rev temp)
      echo "$reverse"
      
      while [ ! "$input" == "$reverse" ]
      do
        echo "Not a palindrome"
        read input
        echo "$input" > temp
        reverse=$(rev temp)
      done
      echo "it is a palindrome"
      rm temp
      

      你可以避免这样的 tmp 文件:

      #!/bin/bash
      if [ $# -ne 1 ]
      then
          echo "Enter the word"
          read input
      else
        input="$1"
      fi
      while [ ! "$input" == "$(rev <<< "${input}")" ]
      do
        echo "Not a palindrome"
        read input
      done
      echo "it is a palindrome"
      

      【讨论】:

        猜你喜欢
        • 2016-05-20
        • 2021-06-17
        • 1970-01-01
        • 1970-01-01
        • 2020-02-15
        • 2015-07-27
        • 2023-03-28
        • 1970-01-01
        • 2013-11-16
        相关资源
        最近更新 更多