shell脚本,判断给出的字符串是否相等。第⼀种⽅法
[root@localhost wyb]# cat11.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]# cat111.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
shell 字符串长度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]#

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。