標籤: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 :[email protected]>
# 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
shell指令碼學習案列