第十章shell编程1.doc

上传人:仙人指路1688 文档编号:2388273 上传时间:2023-02-17 格式:DOC 页数:10 大小:40.50KB
返回 下载 相关 举报
第十章shell编程1.doc_第1页
第1页 / 共10页
第十章shell编程1.doc_第2页
第2页 / 共10页
第十章shell编程1.doc_第3页
第3页 / 共10页
第十章shell编程1.doc_第4页
第4页 / 共10页
第十章shell编程1.doc_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《第十章shell编程1.doc》由会员分享,可在线阅读,更多相关《第十章shell编程1.doc(10页珍藏版)》请在三一办公上搜索。

1、10.Shell编程1.Shell编程语法一个简单的shell程序:$ cat example #!/bin/sh #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/pwd echo echo This directory contains the following files /bin/ls shell结构:

2、1. #!指定执行脚本的shell 2. #注释行 3. 命令和控制结构 创建shell程序的步骤: 1.创建一个包含命令和控制结构的文件。 2.修改这个文件的权限使它可以执行。使用chmod u+x 3.执行./example(也可以使用“sh x example”执行)Shell变量:变量是shell传递数据的一种方法,用来代表每个取值的符号名。 Shell有两类变量:临时变量和永久变量。临时变量是shell程序内部定义的,其使用范围仅限于定义它的程序,对其它程序不可见。包括:用户自定义变量、位置变量。永久变量是环境变量,其值不随shell脚本的执行结束而消失。如 $PATH $LANG

3、$SHELL $PS1 用户自定义变量:用户定义的变量由字母或下划线开头,由字母、数字或下划线序列组成,并且大小写字母意义不同。变量名长度没有限制。 在使用变量值时,要在变量名前加上前缀“$”。设置和使用变量:设置变量:习惯上用大写字母来命名变量。变量名只能以字母表中的字符开头,不能用数字。 变量赋值:赋值号“=”两边应没有空格。定义时赋值,如NUM=1将一个命令的执行结果赋给变量,如:TIME=date将一个变量赋给另一个变量,如:A =$B使用echo命令查看变量值。例如:echo $A 例子:NUM=100 echo $NUM TIME=date echo $ TIME TIME=dat

4、e +% F echo $ TIME列出所有的变量:# set 包含多个字的变量:$NAME=Mike Ron 运行时出错,应改为:$NAME=“Mike Ron” 或 $NAME=Mike Ron 单引号和双引号的区别:单引号之间的内容原封不动地指定给了变量。# ABC=$NAME Junior# echo $ABC $NAME Junior 删除变量:# unset NAME 位置变量和特殊变量:位置变量:Shell解释执行用户命令时,将命令行的第一个部分作为命令名,其它部分作为参数。由出现在命令行上的位置确定的参数称为位置参数。 ls -l file1 file2 file3 $0 这个

5、程序的文件名 ls -l $n 这个程序的第n个参数值,n=1-9 特殊变量:$* 这个程序的所有参数 $# 这个程序的参数个数 $ 这个程序的PID $! 执行上一个后台命令的PID $? 执行上一个命令的返回值 #!/bin/sh# Usage: sh special file01 file02echo $# is: $#echo $* is: $*echo $? is: $?echo $ is: $ echo $! is: $! 2.Shell脚本调试Shell命令:read命令:从键盘读入数据,赋给变量,如:read USERNAME #! /bin/sh read first sec

6、ond third echo the first parameter is $first echo the second parameter is $second” echo the third parameter is $third expr 命令:对整数型变量进行算术运算expr 3 + 5 expr 3 - 5 expr 8 / 3 expr 8 * 10 复杂的expr命令复杂的运算:expr expr 5 + 7/$var4 将运算结果赋予变量:var4= expr $var1 / $var2 #!/bin/sh a=10 b=20 c=30 value1=expr $a + $b

7、+ $c echo The value of value1 is $value1 value2=expr $c / $b echo The value of value2 is $value2 value3=expr $c * $b echo The value of value3 is $value3 value4=expr $a + $c / $b echo The value of value4 is $value4 变量测试语句:用于测试变量是否相等、 是否为空、文件类型等。 格式:test 测试条件 测试范围:整数、字符串、文件 字符串测试:test str1=str2 测试字符串是

8、否相等 test str1!=str2 测试字符串是否不相等 test str1 测试字符串是否不为空test -n str1 测试字符串是否不为空test -z str1 测试字符串是否为空整数测试:test int1 -eq int2 测试整数是否相等test int1 -ge int2 测试int1是否=int2test int1 -gt int2 测试int1是否int2test int1 -le int2 测试int1是否=int2test int1 -lt int2 测试int1是否 /tmp/temp.pidkillid=cat /tmp/temp.pidfor PID in $

9、killiddo/bin/kill -9 $PID 2 /dev/nulldoneawk命令:awk -F 域分隔符命令检测系统中UID为0的用户:awk -F: $3=0 print $1 /etc/passwd检测系统中密码为空的用户:awk -F: length($2)=0 print $1 /etc/shadow ps -le | grep sshd | awk print $4awk命令例子:#!/bin/sh# display user info/bin/echo Please input the usernameread username/bin/grep $username /

10、etc/passwd /dev/null 2 /dev/nullif $? -eq 0 then/bin/echo the username is $usernameelse /bin/echo the username $username does not exist exit 1fi/bin/echo# list /etc/passwd infouserinfo=/bin/grep $username:x /etc/passwduserid=/bin/echo $userinfo | /bin/awk -F : print $3groupid=/bin/echo $userinfo | /

11、bin/awk -F : print $4homedir=/bin/echo $userinfo | /bin/awk -F : print $6shell=/bin/echo $userinfo | /bin/awk -F : print $7# get group name from GIDgrouptmpname=cat /etc/group | /bin/grep :x:$groupidgroupname=/bin/echo $grouptmpname | /bin/awk -F : print $1/bin/echo user id is : $userid/bin/echo hom

12、e directory is : $homedir/bin/echo default group is : $groupname/bin/echo shell is : $shell/bin/echo group members info#get group membersgroups=/usr/bin/groups $username/bin/echo $groups/bin/echo#get login infouserlogin=/usr/bin/who | /bin/grep $usernameif $userlogin != then/bin/echo $username is on

13、lineelse/bin/echo $username is offline Fiselect in 命令:格式:select 变量 in 关键字 do command 1 command n done select把关键字中的每一项做成类似表单,以交互的方式执行do和done之间的命令。#!/bin/sh#select Usageecho What is your favourite OS?select var in linux unix windows otherdobreakdoneecho you have chosen $varcaseesac语句:格式:case 变量 in字符串1

14、) 命令列表1 ; 字符串n) 命令列表n ; esac while语句:格式:while 条件 do 命令 done #! /bin/sh num=1 while $num -le 10 do SUM=expr $num * $numecho $SUM num=expr $num + 1 done until语句:until类似while循环,不同的是until是条件返回值为假时才继续执行。格式:until 条件 do 命令 done 跳出循环:break和continue break:跳出整个循环。continue:跳过本次循环,进行下次循环。例子#!/bin/sh# untilecho

15、Press Y/y to stop.read inputuntil $input = Y | $input = y do echo error input,please try again .read input doneecho Stop here!shift指令:参数左移,每执行一次,参数序列顺次左移一个位置,$#的值减1,用于分别处理每个参数,移出去的参数不再可用。例子#!/bin/shwhile truedoecho Please enter your selection : C,D,B,Eread opcase $op in C) echo your selection is Cop

16、y ; D) echo your selection is Delete ; B) echo your selection is Backup ; E) echo Exit . break ; *) echo invalid selection,please try again continueesac#! /bin/sh if $# -le 0 thenecho Not enough parameters exit 0 fi sum=0 while $# -gt 0 do sum=expr $sum + $1 shift done echo $sum 3.Shell应用实例函数的定义:函数名

17、 () 命令序列 函数的调用:不带(),函数名 参数1 参数2 函数中的变量:变量均为全局变量,没有局部变量 函数中的参数:调用函数时,可以传递参数,在函数中用$1、$2来引用 #!/bin/shHELP()echo Usage:sh function $1 $2 $3if $# -ne 3 ; then HELPelse echo thank you for your input, the three arguments is 1 2 3fi Shell 脚本调试:sh -x script 这将执行该脚本并显示所有变量的值。 sh -n script 不执行脚本只是检查语法的模式,将返回所有

18、语法错误。 脚本权限问题:1.sh脚本:对于脚本具有r权限 对于脚本所在目录具有rx权限2. ./脚本:对于脚本具有rx权限对于脚本所在目录具有rx权限练习:要求,通过命令输入两个数,shell程序完成比较两个数的大小。如果参数个数不满足条件,给出提示信息,最终给出比较两个数大小的提示信息。#!/bin/shif $# -ne 2 ; thenecho not enough parametersexit 0fiif $1 -eq $2 ; then echo $1 equals $2elif $1 -lt $2 ; then echo $1 is little than $2elif $1 -

19、gt $2 ;thenecho $1 is greater than $2fi练习:1.批量添加用户,要求用户输入用户的前缀名以及用户数目,密码用一致的密码。参考代码useradd:#!/bin/sh# add usersecho please input username:read nameecho please input number:read sumnum=1while $num -le $sum do /usr/sbin/useradd $name$numnum=expr $num + 1 doneecho please input password:read passwdnum=1

20、while $num -le $sum do echo $passwd | /usr/bin/passwd -stdin $name$numnum=expr $num + 1 done2.批量删除用户,要求用户输入用户的前缀名以及用户数目。参考代码deluser:#!/bin/sh# add usersecho please input username:read nameecho please input number:read sumnum=1while $num -le $sum do /usr/sbin/userdel -r $name$numnum=expr $num + 1 done

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号