zoukankan      html  css  js  c++  java
  • shell脚本,检查给出的字符串是否为回文

    [root@localhost wyb]# cat 12321.sh 
    #!/bin/bash
    #检查给出的字符串是否为回文
    
    read -p "Please input a String:" number
    [ -z $number ] && echo "input nothing " && exit 1
    
    
    len=${#number}
    count=$((len/2))
    
    for i in `seq $count`
    do
          lasti=$((len-i+1))
          first=`echo $number|cut -c $i`
          two=`echo $number|cut -c $lasti`
        
         [[ "$first" !=  "$two"  ]] && echo no && exit 2
    
    done
    echo yes
    [root@localhost wyb]# bash
    12321.sh Please input a String:111111 yes [root@localhost wyb]# bash 12321.sh Please input a String:12321 yes [root@localhost wyb]# bash 12321.sh Please input a String:11111111111111122 no [root@localhost wyb]# bash 12321.sh Please input a String:123321 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 1
    
    a1=`echo $number|tac`
    b2=`echo $number|rev`
    [[ "$a1" != "$b2" ]] && echo no || echo yes
    
    [root@localhost wyb]# bash rev12321.sh 
    Please input a String:123421
    no
    [root@localhost wyb]# bash rev12321.sh 
    Please input a String:1111111111
    yes
    [root@localhost wyb]# bash rev12321.sh 
    Please input a String:111111111122
    no
    [root@localhost wyb]# bash rev12321.sh 
    Please input a String:123321
    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 1
    
    
    number2=`echo $number|rev`
    
    [[  "$number2"  =  "$number" ]] && echo yes  || echo no
    [root@localhost wyb]# bash rev.sh 
    Please input a String:111111
    yes
    [root@localhost wyb]# bash rev.sh 
    Please input a String:111111111122
    no
    [root@localhost wyb]# bash rev.sh 
    Please input a String:123321
    yes
    [root@localhost wyb]# 
     
    
    


  • 相关阅读:
    android gradle 打包命令
    android RRO
    android adb 常用命令
    mui执行滑动事件: Unable to preventDefault inside passive event listener
    获取 input[type=file] 文件上传尺寸
    MySQL:You can't specify target table for update in FROM clause
    input标签中autocomplete="off" 失效的解决办法
    @media属性针对苹果手机写法
    centos7 下mysql5.7修改默认编码格式为UTF-8
    使用Mui加载数据后a标签点击事件失效
  • 原文地址:https://www.cnblogs.com/wangyuebo/p/5817607.html
Copyright © 2011-2022 走看看