




已阅读5页,还剩12页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第10讲 Shell编程Shell编程语法:2Shell 结构:2创建shell程序的步骤:2Shell变量:2Shell命令:4read 命令:从键盘读入数据,赋给变量4expr命令:4变量测试语句:4字符串测试5整数测试5文件测试6流控制语句:6exit语句:6If语句:6多个条件的联合:7for done 语句:7Select语句:8Case esac语句:8While语句:9Until语句:10跳出循环:break和continue11Shift 指令:11函数应用:12shell脚本调试:13Awk命令应用:13案例:自动生成系统信息13案例:自动把用户踢出系统14案例:批量添加用户14案例:删除用户15案例:显示用户信息16案例:17Shell编程语法:Shell 结构:1.#!指定执行脚本的shell2.#注释行3.命令和控制结构案例:#!/bin/sh /指定执行脚本的shell#This is to show what a example looks like. /注释行echo Our first example /此行是命令echo # This inserts an empty line in output.echo We are currently in the following directory./bin/pwdechoecho This directory contains the following files/bin/ls创建shell程序的步骤:第一步:创建一个包含命令和控制结构的文件 :vi example第二步:修改这个文件的权限是它可以执行使用chmod u+x第三步:执行 .example(也可以使用“sh example”执行)Shell变量:Shell有两类变量:临时变量:shell程序内部定义的,其使用范围仅限于定义它的程序,对其它程序不可见。包括:用户自定义变量,位置变量。 永久变量:是环境变量,其值不随shell脚本的执行结束而消失。用户自定义变量:变量名=赋值定义是赋值: 如:num=1使用变量时,要在变量名前加上前缀“$”使用echo命令查看变量值:案例:rootlocalhost shell.example# num=1rootlocalhost shell.example# echo $num1rootlocalhost shell.example#变量之间赋值:rootlocalhost shell.example# num2=$numrootlocalhost shell.example# echo $num21rootlocalhost shell.example#命令的结果赋给变量:案例:time=date /用命令替换符rootlocalhost shell.example# time=daterootlocalhost shell.example# echo $timeMon May 23 11:27:28 CST 2011列出所有的变量:set命令案例:rootlocalhost shell.example# setBASH=/bin/bashBASH_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 变量名包含多个字的变量的方法:变量赋值单引号和双引号区别:双引号变量读取取值单引号变量会不读值显示案例:rootlocalhost shell.example# NAME=zhuangqianlinrootlocalhost shell.example# my=$NAME is a studentrootlocalhost shell.example# my1=$NAME is a studentrootlocalhost shell.example# echo $my$NAME is a student /单引号变量会不读值显示rootlocalhost shell.example# echo $my1zhuangqianlin is a student /双引号变量读取取值rootlocalhost shell.example#位置变量和特殊变量:1.由命令行上的位置确定的参数称位置参数(位置变量) 2.特殊变量$? 用来判断一个命令执行的正误Shell命令:read 命令:从键盘读入数据,赋给变量例如: rootlocalhost shell.example# read name1 name2 name3shaolin1 shaolin2 shaolin3 /此处是在命令提示符下从键盘上输入的值rootlocalhost shell.example# echo $name1 $name2 $name3shaolin1 shaolin2 shaolin3rootlocalhost shell.example#expr命令:案例1:简单运算rootlocalhost shell.example# expr 3 + 58rootlocalhost shell.example# expr 3 - 5-2rootlocalhost shell.example# expr 3 / 50rootlocalhost shell.example# expr 3 * 515案例2:复杂运算rootlocalhost shell.example# vi test.sha=20b=60c=expr $a + $b /用替换符:echo $crootlocalhost shell.example# sh test.sh80注意:运算符左右两侧必须有空格 乘法运算(*)是必须用转义字符. 形式为:*变量测试语句:作用:用来测试变量是否相等、是否为空、文件类型等。格式:test 测试条件 测试范围:整数、字符串、文件字符串测试 整数测试文件测试变量测试语句一般不单独使用,一般作为if语句的测试条件如: if test d $1 then. fi变量测试语句可用进行简化,如:Test d $1 等价于 -d $1流控制语句:作用:用于控制shell程序的流程exit语句:退出程序执行返回0:表示正常退出返回1:表示非正常退出 例如:exit 0If语句:格式:第一种:iffthen.fi第二种:ifthen.else .fi第三种:多个条件的联合:for done 语句:格式:案例:rootlocalhost shell.example# vi for#!/bin/shfor DAY in Sunday Monday Tuesday Wednesday Thursday Friday Saturdaydo echo The day is : $DAYdonerootlocalhost shell.example# sh forThe day is : SundayThe day is : MondayThe day is : TuesdayThe day is : WednesdayThe day is : ThursdayThe day is : FridayThe day is : SaturdaySelect语句:格式:范例:rootlocalhost shell.example# vi select#!/bin/sh# select Usageecho What is your favourite OS?select var in Linux UNIX Windows Otherdo breakdoneecho You have selected $varrootlocalhost shell.example# sh selectWhat is your favourite OS?1) Linux2) UNIX3) Windows4) Other#? 1You have selected Linuxrootlocalhost shell.example#Case esac语句:格式:案例:rootlocalhost shell.example# vi select.case #!/bin/bash# select case Usageecho a is 5, b is 3. Please select you method: a=5b=3select var in a+b a-b a*b a/bdo breakdonecase $var in 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 input error.esacrootlocalhost shell.example# sh select.case a is 5, b is 3. Please select you method: 1) a+b2) a-b3) a*b4) a/b#? 2a-b=2rootlocalhost shell.example#While语句:格式:、案例:rootlocalhost shell.example# vi while#!/bin/shnum=1while $num -le 10 do SUM=expr $num * $num echo $SUM num=expr $num + 1donerootlocalhost shell.example# sh while 149162536496481100rootlocalhost shell.example#Until语句:格式:案例:rootlocalhost shell.example# vi until #!/bin/shuntil -x /etc/inittab do /bin/ls -l /etc/inittab exit 0donerootlocalhost shell.example# sh until -rw-r-r- 1 jack sys 1668 May 22 16:05 /etc/inittabrootlocalhost shell.example#跳出循环:break和continueShift 指令:范例:rootlocalhost shell.example# vi shift #!/bin/shif $# -le 0 then echo Not enough parameters exit 0fisum=0while $# -gt 0 do sum=expr $sum + $1 shiftdoneecho $sumrootlocalhost shell.example# sh shift Not enough parameters函数应用:函数的定义:函数中的变量:范例:rootlocalhost shell.example# vi function HELP()echo Usage: sh function $1 $2 $3if $# -ne 3 then HELP /调用函数else echo thank for your input, the three arguments is 1 2 3. fishell脚本调试:Awk命令应用:作用:分段提取格式:awk F 域分隔符 命令案例:自动生成系统信息rootlocalhost shell.example# vi sysinfo.sh #!/bin/sh# auto mail for system info/bin/date +%F /tmp/sysinfo 时间echo disk info: /tmp/sysinfo 磁盘信息/bin/df -h /tmp/sysinfoecho /tmp/sysinfoecho online users: /tmp/sysinfo 在线用户信息/usr/bin/who | /bin/grep -v root /tmp/sysinfoecho /tmp/sysinfoecho memory info: /tmp/sysinfo 内存信息/usr/bin/free -m /tmp/sysinfoecho /tmp/sysinfo# write root/usr/bin/write root /tmp/temp.pidkillid=cat /tmp/temp.pidfor PID in $killiddo /bin/kill -9 $PID 2 /dev/nulldonerootlocalhost shell.example# sh killuser.sh zhuangqlKilledrootlocalhost shell.example#案例:批量添加用户rootlocalhost shell.example# vi useradd.sh #!/bin/sh# Author: Sam # The script to add user# /etc/passwd infoecho please input username:read name /用户名前缀echo please input number:read numn=1while $n -le $num do /usr/sbin/useradd $name$n /创建用户名 n=expr $n + 1 /命令替换符done# /etc/shadow infoecho please input the password:read passwdm=1while $m -le $num do echo $passwd | /usr/bin/passwd -stdin $name$m /设置密码 m=expr $m + 1done# dos2unix scriptrootlocalhost shell.example# sh useradd.sh please input username:userplease input number:10please input the password:111111 Changing password for user user1.passwd: all authentication tokens updated successfully.Changing password for user user2.passwd: all authentication tokens updated successfully.Changing password for user user3.passwd: all authentication tokens updated successfully.Changing password for user user4.passwd: all authentication tokens updated successfully.Changing password for user user5.passwd: all authentication tokens updated successfully.Changing password for user user6.passwd: all authentication tokens updated successfully.Changing password for user user7.passwd: all authentication tokens updated successfully.Changing password for user user8.passwd: all authentication tokens updated successfully.Changing password for user user9.passwd: all authentication tokens updated successfully.Changing password for user user10.passwd: all authentication tokens updated successfully.rootlocalhost shell.example#案例:删除用户rootlocalhost shell.example# vi deluser.sh #!/bin/shecho please input username:read name /用户前缀echo please input number:read num/用户数量sum=0while $sum -lt $num dosum=expr $sum + 1/usr/sbin/userdel -r $name$sumDonerootlocalhost shell.example# sh deluser.sh please input username:userplease input number:5案例:显示用户信息rootlocalhost shell.example# vi userinfo.sh #!/bin/sh# display users info./bin/echo Please input the usernameread username/bin/grep $username /etc/passwd /dev/null 2 /dev/nullif $? -eq 0 then /bin/echo username is : $usernameelse /bin/echo user $username does not exist exit 1fi/bin/echo#
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 小区室外路灯施工方案
- 小学篮球趣味活动策划方案
- 老旧小区施工方案暗标
- 决策咨询建议专刊方案
- 新疆阿拉沟水库施工方案
- 沈阳大赛活动会议方案策划
- 大学创意活动策划方案公司
- 五一永州景区活动策划方案
- 火锅店过年开业营销方案
- 造价咨询综合协调方案
- 小学生游泳队训练计划
- 2024从“小众运动”到“全民热潮”解码网球人群与市场机遇
- ktv营销管理方案
- 2025-2030中国质子治疗系统行业市场发展趋势与前景展望战略研究报告
- 输血规范培训制度
- 第一单元与班级共成长 教学设计-2023-2024学年道德与法治四年级上册(部编版)
- 干道工程(道路、燃气、雨污水管线、再生水管线)投标方案(技术方案)
- 房子互换简单协议书
- 钢结构厂房基础施工承包合同
- 江苏连云港历年中考作文题与审题指导(2003-2024)
- 劳务分包加采购合同标准文本
评论
0/150
提交评论