shell之 if语句、case语句

1. if语句:

if语句的一般形式 

if

   条件1

then

   执行的命令1

elif             #elif可多次使用

   条件2

then

   执行命令2

.....

 

else

   执行命令3

fi

 

 

 

实验:

 cd /mnt

 rm -rf *

 ls

  vim if.sh

########


 sh if.sh


 sh if.sh a


 sh if.sh b


 sh if.sh c


 sh if.sh d


 

 

 

 

@@用if语句 判断文件类型:

 vim check_file.sh

###############   


 sh check_file.sh


 sh check_file.sh westos


 sh check_file.sh /etc/passwd


 sh check_file.sh /dev/vdb


 sh check_file.sh /mnt/

 sh check_file.sh /etc/system-release


 

 

@@用函数的方式判定文件类型:

 vim check_file.sh

###########


 

 sh check_file.sh


 sh check_file.sh westos


 sh check_file.sh /etc/passwd


 sh check_file.sh /mnt


 sh check_file.sh /etc/system-release


 

 

@@用户管理:

 vim user_create.sh

###########



 sh  user_create.sh userfile passfile

 touch userfile

 sh  user_create.sh userfile passfile

 touch  passfile

 sh  user_create.sh userfile passfile

 vim userfile

 cat userfile

 sh  user_create.sh userfile passfile

 

 

 

 

 

2. case语句

 

(1)case语句的一般形式:

 case 变量名 in

           常量1)       #可以判断多次;当常量1与变量名相同时,执行命令1,以此类推

           命令1

           ;;

           常量2)

           命令2

esac

         

(2)if语句与case语句的区别:

          if语句中的条件判断是从上到下顺序依次判断  

          case语句中的条件判断是同步的

 

 

##字符匹配常用case语句,因为if语句对于越置后的条件判断越慢,这样效率很低

实验1:

vim test.sh

######


 sh -x test.sh cat


 sh -x test.sh dog


 sh -x test.sh  we


 

实验2:

 vim test.sh

#########


 

 sh -x test.sh dog   


 sh -x test.sh cat   #都只执行了1次

 

//

 sh -x test.sh we

//

 

 

@@自动建立和删除文件中的用户

 vim userctl.sh

#########


 

 sh userctl.sh create user2

 id user3

 sh userctl.sh delete user3

 

 

 

 

3. expect语句:

 

expect                                       #是自动答应命令,用于交互式命令的自动执行

spawn                                       #是expect中的监控程序,其运行后会监控命令提出的交互问题

send                                          #发送问题答案给交互式命令

"\r"                                            #表示回车

exp_contitnue                            #表示当问题不存在时继续回答下面的问题

expect eof                                 #表示问题回答完毕推出expect环境

interact                                     #表示问题回答完毕留在交互式界面

set NAME [ lindex $argv n ]       #定义变量

 

 

 

@@创建分区  

 fdisk -l




 vim create_partition.sh

###############


 sh create_partition.sh   #执行脚本

 fdisk -l


 

 

@@删除第三个分区

 

 先创建3个分区

 fdisk -l

//

 vim delete_partition.sh

#####################


 sh delete_partition.sh


 fdisk -l


 

 

 

 

@@自动回答

实验1:

 yum install expect -y     #安装自动答应服务器

 vim ask.sh

######


 

 sh  ask.sh


 

vim answer.sh

#######


 expect answer.exp


 chmod +x /mnt/ask.sh

 expect answer.exp


 

实验2:

 vim  ask.sh

###############


 

 expect  answer.exp     #报错


vim answer.sh

#########


 

expect  answer.exp       


 

ask.sh还原 (取消注释)

 

实验3:

 vim answer.exp

##########



expect answer.exp lee 17 linux happy


 

 

##可用 #/bin/bash 将expect和shell联系起来

实验4:

 vim answer.exp

#########


  sh answer.exp lee 17 linux happy


 

 

 

 

@@自动连接ip           

 vim auto_connection.sh

#############


 sh auto_connection.sh 172.25.254.130 redhat




@@输出所有开着的主机的主机名和ip  

 

vim  auto_connection.sh

###############

  AUTO_CONNECT()

  {

  /usr/bin/expect <<EOF

  set timeout 10

  spawn ssh root@172.25.254.$i  hostname

  expect {

        "yes/no" { send "yes\r";exp_continue }

         "password" { send "westos\r" }    

          }

  expect eof

  EOF

 }

 for i in {76..79}

 do

                             ping -c1 -w1 172.25.254.$i &> /dev/null                                            

            HOSTNAME=`AUTO_CONNECT |grep -E "authenticity|fingerprint|ECDSA|connecting|Warning|pas    sword|spawn" -v`

                             echo $HOSTNAME 172.25.254.$i  | sed 's/\r//g'

  }

  done

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值