shell脚本,判断给出的字符串是否相等。

第一种方法
[root@localhost wyb]# cat 11.sh #!/bin/bash #判断给出的字符串是否相等 read -p "Please Input a number:" number [ -z $number ] && echo 'Input nothing' && exit 1 len=${#number} a=`echo $number|cut -c 1` for i in `seq $len` do b=`echo $number|cut -c $i` [[ "$a" != "$b" ]] && echo no && exit done echo yes [root@localhost wyb]# bash 11.sh Please Input a number:1111111122 no [root@localhost wyb]# bash 11.sh Please Input a number:1111111 yes [root@localhost wyb]# bash 11.sh Please Input a number:666666666 yes [root@localhost wyb]#

第二种方法 [root@localhost wyb]#
cat 111.sh #!/bin/bash #判断给出的字符串是否相等 read -p "Please Input a number:" number [ -z $number ] && echo 'Input nothing' && exit 1 a=`echo $number|cut -c 1` b=`echo $number|tr -d $a` [ -z $b ] && echo yes || echo no [root@localhost wyb]# bash 111.sh Please Input a number:111111 yes [root@localhost wyb]# bash 111.sh Please Input a number:123445 no [root@localhost wyb]# bash 111.sh Please Input a number:66666 yes [root@localhost wyb]#
原文地址:https://www.cnblogs.com/wangyuebo/p/5823544.html