shell脚本,检查给出的字符串是否为回文
2024-08-30 03:43:29
[root@localhost wyb]# cat .sh
#!/bin/bash
#检查给出的字符串是否为回文 read -p "Please input a String:" number
[ -z $number ] && echo "input nothing " && exit len=${#number}
count=$((len/)) for i in `seq $count`
do
lasti=$((len-i+))
first=`echo $number|cut -c $i`
two=`echo $number|cut -c $lasti` [[ "$first" != "$two" ]] && echo no && exit done
echo yes
[root@localhost wyb]# bash .sh
Please input a String:
yes
[root@localhost wyb]# bash .sh
Please input a String:
yes
[root@localhost wyb]# bash .sh
Please input a String:
no
[root@localhost wyb]# bash .sh
Please input a String:
yes
[root@localhost wyb]#
用rev来检查是不是回文
[root@localhost wyb]# cat rev12321.sh
#!/bin/bash
#检查给出的字符串是否为回文
read -p "Please input a String:" number
[ -z $number ] && echo "input nothing " && exit a1=`echo $number|tac`
b2=`echo $number|rev`
[[ "$a1" != "$b2" ]] && echo no || echo yes [root@localhost wyb]# bash rev12321.sh
Please input a String:
no
[root@localhost wyb]# bash rev12321.sh
Please input a String:
yes
[root@localhost wyb]# bash rev12321.sh
Please input a String:
no
[root@localhost wyb]# bash rev12321.sh
Please input a String:
yes
[root@localhost wyb]#
[root@localhost wyb]# cat rev.sh
#!/bin/bash
#检查给出的字符串是否为回文 read -p "Please input a String:" number
[ -z $number ] && echo "input nothing " && exit number2=`echo $number|rev` [[ "$number2" = "$number" ]] && echo yes || echo no
[root@localhost wyb]# bash rev.sh
Please input a String:
yes
[root@localhost wyb]# bash rev.sh
Please input a String:
no
[root@localhost wyb]# bash rev.sh
Please input a String:
yes
[root@localhost wyb]#
最新文章
- C语言 · 十六进制转十进制
- Newtonsoft.Json之JArray, JObject, JPropertyJValue
- ubuntu server nginx 安装与配置
- Ubuntu下VSFTPD(五)(匿名FTP设置方法)
- C# - Environment类,获取桌面的路径
- MongoDB系列之三(副本集配置)
- 用xml画水平虚线和竖直虚线.md
- NYOJ127 星际之门(一)【定理】
- 【2017下集美大学软工1412班_助教博客】团队作业9——事后分析(Beta版本)成绩公示
- java 类字面常量,泛化的Class引用
- 关于opcdaauto.dll的注册
- OpenGL笔记(四) API参考
- 新装Linux无法访问域名
- Git_简介
- 沉淀,再出发:jQuery的初步了解和入门
- DRP——JDBC中的Batch
- C# 超级狗 二次开发 读写数据 激活验证 存储数据库连接字符串
- 【09】循序渐进学 docker:docker swarm
- django初探-创建简单的博客系统(一)
- java将list<;T>;导出为xls文件
热门文章
- laravel 遍历循环
- c#实现优先级队列
- python 如何在 command 中能够找到 其他module
- hyperledger fabric 1.0.5 分布式部署 (二)
- java string(转)
- JS中一个new到底做了哪些事情?
- [HDU1595] find the longest of the shortest
- a标签中href=";";的几种用法
- centos下gitlab的简单安装配置
- 116 Populating Next Right Pointers in Each Node 每个节点的右向指针