脚本实例

来源:互联网 发布:毁灭战士4优化 贴吧 编辑:程序博客网 时间:2024/05/21 12:41

1.强弱引用实例

 [root@silentha mnt]# vim 1.sh
 [root@silentha mnt]# chmod +x 1.sh
 [root@silentha mnt]# cat 1.sh
 #!/bin/bash
 echo '$$$$$ the time now is "'\ ` date +%T `\ '" $$$$$'
 [root@silentha mnt]# sh 1.sh
 $$$$$ the time now is " 16:57:00 " $$$$$
 
2.1分10秒的倒计时

 [root@silentha mnt]# vim time.sh
 [root@silentha mnt]# chmod +x time.sh
 [root@silentha mnt]# cat time.sh
 #!/bin/bash
 MIN=1
 for ((SEC=10;SEC>=0;SEC--))
 do
 echo -ne "After ${MIN}:${SEC}s is end "
 sleep 1
 echo -ne "\r    \r"
  while [ "$SEC" -le "0" -a "$MIN" -gt "0" ]
  do
  echo -ne "After ${MIN}:${SEC}s is end "
  echo -ne "\r    \r"
  ((MIN--))
  SEC=60
  done
 done
 
 
3.检测ip地址

 [root@silentha mnt]# vim check_ip1.sh
 [root@silentha mnt]# chmod +x check_ip1.sh
 [root@silentha mnt]# sh check_ip1.sh
 Please input you want check ip address: 172.25.254.19
 172.25.254.19 is up
 [root@silentha mnt]# sh check_ip1.sh
 Please input you want check ip address: 172.25.254.23
 172.25.254.23 is down
 [root@silentha mnt]# cat check_ip1.sh
 #!/bin/bash
 read -p "Please input you want check ip address: " IP
 ping -c1 -w1 $IP &> /dev/null && echo $IP is up || echo $IP is down
 
4.批量检测ip地址

 [root@silentha mnt]# vim check_ip.sh
 [root@silentha mnt]# chmod +x check_ip.sh
 [root@silentha mnt]# sh check_ip.sh
 172.25.254.15 is down
 172.25.254.16 is down
 172.25.254.17 is down
 172.25.254.18 is down
 172.25.254.19 is up
 172.25.254.20 is down
 [root@silentha mnt]# cat check_ip.sh
 #!/bin/bash
 for NUM in {15..20}
 do
 ping -c1 -w1 172.25.254.$NUM &> /dev/null && echo 172.25.254.$NUM is up || echo 172.25.254.$NUM is down
 done
 
5.数字运算符

 [root@silentha mnt]# vim check_num.sh
 [root@silentha mnt]# chmod +x check_num.sh
 [root@silentha mnt]# sh check_num.sh
 Please input a number: 7
 yes
 [root@silentha mnt]# sh check_num.sh
 Please input a number: 10
 yes
 [root@silentha mnt]# sh check_num.sh
 Please input a number: 11
 no
 [root@silentha mnt]# cat check_num.sh
 #!/bin/bash
 read -p "Please input a number: " NUM
 if
 [ "$NUM" -ge "0" -a "$NUM" -le "10" ]
 then
 echo yes
 else
 echo no
 fi
 
6.文件状态运算符

 [root@silentha mnt]# vim check_file.sh
 [root@silentha mnt]# chmod +x check_file.sh
 [root@silentha mnt]# sh check_file.sh
 Please input give me a file
 [root@silentha mnt]# sh check_file.sh haha
 haha is not exist
 [root@silentha mnt]# sh check_file.sh /etc/passwd
 /etc/passwd is a file
 [root@silentha mnt]# sh check_file.sh /mnt
 /mnt is a directory
 [root@silentha mnt]# cat check_file.sh
 #!/bin/bash
 if
 [ -e "$1" ]
 then
 [ -f "$1" -a ! -L "$1" ] && echo $1 is a file
 [ -b "$1" ] && echo $1 is a block
 [ -c "$1" ] && echo $1 is a count
 [ -d "$1" ] && echo $1 is a directory
 [ -L "$1" ] && echo $1 is a lik
 else
 [ -z "$1" ] && echo "Please input give me a file"
 [ -n "$1" ] && echo $1 is not exist
 fi
 
7.创建用户并设置密码

 [root@silentha mnt]# vim create_user.sh
 [root@silentha mnt]# chmod +x create_user.sh
 [root@silentha mnt]# sh create_user.sh
 please input you want create username: lala
 please input lala's password: lala create sucessfullly
 [root@silentha mnt]# cat create_user.sh
 #!/bin/bash
 read -p "please input you want create username: " NAME
 read -p "please input ${NAME}'s password: " -s PASS
 useradd $NAME
 echo $PASS | passwd --stdin $NAME &> /dev/null && echo $NAME create sucessfullly || echo error
 
 
8.批量创建用户并设置密码

 [root@silentha mnt]# vim userfile
 [root@silentha mnt]# cat userfile
 user1
 user2
 user3
 [root@silentha mnt]# vim passfile
 [root@silentha mnt]# cat passfile
 redhat
 redhat
 redhat
 [root@silentha mnt]# vim create_user.sh
 [root@silentha mnt]# chmod +x create_user.sh
 [root@silentha mnt]# sh create_user.sh
 Changing password for user user1.
 passwd: all authentication tokens updated successfully.
 Changing password for user user2.
 passwd: all authentication tokens updated successfully.
 Changing password for user user3.
 passwd: all authentication tokens updated successfully.
 [root@silentha mnt]# su - user1
 [user1@silentha ~]$ su - user2
 Password:
 Last failed login: Wed Jun 21 17:33:18 CST 2017 on pts/1
 There were 2 failed login attempts since the last successful login.
 [user2@silentha ~]$
 [root@silentha mnt]# cat create_user.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`
   [ "$MAXUSER" -eq "$MAXPASS" ]&&(
   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
   )||(
   echo $1 and $2 have different lines
   )
  elif
  [ ! -e "$1" ]
  then
  echo "ERROR:$1 is not exsit"
  else
  echo "ERROR:$2 is not exsit"
  fi
 else
 echo "ERROR: Please input userfile and password file after command !!"
 fi
 
 
9.自动应答脚本

 [root@silentha mnt]# vim ask.sh
 [root@silentha mnt]# chmod +x ask.sh
 [root@silentha mnt]# cat ask.sh
 #!/bin/bash
 read -p "Who are you: " who
 read -p "How old are you: " old
 read -p "Are you happy: " feel
 echo $who is $old years old and feel $feel
 [root@silentha mnt]# vim expect.exp
 [root@silentha mnt]# chmod +x expect.exp
 [root@silentha mnt]# cat expect.exp
 #!/usr/bin/expect
 spawn /mnt/ask.sh
 expect "who"
 send "tom\r"
 expect "old"
 send "18\r"
 expect "happy"
 send "happy\r"
 expect eof
 exit
 [root@silentha mnt]# expect expect.exp
 spawn /mnt/ask.sh
 Who are you: tom
 How old are you: 18
 Are you happy: happy
 tom is 18 years old and feel happy
 





原创粉丝点击