fswdnr 发表于 2018-8-23 08:40:35

shell3-pl

  ontinue
  break
  # vim s25.sh
  #!/bin/bash
  fori in `seq 1 10`
  do
  if [ $i -eq 5 ]
  then
  continue
  fi
  echo $i
  done
  #chmod +x s25.sh
  #./s25.sh
  1
  2
  3
  4
  6
  7
  8
  9
  10
  #vim s25.sh
  #!/bin/bash
  fori in `seq 1 10`
  do
  if [ $i -eq 5 ]
  then
  break
  fi
  echo $i
  done
  You have new mail in /var/spool/mail/root
  #chmod +x s26.sh
  #./s26.sh
  1
  2
  3
  4
  练习:输入用户名和密码 ,如果是root和redhat,输出“欢迎光临“,如果不正确,输出“不正确,请重新输入用户名和密码“。
  # vim s27.sh
  #!/bin/bash
  while :
  do
  read -p "请输入用户名:" username
  read -s -p "请输入密码:" password
  if [ $username -eq "root" -a $password -eq "redhat" ]
  then
  echo "欢迎光临"
  break
  else
  echo "不正确,请重新输入用户名和密码"
  fi
  done
  ~
  练习: 限制尝试次数为5次,5次以后禁止使用。
  # vim s28.sh
  #!/bin/bash
  i=1
  while [ $i -le 5 ]
  do
  read -p "请输入用户名:" username
  read -s -p "请输入密码:" password
  echo
  if [ $username == "root" -a $password == "redhat" ]
  then
  echo "欢迎光临"
  break
  elif [ $i -eq 5]
  then
  echo "你无权使用本程序"
  break
  else
  echo "不正确,你还有$次机会,请重新输>入用户名和密码"
  fi
  i=$[$i+1]
  done
  # chmod +x s28.sh
  # ./s28.sh
  -------------------------------
  > <|结合done用
  1) >
  # vim s29.sh
  #!/bin/bash
  for i in `seq 1 10`
  do
  echo $i
  done>/test/1.txt
  # chmod +x s29.sh
  #./s29.sh
  #cat /test/1.txt
  1
  2
  3
  4
  5
  6
  7
  8
  9
  10
  --------------------
  2)< while循环
  批量添加用户
  #vimuser_list
  utom   utomg    2000   /utom    /bin/bash   utomutom
  jarry    jarryg   2001   /jarry   /bin/zsh    jarryjarry
  larry    larryg   2002   /larry   /bin/tcsh   larrylarry
  #!/bin/bash
  #add user scripts
  while read line
  do
  uname=$(echo $line|awk '{print $1}')
  gname=$(echo $line|awk '{print $2}')
  uid=$(echo $line|awk '{print $3}')
  home=$(echo $line|awk '{print $4}')
  shell=$(echo $line|awk '{print $5}')
  passwd=$(echo $line|awk '{print $6}')
  grep "^\" /etc/group &>/dev/null
  if [ $? -ne 0 ]
  then
  groupadd $gname
  fi
  id $uname &>/dev/null
  if [ $? -ne 0 ]
  then
  useradd -g $gname -u $uid -d $home -s $shell $uname
  echo $passwd |passwd --stdin $uname
  fi
  done/test/ip.txt
  while read line
  do
  ip=$(echo $line|awk '{print $2}')
  iptables -I INPUT -s $ip -p tcp --dport 22 -j DROP
  done/dev/null
  }
  wd='192.168.3.'
  for i in `seq 1 254`
  do
  echo $wd$i
  done>/tmp/ip1.txt
  while read ip
  do
  testip $ip
  if [ $? -eq 0 ]
  then
  echo $ip >> /tmp/ip.txt
  fi
  done /root/.ssh/known_hosts
  # vim auto.exp
  #!/usr/bin/expect
  set timeout 60
  spawn ssh 192.168.3.2
  expect "continue connecting"
  send "yes\r"
  expect "password:"
  send "redhat1\r"
  interact
  # chmod +x auto.exp
  # ./auto.exp
  --------------------------
  脚本加密
  # tar zxvf shc-3.8.7.tgz -C /usr/local/src
  shc-3.8.7/CHANGES
  shc-3.8.7/Copying
  shc-3.8.7/Makefile
  shc-3.8.7/match
  shc-3.8.7/pru.sh
  shc-3.8.7/shc-3.8.7.c
  shc-3.8.7/shc.1
  shc-3.8.7/shc.README
  shc-3.8.7/shc.c
  shc-3.8.7/shc.html
  shc-3.8.7/test.bash
  shc-3.8.7/test.csh
  shc-3.8.7/test.ksh
  # cd /usr/local/src
  # cd shc-3.8.7/
  # make
  # make test
  # make strings
  # make expiration
  # ./shc -e 13/07/2013 -m "please connect larry@gmail.com" -f /test/aaa.sh
  ---------------------------------
  脚本调试
  1) 设置断点echo
  2)sh -x file.sh

页: [1]
查看完整版本: shell3-pl