chmod u+x
./shxx
1. Simple Shell Programming SH example execution
Display the directory and file in which you are located
$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. Write a weekly diary of the statistics disk, the user has
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. Scripts for automating backups
The position variable is represented
SH autoback
#!/bin/sh
#backup Fies by date
Date= '/bin/date +%y%m%d '
/bin/tar-cf/backup/$1. $DEATE. Tar $ >/dev/null 2>>/backup/$1.back.log
/bin/gzip/backup/$1. $DATE. Tar
If[$?-eq 0]
Then
echo "$s 1 $DATE backup successfuly" >>/backup/$1.back.log
Else
echo "$ERROR: Falure $ $DATE back!" >>/backup/$1.back.log
Fi
#crontab-E
# 0 3 * * 2,5 Acript
Using meta-algorithm for 4.EXPR variables
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 $value 4"
The value of the 5.test character judgment (detection service in the server has the boot, does not start the boot)
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 would 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. Keyboard input Read the user group, Management group and other information examples./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 $} '
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 '} '
/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. Kick the user
Instance SH killuser.sh Test
Cat killuser.sh
#!/bin/sh
# The script to kill logined user.
Username= "$"
/bin/ps aux | /bin/grep $username | /bin/awk ' {print $} ' >/tmp/temp.pid
Killid = ' Cat/tmp/temp.pid '
For-PID in $killid
Do
/bin/kill-9 $PID 2>/dev/null
Done
8. Batch Add Users
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. Delete a user
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. Compare the view changes of the command
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 Script Learning case List