第10讲 Shell编程.docx
《第10讲 Shell编程.docx》由会员分享,可在线阅读,更多相关《第10讲 Shell编程.docx(21页珍藏版)》请在冰豆网上搜索。
第10讲Shell编程
第10讲Shell编程
Shell编程语法:
2
Shell结构:
2
创建shell程序的步骤:
2
Shell变量:
2
Shell命令:
4
read命令:
从键盘读入数据,赋给变量4
expr命令:
4
变量测试语句:
4
字符串测试5
整数测试5
文件测试6
流控制语句:
6
exit语句:
6
If语句:
6
多个条件的联合:
7
for…done语句:
7
Select语句:
8
Case…esac语句:
8
While语句:
9
Until语句:
10
跳出循环:
break和continue11
Shift指令:
11
函数应用:
12
shell脚本调试:
13
Awk命令应用:
13
案例:
自动生成系统信息13
案例:
自动把用户踢出系统14
案例:
批量添加用户14
案例:
删除用户15
案例:
显示用户信息16
案例:
17
Shell编程语法:
Shell结构:
1.#!
指定执行脚本的shell
2.#注释行
3.命令和控制结构
案例:
#!
/bin/sh//指定执行脚本的shell
#Thisistoshowwhataexamplelookslike.//注释行
echo"Ourfirstexample"//此行是命令
echo#Thisinsertsanemptylineinoutput.
echo"Wearecurrentlyinthefollowingdirectory."
/bin/pwd
echo
echo"Thisdirectorycontainsthefollowingfiles"
/bin/ls
创建shell程序的步骤:
第一步:
创建一个包含命令和控制结构的文件:
viexample
第二步:
修改这个文件的权限是它可以执行
使用chmodu+x
第三步:
执行.example
(也可以使用“shexample”执行)
Shell变量:
Shell有两类变量:
临时变量:
shell程序内部定义的,其使用范围仅限于定义它的程序,对其它程序不可见。
包括:
用户自定义变量,位置变量。
永久变量:
是环境变量,其值不随shell脚本的执行结束而消失。
用户自定义变量:
变量名=赋值
定义是赋值:
如:
num=1
使用变量时,要在变量名前加上前缀“$”
使用echo命令查看变量值:
案例:
[root@localhostshell.example]#num=1
[root@localhostshell.example]#echo$num
1
[root@localhostshell.example]#
变量之间赋值:
[root@localhostshell.example]#num2=$num
[root@localhostshell.example]#echo$num2
1
[root@localhostshell.example]#
命令的结果赋给变量:
案例:
time=`date`//用命令替换符
[root@localhostshell.example]#time=`date`
[root@localhostshell.example]#echo$time
MonMay2311:
27:
28CST2011
列出所有的变量:
set命令
案例:
[root@localhostshell.example]#set
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="3"[1]="2"[2]="25"[3]="1"[4]="release"[5]="i686-redhat-linux-gnu")
…………………..
删除变量:
unset命令
格式:
unset变量名
包含多个字的变量的方法:
变量赋值单引号和双引号区别:
双引号变量读取取值
单引号变量会不读值显示
案例:
[root@localhostshell.example]#NAME=zhuangqianlin
[root@localhostshell.example]#my='$NAMEisastudent'
[root@localhostshell.example]#my1="$NAMEisastudent"
[root@localhostshell.example]#echo$my
$NAMEisastudent//单引号变量会不读值显示
[root@localhostshell.example]#echo$my1
zhuangqianlinisastudent//双引号变量读取取值
[root@localhostshell.example]#
位置变量和特殊变量:
1.由命令行上的位置确定的参数称位置参数(位置变量)
2.特殊变量
$?
用来判断一个命令执行的正误
Shell命令:
read命令:
从键盘读入数据,赋给变量
例如:
[root@localhostshell.example]#readname1name2name3
shaolin1shaolin2shaolin3//此处是在命令提示符下从键盘上输入的值
[root@localhostshell.example]#echo$name1$name2$name3
shaolin1shaolin2shaolin3
[root@localhostshell.example]#
expr命令:
案例1:
简单运算
[root@localhostshell.example]#expr3+5
8
[root@localhostshell.example]#expr3-5
-2
[root@localhostshell.example]#expr3/5
0
[root@localhostshell.example]#expr3\*5
15
案例2:
复杂运算
[root@localhostshell.example]#vitest.sh
a=20
b=60
c=`expr$a+$b`//用替换符:
``
echo$c
[root@localhostshell.example]#shtest.sh
80
注意:
运算符左右两侧必须有空格
乘法运算(*)是必须用转义字符.形式为:
\*
变量测试语句:
作用:
用来测试变量是否相等、是否为空、文件类型等。
格式:
test测试条件
测试范围:
整数、字符串、文件
字符串测试
整数测试
文件测试
变量测试语句一般不单独使用,一般作为if语句的测试条件
如:
iftest–d$1then
….
fi
变量测试语句可用[]进行简化,如:
Test–d$1等价于[-d$1]
流控制语句:
作用:
用于控制shell程序的流程
exit语句:
退出程序执行
返回0:
表示正常退出
返回1:
表示非正常退出
例如:
exit0
If语句:
格式:
第一种:
iff…then
……..
fi
第二种:
if…then
……..
else
…..
fi
第三种:
多个条件的联合:
for…done语句:
格式:
案例:
[root@localhostshell.example]#vifor
#!
/bin/sh
forDAYinSundayMondayTuesdayWednesdayThursdayFridaySaturday
do
echo"Thedayis:
$DAY"
done
[root@localhostshell.example]#shfor
Thedayis:
Sunday
Thedayis:
Monday
Thedayis:
Tuesday
Thedayis:
Wednesday
Thedayis:
Thursday
Thedayis:
Friday
Thedayis:
Saturday
Select语句:
格式:
范例:
[root@localhostshell.example]#viselect
#!
/bin/sh
#"select"Usage
echo"WhatisyourfavouriteOS?
"
selectvarin"Linux""UNIX""Windows""Other"
do
break
done
echo"Youhaveselected$var"
[root@localhostshell.example]#shselect
WhatisyourfavouriteOS?
1)Linux
2)UNIX
3)Windows
4)Other
#?
1
YouhaveselectedLinux
[root@localhostshell.example]#
Case…esac语句:
格式:
案例:
[root@localhostshell.example]#viselect.case
#!
/bin/bash
#"select""case"Usage
echo"ais5,bis3.Pleaseselectyoumethod:
"
a=5
b=3
selectvarin"a+b""a-b""a*b""a/b"
do
break
done
case$varin
"a+b")echo'a+b='`expr$a"+"$b`;;
"a-b")echo'a-b='`expr$a"-"$b`;;
"a*b")echo'a*b='`expr$a"*"$b`;;
"a/b")echo'a/b='`expr$a"/"$b`;;
*)echo"inputerror..."
esac
[root@localhostshell.example]#shselect.case
ais5,bis3.Pleaseselectyoumethod:
1)a+b
2)a-b
3)a*b
4)a/b
#?
2
a-b=2
[root@localhostshell.example]#
While语句:
格式:
、
案例:
[root@localhostshell.example]#viwhile
#!
/bin/sh
num=1
while[$num-le10]
do
SUM=`expr$num\*$num`
echo$SUM
num=`expr$num+1`
done
[root@localhostshell.example]#shwhile
1
4
9
16
25
36
49
64
81
100
[root@localhostshell.example]#
Until语句:
格式:
案例:
[root@localhostshell.example]#viuntil
#!
/bin/sh
until[-x/etc/inittab]
do
/bin/ls-l/etc/inittab
exit0
done
[root@localhostshell.example]#shuntil
-rw-r--r--1jacksys1668May2216:
05/etc/inittab
[root@localhostshell.example]#
跳出循环:
break和continue
Shift指令:
范例:
[root@localhostshell.example]#vishift
#!
/bin/sh
if[$#-le0]
then
echo"Notenoughparameters"
exit0
fi
sum=0
while[$#-gt0]
do
sum=`expr$sum+$1`
shift
done
echo$sum
~
[root@localhostshell.example]#shshift
Notenoughparameters
函数应用:
函数的定义:
函数中的变量:
范例:
[root@localhostshell.example]#vifunction
HELP()
{
echo"Usage:
shfunction\$1\$2\$3"
}
if[$#-ne3]
then
HELP//调用函数
else
echo"thankforyourinput,thethreeargumentsis123."
fi
shell脚本调试:
Awk命令应用:
作用:
分段提取
格式:
awk–F域分隔符‘命令’
案例:
自动生成系统信息
[root@localhostshell.example]#visysinfo.sh
#!
/bin/sh
#automailforsysteminfo
/bin/date+%F>>/tmp/sysinfo时间
echo"diskinfo:
">>/tmp/sysinfo磁盘信息
/bin/df-h>>/tmp/sysinfo
echo>>/tmp/sysinfo
echo"onlineusers:
">>/tmp/sysinfo在线用户信息
/usr/bin/who|/bin/grep-vroot>>/tmp/sysinfo
echo>>/tmp/sysinfo
echo"memoryinfo:
">>/tmp/sysinfo内存信息
/usr/bin/free-m>>/tmp/sysinfo
echo>>/tmp/sysinfo
#writeroot
/usr/bin/writeroot
#crontab-e
#09**1-5script//将上面的脚本调到这里
创建计划任务:
[root@localhostshell.example]#crontab-e
09**1-5/bin/bash//test/shell.example/shell.example/sysinfo
案例:
自动把用户踢出系统
[root@localhostshell.example]#vikilluser.sh
#!
/bin/sh
#Thescripttokilllogineduser.
/bin/psaux|/bin/grep$1|/bin/awk'{print$2}'>/tmp/temp.pid
killid=`cat/tmp/temp.pid`
forPIDin$killid
do
/bin/kill-9$PID2>/dev/null
done
[root@localhostshell.example]#shkilluser.shzhuangql
Killed
[root@localhostshell.example]#
案例:
批量添加用户
[root@localhostshell.example]#viuseradd.sh
#!
/bin/sh
#Author:
Samsamlee@>
#Thescripttoadduser
#/etc/passwdinfo
echo"pleaseinputusername:
"
readname//用户名前缀
echo"pleaseinputnumber:
"
readnum
n=1
while[$n-le$num]
do
/usr/sbin/useradd$name$n//创建用户名
n=`expr$n+1`//命令替换符
done
#/etc/shadowinfo
echo"pleaseinputthepassword:
"
readpasswd
m=1
while[$m-le$num]
do
echo$passwd|/usr/bin/passwd--stdin$name$m//设置密码
m=`expr$m+1`
done
#dos2unixscript
[root@localhostshell.example]#shuseradd.sh
pleaseinputusername:
user
pleaseinputnumber:
10
pleaseinputthepassword:
111111
Changingpasswordforuseruser1.
passwd:
allauthenticationtokensupdatedsuccessfully.
Changingpasswordforuseruser2.
passwd:
allauthenticationtokensupdatedsuccessfully.
Changingpasswordforuseruser3.
passwd:
allauthenticationtokensupdatedsuccessfully.
Changingpasswordforuseruser4.
passwd:
allauthenticationtokensupdatedsuccessfully.
Changingpasswordforuseruser5.
passwd:
allauthenticationtokensupdatedsuccessfully.
Changingpasswordforuseruser6.
passwd:
allauthenticationtokensupdatedsuccessfully.
Changingpasswordforuseruser7.
passwd:
allauthenticationtokensupdatedsuccessfully.
Changingpasswordforuseruser8.
passwd:
allauthenticationtokensupdatedsuccessfully.
Changingpasswordforuseruser9.
passwd:
allauthenticationtokensupdatedsuccessfully.
Changingpasswordforuseruser10.
passwd:
allauthenticationtokensupdatedsuccessfully.
[root@localhostshell.example]#
案例:
删除用户
[root@localhostshell.example]#videluser.sh
#!
/bin/sh
echo"pleaseinputusername:
"
readname//用户前缀
echo"pleaseinputnumber:
"
readnum//用户数量
sum=0
while[$sum-lt$num]
do
sum=`expr$sum+1`
/usr/sbin/userdel-r$name$sum
Done
[root@localhostshell.example]#shdeluser.sh
pleaseinputusername:
user
pleaseinputnumber:
5
案例:
显示用户信息
[root@localhostshell.example]#viuserinfo.sh
#!
/bin/sh
#displayuser'sinfo...
/bin/echo"Pleaseinputtheusername"
readusername
/bin/grep$username/etc/passwd>/dev/null2>/dev/null
if[$?
-eq0]
then
/bin/echo"usernameis:
$username"
else
/bin/echo"user$usernamedoesnotexist"
exit1
fi
/bin/echo
#list/etc/passwdinfo
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:
'{prin