编写一个shell 脚本,输入一个字符串,忽略(删除)非字母后,检测该字符串是否为回文

请大神解答谢了...
2026年09月23日 08:37
有2个网友回答
网友(1):

read text
text=`echo $text|tr -c -d [:alpha:]`
r_text=`echo $text|rev`

if [ $text = $r_text ];then
    echo success
else
    echo failed
fi

这个基本满足你的要求, 但是输入的字符串不能有空格

网友(2):

#!/bin/bash

read text
text=`echo $text | tr -c -d [:alpha:]`
r_text=`echo $text | rev`
echo "r_text:"$r_text
if [ $text == $r_text ];then
echo success
else
echo failled
fi