shell编程

#!/bin/sh
#auto mail for system info

/bin/date +%F >> /tmp/sysinfo
echo "disk info:" >> /tmp/sysinfo
/bin/df -h >> /tmp/sysinfo
echo >> /tmp/sysinfo
echo "online users:" >> /tmp/sysinfo
/usr/bin/who | /bin/grep -v root >> /tmp/sysinfo
echo >> /tmp/sysinfo
echo "memory info:" >> /tmp/sysinfo
/usr/bin/free -m >> /tmp/sysinfo
echo >> /tmp/sysinfo

#write root
/usr/bin/write xiaobai < /tmp/sysinfo && /bin/rm /tmp/sysinfo


test.apache文件
      1 #!/bin/sh
      2 # "if...else" usage
      3 # Using this program to show your system's services
      4
      5 echo "Now, the web services of this Linux system will to detect..."
      6 echo
      7
      8 # Detect www service
      9    web=`/usr/bin/pgrep httpd`
     10    if [ "$web" != "" ]
     11    then
     12        echo "The Web service is running."
     13    else
     14        echo "The Web service is NOT running."
     15        /etc/rc.d/init.d/httpd start
     16    fi

if_else文件
      1 #!/bin/sh
      2
      3 echo "please input a file name: "
      4 read file_name
      5 if [ -d $file_name ]
      6     then
      7     echo "$file_name is a directory"
      8 elif [ -f $file_name ]
      9     then
     10     echo "$file_name is common file"
     11 elif [ -c $file_name -o -b $file_name ]
     12     then
     13     echo "$file_name is a device file"
     14 else
     15     echo "$file_name is an unknown file"
     16 fi

test.sh文件
      1 #!/bin/sh
      2 if [ $# -ne 2 ];then
      3     echo "Not enough parameters"
      4     exit 0
      5 fi
    
for文件
      1 #!/bin/sh
      2
      3 for DAY in Sunday Monday Tuesday Wednesday Tursday Friday Saturday
      4 do
      5  echo "The day is : $DAY"
      6 done
    
userinfo.sh文件
      1 #!/bin/sh
      2 # display user's info
      3
      4 /bin/echo "Please input the username"
      5 read username
      6 /bin/grep $username /etc/passwd > /dev/null 2> /dev/null
      7 if [ $? -eq 0 ]
      8    then
      9         /bin/echo "username is : $username"
     10    else
     11        /bin/echo  "user $username does not exist"
     12        exit 1
     13 fi
     14 /bin/echo
     15
     16 # list /etc/passwd info
     17 userinfo=`/bin/grep ^$username:x /etc/passwd`
     18 userid=`/bin/echo $userinfo | /bin/awk -F: '{print $3}'`
     19 groupid=`/bin/echo $userinfo | /bin/awk -F : '{print $4}'`
     20 homedir=`/bin/echo $userinfo | /bin/awk -F : '{print $6}'`
     21 shell=`/bin/echo $userinfo | /bin/awk -F : '{print $7}'`
     22
     23 # get group name from GID
     24 grouptmpname=`cat /etc/group | /bin/grep :x:$groupid`
     25 groupname=`/bin/echo $grouptmpname | /bin/awk -F : '{print $1}'`
     26 /bin/echo "user id is : $userid"
     27 /bin/echo "default group is : $groupname"
     28 /bin/echo "home is : $homedir"
     29 /bin/echo "shell is : $shell"
     30 /bin/echo "group members infos:"
     31
     32 # get group members
     33 groups=`/usr/bin/groups $username`
     34 /bin/echo $groups
     35 /bin/echo
     36
     37 #get login info
     38 userlogin=`/usr/bin/who | /bin/grep $username`
     39 if [ "$userlogin" != "" ]
     40 then
     41     /bin/echo "$username is online"
     42 else
     43     /bin/echo "$username NOT logged in"
     44 fi
    
killuser.sh文件
      1 #!/bin/sh
      2 # The script to kill logined user.
      3
      4 username="$1"
      5
      6 /bin/ps aux | /bin/grep $username | /bin/awk '{print $2}' > /tmp/temp.pid
      7
      8 killid=`cat /tmp/temp.pid`
      9
     10 for PID in $killid
     11 do
     12     /bin/kill -9 $PID 2> /dev/null
     13 done
    
autobak.sh文件
      1 #!/bin/sh
      2 # backup files by date
      3
      4 DATE=`/bin/date +%Y%m%d`
      5 /bin/tar -cf /backup/$1.$DATE.tar $1 > /dev/null 2>>/backup/$1.bak.log
      6 /bin/gzip /backup/$1.$DATE.tar
      7 if [ $? -eq 0 ]
      8 then
      9     echo "$1 $DATE backup successfully" >> /backup/$1.bak.log
     10 else
     11     echo "ERROR:failure $1 $DATE backup!" >> /backup/$1.bak.log
     12 fi
     13
     14 # crontab -e
     15 # 0 3 * * 2,5 script
    
    
-c表示字符设备
-b表示块设备
[root@localhost ~]# ls -l /dev/tty
crw-rw-rw-. 1 root tty 5, 0 Jan 31 21:09 /dev/tty
[root@localhost ~]# ls -l /dev/sda
brw-rw----. 1 root disk 8, 0 Jan 31 21:10 /dev/sda

文件提取awk
awk -F: '$3==0 {print $1}' /etc/passwd
awk -F: 'length($2)==0 {print $1}' /etc/passwd
    
多个条件的联合
-a 与
-o 或

-eq =
-ge >=
-gt >
-le <=
-lt <
-ne !=


select文件
      1 #!/bin/sh
      2 # "select" Usage
      3
      4 echo "What is your favouriter OS?"
      5
      6 select var in "Linux" "UNIX" "Windows" "Others"
      7 do
      8     break
      9 done
     10
     11 echo "Your have selected $var"


case文件
      1 #!/bin/sh
      2
      3 echo "###################"
      4 echo "Please select your operation"
      5 echo " Please "C" to copy"
      6 echo " Please "D" to Delete"
      7 echo " Please "B" to Backup"
      8 echo "####################"
      9 read op
     10 case $op in
     11      C)
     12        echo "Your selection is Copy"
     13      ;;
     14      D)
     15        echo "Your selection is Delete"
     16      ;;
     17      B)
     18        echo "Your selection is Backup"
     19      ;;
     20      *)
     21        echo "invalide selection"
     22 esac

select.case文件
      1 #!/bin/bash
      2 # "select" "case" Usage
      3
      4 echo "a is 5,b is 3. Please select you method: "
      5
      6 a=5
      7 b=3
      8
      9 select var in "a+b" "a-b" "a*b" "a/b"
     10 do
     11     break
     12 done
     13
     14 case $var in
     15    "a+b") echo 'a+b='`expr $a "+" $b`;;
     16    "a-b") echo 'a-b='`expr $a "-" $b`;;
     17    "a*b") echo 'a*b='`expr $a "*" $b`;;
     18    "a/b") echo 'a/b='`expr $a "/" $b`;;
     19     *) echo "input error..."
     20 esac

while.sh文件
      1 while [ -d /etc ]
      2 do
      3     ls -ld /etc
      4     break
      5 done

while文件
      1 #!/bin/sh
      2
      3 num=1
      4 while [ $num -le 10 ]
      5 do
      6    SUM=`expr $num \* $num`
      7    echo $SUM
      8    num=`expr $num \+ 1`
      9 done

useradd.sh文件
      1 #!/bin/sh
      2 # Author:xiaobai
      3 # The script to add user
      4
      5 # /etc/passwd info
      6 echo "Please input username:"
      7 read name
      8 echo "Please input number:"
      9 read num
     10 n=1
     11 while [ $n -le $num ]
     12 do
     13     /usr/sbin/useradd $name$n
     14     n=`expr $n + 1`
     15 done
     16
     17 # /etc/shadow info
     18 echo "Please input the password:"
     19 read passwd
     20 m=1
     21 while [ $m -le $num ]
     22 do
     23     echo $passwd | /usr/bin/passwd --stdin $name$m
     24     m=`expr $m + 1`
     25 done

deluser.sh文件
      1 #!/bin/sh
      2 echo "please input username:"
      3 read name
      4 echo "please input number"
      5 read num
      6
      7 sum=0
      8 while [ $sum -lt $num ]
      9 do
     10    sum=`expr $sum + 1`
     11    /usr/sbin/userdel -r $name$sum
     12 done
    
until文件    
      1 #!/bin/sh
      2
      3 until [ -x /etc/inittab ]
      4 do
      5     /bin/ls -l /etc/inittab
      6     exit 0
      7 done

read.until文件
      1 #!/bin/bash
      2 # "read" "until" usage
      3
      4 echo "Please Y/y to stop..."
      5
      6 read input
      7
      8 until [ "$input" = "Y" ] || [ "$input" = "y" ]
      9 do
     10     echo "error input ,please try again..."
     11     read input
     12 done
     13
     14 echo "Stop here!"

break 文件
      1 #!/bin/sh
      2
      3 while true
      4 do
      5     echo "*******************"
      6     echo "Please select your operation"
      7     echo " 1 Copy"
      8     echo " 2 Delete"
      9     echo " 3 Backup"
     10     echo " 4 Quit"
     11     echo "*******************"
     12     read op
     13 case $op in
     14     1)
     15       echo "Your selection is Copy"
     16       ;;
     17     2)
     18       echo "Your selection is Delete"
     19       ;;
     20     3)
     21       echo "Your selection is Backup"
     22      ;;
     23     4)
     24       echo "Exit ..."
     25           break
     26      ;;
     27     *)
     28      echo "invalide selection,please tyr again"
     29          continue
     30    esac
     31 done


shift文件
       1 #!/bin/sh
      2
      3 if [ $# -le 0 ]
      4 then
      5     echo "Not enough parameters"
      6     exit 0
      7 fi
      8 sum=0
      9 while [ $# -gt 0 ]
     10 do
     11    sum=`expr $sum + $1`
     12    shift
     13 done
     14 echo $sum
    
function文件
      1 HELP()
      2 {
      3    echo "Usage: sh function \$1 \$2 \$3"
      4 }
      5
      6 if [ $# -ne 3 ]
      7 then
      8     HELP
      9 else
     10     echo "thank for your input , the three arguments is 1 2 3."
     11 fi

sh 脚本
1.对脚本有r权限
2.对脚本所在目录有rx权限

./脚本
1.对脚本有rx权限
2.对脚本所在目录有rx权限

setuid.sh文件
      1 #!/bin/sh
      2 # After the system installed, please check setuid files first for security.
      3 # mkdir /backup
      4 # find / -perm -4000 -o -perm -2000 > /backup/setuid.list 2> /dev/null
      5
      6 /usr/bin/find  / -perm -4000 -o -perm -2000 > /tmp/setuid.check 2> /dev/null
      7
      8 for file in `/bin/cat /tmp/setuid.check`
      9 do
     10     /bin/grep $file /backup/setuid.list > /dev/null 2>/dev/null
     11         if [ "$?" != "0" ]
     12         then
     13             echo "$file isn't in lis! it's danger!!"
     14         fi
     15 done
     16

     17 /bin/rm /tmp/setuid.check


监控apache服务脚本

#!/bin/bash
#http.sh
/usr/bin/nc -w2 192.168.10.1 80
if [ $? -ne 0 ]
then 
echo "httpd is error"|mail -s "httpd is down" root@localhost
fi


硬盘报警监控脚本

#!/bin/bash
#disk.sh

dnum=`df |awk 'NR==3{print int($4)}'`
if [ $dnum -gt 20 ]
then
echo "the disk is greater than 20%,now the disk spare is ${dnum}%"|mail -s "disk greater than 20%" root@localhost
fi

你可能感兴趣的:(编程,shell,File,delete,input,Parameters)