下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第三章 Linux 下 shell 编程,Shell脚本(外壳脚本)就是将多条终端命令编写到一起,通过使用变量和流程控制等实现一定的交互式功能。,一、什么是 shell:,用户与Linux的接口 命令解释器 编程语言,二、shell的种类、创建与执行:,Linux中有多种不同的shell,如csh、tcsh、ash、bash 等 , 但是通常我们使用 bash 进行shell编程,因为 bash 是免费的并且很容易使用。,1、用vi 编辑器将一系列命令别写成 .sh文件,程序必须以下面的行开始#!/bin/sh,2、用chmod修改该文件的执行权限;,3、./ 文件名 或者 sh 文件名,以#
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”,在单个命令行中使用多个命令的方法,2、变量的创建与引用:,可以在任何时间通过简单的赋值来创建:,variable name = value,
3、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,常见的环境变量有:HOME PATH PS1等,可以像定义一个变量一样来设置环境变量,在标记它为环境变量时需要使用“ export ”命令应用示例: $ export MYENV=1 $ echo $MYENV 使用“ set ”命令可以获取当前上下文中全部的变量,用户变量: 环境变量: 位置变量:$0 $1 $2
4、 $3 $# $* $,变量:,环境变量:,Vi etc/profile (PATH=$PATH:. export PATH),$# :用于存放命令行中所键入的参数个数 $*:可以引用传递给程序的所有参数 $:和$*一样,执行Shell脚本时可以使用参数。由出现命令行上的位置确定的参数称做位置参数。位置变量:$0 、$1、 $2、$3 $#、 $*、 $。,位置变量,echo $# arguments passed echo they are $*,编写脚本args:,执行:chmod ./args a b c,shiftdemo程序如下: echo $# $* shift echo $# $
5、* shift echo $# $* shift echo $# $* 执行: chmod +x shiftdemo ./ shiftdemo a b c,位置参数的 shift 命令,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 $countabc 编写一段程序计
6、算两个输入数之差,4、测试命令: test 和 ,求值表达式,并返回 true 或者 false,数值测试 串测试 文件测试,5、if case 条件控制语句:,if 测试条件1 then conmmand1 elif 测试条件2 then command2 Else fi,case string in string1 ) commands;; string2 ) commands;; * ) commands; esac,echo please enter your score: read c if test $c -lt 80 then echo bad! elif test $c -lt
7、 90 -a $c -ge 80 then echo good! else echo very good! fi,-a 并且 -o 或者 !非,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,echo “本脚本提供以下服务: echo n “1)列出当前文件夹内的基本信息;“ echo “2)列出当
8、前文件夹内的所有信息; echo “3)退出脚本; echo “请输入您的选择1-3 read choice case $choice in 1)ls ; 2)ls -l; 3)exit; *)echo “选择有误!; esac,用case结构实现三项基本服务的功能:,用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) echo eight; 9) echo nine; esac,# d
9、isplay 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,6、for until while循环语句:,for var in word1 word2wo
10、rdn 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会用参数自动赋值,# 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 name do if -f $name then cat $name echo E
11、nd of $ dir / $name else echo Invalid file name: $ dir / $name fi done else echo Bad directory name : $dir fi,While循环,while 表达式 do command done,while “$#” -ne 0 do echo “$1” shift done 执行: $ chmod +x whiledemo $ ./whiledemo a b c,until command1 do command done,Until 循环,xuehao=1 while $xuehao -le 3 d
12、o 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,7、函数的定义和调用,一个函数是一个子程序,用于实现一串操作的代码块(code block),它是完成特定任务的黑盒子. 当有重复代码, 当一个任务只需要很少的修改就被重复几次执行时, 这时你应考虑使用函数.,funcname() command . command ,函数被调用或被触发, 只需要简单地用函数名调用.,iscontin
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论