shell脚本判断单词是否存在liunx字典中

2020-09-29 11:38发布

1.查看字典并把字典保存在一个变量中

#!/bin/bash
words=`cat /usr/share/dict/words`

2. 输入一个单词并把这个单词保存到word变量中

word=`read -ep "请输入单词:"`

3. 通过for循环以及if判断来确定单词是否存在

for i in $words
do
   if [ '$i'x='$word'x ];then
      echo "单词在字典中"
      break
   else
      echo "单词不在字典中"
      break
   fi
done

注意:
比较两个字符串是否相等的办法是:
if [ "$test"x = "test"x ]; then
这里的关键有几点:
1 使用单个等号
2 注意到等号两边各有一个空格:这是shell的要求
3 注意到"$test"x最后的x,这是特意安排的,因为当$test为空的时候,上面的表达式就变成了x = testx,显然是不相等的。而如果没有这个x,表达式就会报错:[: =: unary operator expected

 4.创建一个txt文档,并把所有服务器的ip存入

[root@localhost ~]# vim ip.txt

192.168.253.177

192.168.253.178

192.168.253.179

5.编辑shell文件

#!/bin/bash
iplist=`cat ip.txt`           #查看所有ip并存入一个变量中
for i in $iplist                #通过for循环来遍历每一个ip是否存活
do
   ping -c2 $i >/dev/null
   if [ $? -eq 0 ];then
      echo "ip is runing"
   else
      echo "ip is down"
   fi
done


转载自:CSDN  作者:Hya、

原文链接:https://blog.csdn.net/yeyslspi59/article/details/106800670