第10讲 Shell编程Word文档格式.docx
《第10讲 Shell编程Word文档格式.docx》由会员分享,可在线阅读,更多相关《第10讲 Shell编程Word文档格式.docx(21页珍藏版)》请在冰豆网上搜索。
指定执行脚本的shell
2.#注释行
3.命令和控制结构
#!
/bin/sh//指定执行脚本的shell
#Thisistoshowwhataexamplelookslike.//注释行
echo"
Ourfirstexample"
//此行是命令
echo#Thisinsertsanemptylineinoutput.
Wearecurrentlyinthefollowingdirectory."
/bin/pwd
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
命令的结果赋给变量:
案例:
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//双引号变量读取取值
位置变量和特殊变量:
1.由命令行上的位置确定的参数称位置参数(位置变量)
2.特殊变量
$?
用来判断一个命令执行的正误
read命令:
从键盘读入数据,赋给变量
例如:
[root@localhostshell.example]#readname1name2name3
shaolin1shaolin2shaolin3//此处是在命令提示符下从键盘上输入的值
[root@localhostshell.example]#echo$name1$name2$name3
shaolin1shaolin2shaolin3
expr命令:
案例1:
简单运算
[root@localhostshell.example]#expr3+5
8
[root@localhostshell.example]#expr3-5
-2
[root@localhostshell.example]#expr3/5
[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
…..
第三种:
多个条件的联合:
for…done语句:
案例:
[root@localhostshell.example]#vifor
#!
/bin/sh
forDAYinSundayMondayTuesdayWednesdayThursdayFridaySaturday
do
echo"
Thedayis:
$DAY"
done
[root@localhostshell.example]#shfor
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Select语句:
范例:
[root@localhostshell.example]#viselect
#"
select"
Usage
WhatisyourfavouriteOS?
"
selectvarin"
Linux"
"
UNIX"
Windows"
Other"
break
Youhaveselected$var"
[root@localhostshell.example]#shselect
1)Linux
2)UNIX
3)Windows
4)Other
#?
1
YouhaveselectedLinux
Case…esac语句:
[root@localhostshell.example]#viselect.case
/bin/bash
case"
ais5,bis3.Pleaseselectyoumethod:
a=5
b=3
a+b"
a-b"
a*b"
a/b"
case$varin
"
)echo'
a+b='
`expr$a"
+"
$b`;
;
a-b='
-"
a*b='
*"
a/b='
/"
*)echo"
inputerror..."
esac
[root@localhostshell.example]#shselect.case
1)a+b
2)a-b
3)a*b
4)a/b
a-b=2
While语句:
、
[root@localhostshell.example]#viwhile
while[$num-le10]
SUM=`expr$num\*$num`
echo$SUM
num=`expr$num+1`
[root@localhostshell.example]#shwhile
4
9
16
25
36
49
64
81
100
Until语句:
[root@localhostshell.example]#viuntil
until[-x/etc/inittab]
/bin/ls-l/etc/inittab
exit0
[root@localhostshell.example]#shuntil
-rw-r--r--1jacksys1668May2216:
05/etc/inittab
跳出循环:
break和continue
Shift指令:
[root@localhostshell.example]#vishift
if[$#-le0]
then
echo"
Notenoughparameters"
fi
sum=0
while[$#-gt0]
sum=`expr$sum+$1`
shift
echo$sum
~
[root@localhostshell.example]#shshift
Notenoughparameters
函数应用:
函数的定义:
函数中的变量:
[root@localhostshell.example]#vifunction
HELP()
{
Usage:
shfunction\$1\$2\$3"
}
if[$#-ne3]
HELP//调用函数
else
thankforyourinput,thethreeargumentsis123."
分段提取
格式:
awk–F域分隔符‘命令’
自动生成系统信息
[root@localhostshell.example]#visysinfo.sh
#automailforsysteminfo
/bin/date+%F>
>
/tmp/sysinfo时间
diskinfo:
>
/tmp/sysinfo磁盘信息
/bin/df-h>
/tmp/sysinfo
echo>
onlineusers:
/tmp/sysinfo在线用户信息
/usr/bin/who|/bin/grep-vroot>
memoryinfo:
/tmp/sysinfo内存信息
/usr/bin/free-m>
#writeroot
/usr/bin/writeroot<
/tmp/sysinfo&
&
/bin/rm/tmp/sysinfo
#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
#Thescripttokilllogineduser.
/bin/psaux|/bin/grep$1|/bin/awk'
{print$2}'
/tmp/temp.pid
killid=`cat/tmp/temp.pid`
forPIDin$killid
/bin/kill-9$PID2>
/dev/null
[root@localhostshell.example]#shkilluser.shzhuangql
Killed
批量添加用户
[root@localhostshell.example]#viuseradd.sh
#Author:
Sam<
E-mail:
samlee@>
#Thescripttoadduser
#/etc/passwdinfo
pleaseinputusername:
readname//用户名前缀
pleaseinputnumber:
readnum
n=1
while[$n-le$num]
/usr/sbin/useradd$name$n//创建用户名
n=`expr$n+1`//命令替换符
#/etc/shadowinfo
pleaseinputthepassword:
readpasswd
m=1
while[$m-le$num]
echo$passwd|/usr/bin/passwd--stdin$name$m//设置密码
m=`expr$m+1`
#dos2unixscript
[root@localhostshell.example]#shuseradd.sh
user
10
111111
Changingpasswordforuseruser1.
passwd:
allauthenticationtokensupdatedsuccessfully.
Changingpasswordforuseruser2.
Changingpasswordforuseruser3.
Changingpasswordforuseruser4.
Changingpasswordforuseruser5.
Changingpasswordforuseruser6.
Changingpasswordforuseruser7.
Changingpasswordforuseruser8.
Changingpasswordforuseruser9.
Changingpasswordforuseruser10.
删除用户
[root@localhostshell.example]#videluser.sh
readname//用户前缀
readnum//用户数量
while[$sum-lt$num]
sum=`expr$sum+1`
/usr/sbin/userdel-r$name$sum
Done
[root@localhostshell.example]#shdeluser.sh
5
显示用户信息
[root@localhostshell.example]#viuserinfo.sh
#displayuser'
sinfo...
/bin/echo"
Pleaseinputtheusername"
readusername
/bin/grep$username/etc/passwd>
/dev/null2>
if[$?
-eq0]
/bin/echo"
usernameis:
$username"
user$usernamedoesnotexist"
exit1
/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