shell脚本学习案列

chmod u+x

./shxx


1.简单的shell程序设计 sh example 执行

显示所在的目录和文件

$cat example

#!/bin/sh

#this is to show what a example looks like

echo "Our first example"

echo # this

echo "we are "

/bin/pwd

echo 

echo "This directory contains the following file"

/bin/ls

2.写一个周记统计磁盘,用户了

sh sysunfo

#!/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 "momory info:- >> /tmp/sysinfo

/usr/bin/free -m >> /tmp/sysinfo

echo >> /tmp/sysinfo

#wirte root

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

# crontab -e

#0 9 * * 1-5 script

3.自动化备份的脚本

$1 代表位置变量

sh autoback

#!/bin/sh

#backup fies by date

DATE=`/bin/date +%Y%m%d`

/bin/tar -cf /backup/$1.$DEATE.tar $1 > /dev/null 2>> /backup/$1.back.log

/bin/gzip /backup/$1.$DATE.tar 

if[$? -eq 0]

then

    echo "$s1 $DATE backup successfuly " >> /backup/$1.back.log

else

    echo "$ERROR: falure $1 $DATE back!" >> /backup/$1.back.log

fi

#crontab -e

# 0 3 * * 2,5 acript

4.expr变量的使用  元算法

sh expr

#!/bin/sh

a=10 

b=20

c=30

value1=`expr $a +$b+$c`

echo"The value of vaule2 is value2"

vaule2=`expr $c /$b`

echo"The value of vaule2 is value2"

value3=`expr $a +$c /$b`

echo "The value of value 4 is $value4"


5.test的字符判断的值 (在服务器中的检测服务有么有启动,没启动则启动)

cat test.apache

#!/bin/sh

# if....esle" usage

# using this program to show your system‘s services.

echo "NOW, the web service of this Linux system will be detect..."

echo

  

 # Detect www service 

   web=`/usr/bin/pgrep httpd`

if {"$web"=""}

   then

       echo "The web service is running."

   else

       echo "The web service is NOT running."

       /etc/rc.d/init.d/httpd start

   fi

6.键盘输入读取用户所在组、管理群等信息 例 ./userinfo root

cat userinfo.sh

#!/bin/sh

#display user‘s inof ...

/bin/echo "Please input the username"

read username

/bin/grep $username /etc/passwd > /dev/null 2> /dev/null

if {$? -eq 0}

then 

  /bin/echo "username is : $username"

else 

  /bin/echo "user $username does not exist"

  exit 1

fi

/bin/echo

# list /etc/passwd info

userinfo=`/bin/grep ``$username:x /etc/passwd`

userid=`/bin/echo $userinfo | /bin/awk -F :`{print $3}``

groupid=`/bin/echo $userinfo | /bin/awk -F :`{print $4}``

homedir=`/bin/echo $userinfo | /bin/awk -F :`{print $6}``

shell=`/bin/echo $userinfo | /bin/awk -F :`{print $7}``


#get group name from GID

grouptmpname=`cat /etc/group | /bin/grep :x:$groupid`

groupname=`/bin/echo $grouptmpname | /bin/awk -F : `{print $1}``

/bin/echo "user id is : $userid"

/bin/echo "default group is : $grouptmpname "

/bin/echo "shell is :$shell"

/bin/echo "group nembers info:"


# get login info

userlogin=`/user/bin/who | /bin/grep $ username`

/bin/echo


# get login info

userlogin=`/usr/bin/groups $ username`

if ["$userlogin"!=""]

then

    /bin/echo/ "username is online"

else

    /bin/echo/ "username NOT logged in"

fi

7. 踢出用户

实例 sh  killuser.sh test

cat killuser.sh

#!/bin/sh

# The script to kill logined user.


username="$1"

/bin/ps aux | /bin/grep $username | /bin/awk ‘{print $2}‘ > /tmp/temp.pid

killid =`cat /tmp/temp.pid`


for PID in $killid

do

       /bin/kill -9 $PID 2> /dev/null

done


8.批量添加用户

cat useradd.sh

#!/bin/sh

#Authur: Sam < E-mail :samlee@amorothernet>

# The script to add user

# /etc/passwd info

echo "pleass input username:"

read name

echo "pleass input number:"

read num

n=1

while [$n -le $num ]

do 

       /usr/sbin/useradd $name$n

       n=`expr $n+1`

done

# /etc/shadow info

echo " pleass input the password:"

read passwd

m=1

while [$m -le $num]

do 

      echo $passwd | /usr/bin/passwd --stdin $name$m > /dev/null

      m=`expr $m + 1`

done

9.删除用户

cat deluser.sh

#!/bin/sh

echo "please input username:"

read name

echo "please input number:"

read num

sum =0

while [ $sum -lt $sum]

do

sum=`expr $sum +1`

/usr/sbin/userdel -r $name$sum

done

10.对比命令的查看修改对比

cat setuid.sh

#!/bin/sh

# After the system installed,please check setudi files first for secrity

# mkdir /backup

#find / perm -4000 -o -perm -2000 > /backup /setuid.list

/usr/bin/find /-perm -4000 -o -perm -2000 > /tmp/setuid.check 2> /dev/null

for file in `/bin/cat /tmp/setuid.check`

do 

       /bin/grep $file /backup/setuid.list >dev/null

                if [ "$?" !="0" ]

                then

                     echo "$file isn‘t in list! it‘s danger!!"

                fi

done

/bin/rm /tmp/setuid.check


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。