457475451 发表于 2018-8-23 11:30:29

shell2-12288655

  实验12.
  case语句
  # vim 大哥是个apple
  case $1 in
  apple)
  echo banana
  ;;
  banana)
  echo apple
  ;;
  *)
  echo error
  esac
  测试:
  # sh 大哥是个apple apple
  banana
  # sh 大哥是个apple banana
  apple
  # sh 大哥是个apple yz
  error
  实验13.
  expect自动应答脚本(输入ip和密码,制动远程连接主机)
  # yum install expect -y
  Loaded plugins: langpacks
  rhel_dvd                                                 | 4.1 kB   00:00
  Resolving Dependencies
  .....
  # vim ty16.exp
  #!/usr/bin/expect
  set IP [ lindex $argv 0 ]
  set PASS [ lindex $argv 1 ]
  spawn ssh root@$IP
  expect {
  "yes/no"
  {send "yes\r";exp_continue}
  "password:"
  {send "$PASS\r"}
  }
  interact
  # /mnt/ty16.exp 172.25.254.118 redhat
  spawn ssh root@172.25.254.118
  The authenticity of host '172.25.254.118 (172.25.254.118)' can't be established.
  ECDSA key fingerprint is eb:24:0e:07:96:26:b1:04:c2:37:0c:78:2d:bc:b0:08.
  Are you sure you want to continue connecting (yes/no)? yes
  Warning: Permanently added '172.25.254.118' (ECDSA) to the list of known hosts.
  root@172.25.254.118's password:
  Last login: Wed Dec 14 21:58:09 2016 from 172.25.254.18
  # ifconfig
  eth0: flags=4163mtu 1500
  inet 172.25.254.118netmask 255.255.255.0broadcast 172.25.254.255
  inet6 fe80::5054:ff:fe00:120bprefixlen 64scopeid 0x20
  ether 52:54:00:00:12:0btxqueuelen 1000(Ethernet)
  RX packets 16575bytes 1458237 (1.3 MiB)
  RX errors 0dropped 0overruns 0frame 0
  TX packets 9690bytes 1204275 (1.1 MiB)
  TX errors 0dropped 0 overruns 0carrier 0collisions 0
  实验14.
  教室11-25主机开着的显示主机名,未开就显示关闭
  # vim ty16.exp
  #!/usr/bin/expect
  set timeout 2
  set IP [ lindex $argv 0 ]
  set PASS [ lindex $argv 1 ]
  set COMM [ lindex $argv 2 ]
  spawn ssh root@$IP $COMM
  expect {
  "yes/no"
  {send "yes\r";exp_continue}
  "password:"
  {send "$PASS\r"}
  {send "$COMM\r"}
  }
  expect eof
  # vim ty17.sh
  #!/bin/bash
  for NUM in {17..21}
  do
  ping -c1 -w1 172.25.254.$NUM &> /dev/null && (
  /mnt/ty16.exp 172.25.254.$NUM westos hostname | grep -E "spawn|root|denied" -v )
  done
  # sh ty17.sh
  foundation20.ilt.example.com
  实验15.
  用户的密码匹配实验
  # vim ty20.sh
  #!/bin/bash
  if
  [ -n "$1" -a -n "$2" ]
  then
  if
  [ -e "$1" -a -e "$2" ]
  then
  MAXUSER=`wc -l $1 | cut -d " " -f 1`
  MAXPASS=`wc -l $2 | cut -d " " -f 1`
  if
  [ "$MAXUSER" -eq "$MAXPASS" ]
  then
  for NUM in $( seq 1 $MAXUSER )
  do
  USERNAME=`sed -n ${NUM}p $1`
  PASSWORD=`sed -n ${NUM}p $2`
  CKUSER=`getent passwd $USERNAME`
  [ -z "$CKUSER" ]&&(
  useradd $USERNAME
  echo $PASSWORD | passwd --stdin $USERNAME
  )||echo "$USERNAME exist !!"
  done
  else
  echo $1 and $2 have different lines
  fi
  elif
  [ ! -e "$1" ]
  then
  echo "ERROR:$1 is not exist"
  else
  echo "ERROR:$2 is not exist"
  fi
  else
  echo "ERROR:Please input userfile and password file after command !!"
  fi
  # sh ty20.shuserfile passfile
  Changing password for user westos.
  passwd: all authentication tokens updated successfully.
  Changing password for user redhat.
  passwd: all authentication tokens updated successfully.
  yz exist !!
  ##引用##
  引用有三种类型
  1.弱引用
  将字符串放置在双引号中,保留字符串中所有字符的文字值,$、`、\和!字符除外。
  echo "can I have a $FRUIT"
  echo "the current time is $(date+%)."
  2.强引用
  将字符串放置在单引号中,保留字符串中所有字符的文字值,同时禁用所有扩展。
  echo "make$$$Fast"
  rm'untitled folder'
  3.转义
  非引用的\是转义字符。它保留了下一个字符的文字值。如\$PATH是确切的字符串$PATH,而不是PATH变量的内容
  echo make\$\$\$Fast\!
  ls untitled\folder
  ##shell变量##
  若要定义或 指定值:
  FRUIT=apple
  若要参考或使用变量:
  $FRUIT
  ${FRUIT}
  test 和中括号的作用一样
  -z为空
  -n不为空

页: [1]
查看完整版本: shell2-12288655