版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第三章 Linux 下 shell 编程,Shell脚本(外壳脚本)就是将多条终端命令编写到一起,通过使用变量和流程控制等实现一定的交互式功能。,一、什么是 shell:,用户与Linux的接口 命令解释器 编程语言,1,二、shell的种类、创建与执行:,Linux中有多种不同的shell,如csh、tcsh、ash、bash 等 , 但是通常我们使用 bash 进行shell编程,因为 bash 是免费的并且很容易使用。,1、用vi 编辑器将一系列命令别写成 .sh文件,程序必须以下面的行开始#!/bin/sh,2、用chmod修改该文件的执行权限;,3、./ 文件名 或者 sh 文件名,
2、以#开头的句子表示注释,直到这一行的结束,2,三、shell 编程:,1、echo 命令和 read 命令:,echo:输出变量或者常量到终端。,read:将终端的输入赋给某个变量。,echo “today is date ,please enter your name” # this is an example of echo and read command! read name echo -n “the name you entered is $name”,在单个命令行中使用多个命令的方法,3,2、变量的创建与引用:,可以在任何时间通过简单的赋值来创建:,variable name =
3、value,Linux里面所有的变量都被当做字符串,引用变量:$ variablename,a=5+4 b=$(5+4) c=expr 5 + 3 echo $a $b expr 5 + 3 c=$(a-b) (a=$a+1),a=hello world echo “a is: echo $a,4,常见的环境变量有:HOME PATH PS1等,可以像定义一个变量一样来设置环境变量,在标记它为环境变量时需要使用“ export ”命令应用示例: $ export MYENV=1 $ echo $MYENV 使用“ set ”命令可以获取当前上下文中全部的变量,用户变量: 环境变量: 位置变量:
4、$0 $1 $2 $3 $# $* $,变量:,环境变量:,Vi etc/profile (PATH=$PATH:. export PATH),5,$# :用于存放命令行中所键入的参数个数 $*:可以引用传递给程序的所有参数 $:和$*一样,执行Shell脚本时可以使用参数。由出现命令行上的位置确定的参数称做位置参数。位置变量:$0 、$1、 $2、$3 $#、 $*、 $。,位置变量,6,echo $# arguments passed echo they are $*,编写脚本args:,执行:chmod ./args a b c,7,shiftdemo程序如下: echo $# $* s
5、hift echo $# $* shift echo $# $* shift echo $# $* 执行: chmod +x shiftdemo ./ shiftdemo a b c,位置参数的 shift 命令,8,3、转义字符和通配字符:,转义字符指的是在Shell中有特殊含义的字符。 例如: | ? * $ “ ( ) # ls file1-10.c # count=ls l | grep d | wc l ls l /dev | more # cat b.txt #cat a.txtb.txt # echo $(5+4) #echo ”The date is date” #echo $
6、countabc 编写一段程序计算两个输入数之差,9,4、测试命令: test 和 ,求值表达式,并返回 true 或者 false,数值测试 串测试 文件测试,10,5、if case 条件控制语句:,if 测试条件1 then conmmand1 elif 测试条件2 then command2 Else fi,case string in string1 ) commands;; string2 ) commands;; * ) commands; esac,11,echo please enter your score: read c if test $c -lt 80 then ec
7、ho bad! elif test $c -lt 90 -a $c -ge 80 then echo good! else echo very good! fi,-a 并且 -o 或者 !非,12,if $1 -gt 100 then echo the number is greater than 100. elif $1 -lt 10 then echo the number is smaller than 10. else echo the number is between 10 and 100. fi $ ./ifdemo 100,13,echo “本脚本提供以下服务: echo n
8、“1)列出当前文件夹内的基本信息;“ echo “2)列出当前文件夹内的所有信息; echo “3)退出脚本; echo “请输入您的选择1-3 read choice case $choice in 1)ls ; 2)ls -l; 3)exit; *)echo “选择有误!; esac,用case结构实现三项基本服务的功能:,14,用case结构实现中英文数字转换的程序,if $# -ne 1 then echo usage: ./casedemo number exit 1 fi case $1 in 0) echo zero; 1) echo one; 2) echo two; 8) e
9、cho eight; 9) echo nine; esac,15,# display files under a given directory # $1-the name of the diectory # $2-the name of files dir=$1;shift if -d $dir then cd $dir if -f $1 then cat $1 echo End of $ dir / $1 else echo Invalid file name: $ dir / $1 fi else echo Bad directory name : $dir fi,16,6、for un
10、til while循环语句:,for var in word1 word2wordn do command done,for i in 1 2 3 do echo $i done,例如:,for var do command done,for i do echo $i done,执行时加上参数,以空格隔开,i会用参数自动赋值,17,# display files under a given directory # $1-the name of the diectory # $2-the name of files dir=$1;shift if -d $dir then cd $dir for
11、 name do if -f $name then cat $name echo End of $ dir / $name else echo Invalid file name: $ dir / $name fi done else echo Bad directory name : $dir fi,18,While循环,while 表达式 do command done,while “$#” -ne 0 do echo “$1” shift done 执行: $ chmod +x whiledemo $ ./whiledemo a b c,until command1 do command
12、 done,Until 循环,19,xuehao=1 while $xuehao -le 3 do echo Please enter the info about xuehao=$xuehao echo please enter name: read name echo please enter telephonenum: read tel echo xuehao:$xuehao name:$name tel:$tela.txt (xuehao=$xuehao+1) / xuehao=$($xuehao+1) done,xuehao=expr $xuehao + 1,20,7、函数的定义和调用,一个函数是一个子程序,用于实现一串操作的代码块(code block),它是完成特定任务的黑盒子. 当有重复代码, 当一个任务只需要很少的修改就被重复几次执行时, 这时你应考虑使用函数.,funcname() command . command ,函数被调用或被触发, 只需要简单地用函数名调用.,21,iscontinue(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 外研版小学四年级英语下册课时1 Unit1 Peopleatwork教学设计
- 放射性皮炎夏季的护理
- 规模化牦牛疾病治疗与诊断
- 2026下半年小学教师资格证考试政治全真模拟试卷及解析
- 【新教材】统编版(2024)|七年级上册道德与法治 12.2正确对待顺境和逆境 教案
- 儿童生长发育监测规范化诊疗 课件
- AI自制玩教具教程
- 人工智能开源框架解析
- 刺绣设计与应用-苏绣(可编辑)
- 2026及未来5年中国双底玻纤漏板数据监测研究报告
- 2026年河南省重点学校高一入学语文分班考试试题及答案
- AIAG CQI-35 中文版(线束质量指南 第一版 汽车线束全流程质量管控)
- 中国公证协会招聘考试真题2025
- 万人计划青年人才答辩万人计划青年拔尖人才课件
- 造价审计经验总结
- 燃料电池Fuelcell材料
- 微生物学 链球菌属
- 工贸企业安全生产培训心得7篇
- GB/T 5621-2008凿岩机械与气动工具性能试验方法
- GB/T 10855-2016齿形链和链轮
- 机场灯光站柴油发电机组拆除及安装安装方案计划
评论
0/150
提交评论