Shell程序设计学习内容:1.什么是shell2.语法:变量、条件判断、程序控制3.函数4.命令和命令的执行5.Here文档6.调试7.Grep命令和正则表达式8.Find命令一、什么是shell?Shell是一个作为用户与Linux系统间接口的程序,它允许用户向操作系统输入需要执行的命令。
在一个linux系统中可以安装多个shell,这些shell和其他程序一起环绕在linux内核四周。
默认安装的标准是/bin/sh,它是GNU工具集中的bash。
使用/bin/bash –version 可以查看版本。
Shell 历史版本:sh csh,、tcsh、zsh ksh、pdksh bash二、shell脚本编写、运行、调试1.脚本均以#!/bin/bash开头。
2.脚本中的‘#’为注释符。
3.使用exit为脚本设置返回一个退出码,注意0 表示的是执行成功。
示例:#!/bin/bash# this is my first shell!echo “helloword!”exit 04.设置脚本为可执行:chmod u+x filename.sh5.执行:./filename.sh6.sh –x ./filename.sh三、shell语法1、变量:无需事先声明,直接使用,在访问时要加$在变量前。
例如:Str=helloecho $Stra.这两行语句将在屏幕输出一个“hello”,shell区别大小写,定义变量时应该注意。
b.如果为变量所赋的字符串中含有空格,table或换行符应使用“”标示,例如“hello word!”c.变量在引号中依然有效如echo “this is $Str!”依然会输出:this is hello!练习示例:(注:后续的示例中只有关键部分,练习时补全) echo “input password:”read pasdecho “the password is $pasd”d.如果需要输出字符$Str可以用单引号和\来处理:‘$Str’和\$Str 输出的都是$Str不是变量表示的值,shell 中的变量一般都是字符串形式。
e: 环境变量,介绍几种:$HOME 当前用户的家目录$PATH 搜索命令的目录列表$0 shell脚本的名字$# 传递给脚本的参数个数$$ 脚本的进程号f:参数变量:$1,$2脚本的程序参数$* 列出所有参数$@ S*的一种变体,它参数不会挤在一块。
g:变量做整形数字使用:a=123let “a +=1”echo “a = $a”2、条件1)使用test 或[ ]如if test –f file.cthen…..fi以上代码也可写成如下形式(尽量使用这种形式)if [ -f file.c ] (注意:if语句和[之间有个空格]then….fi完整示例:#!/bin/bashecho “is it morning? Please answer yes or no”read timeofdayif [ $timeofday = “yes” ] ;thenecho “good morning”elseecho “good afternoon”fiexit 02)elif语句,在if,else语句中增加分支判断。
如下示例:#!/bin/bashecho “is it morning? Please answer yes or no”read timeofdayif [ $timeofday = “yes” ] ;thenecho “good morning”elif [ $timeofday = “no” ]thenecho “Good afternoon”elseecho “sorry ,$timeofday not recognized. Enter yes or no”exit 1fiexit 03、一个与变量有关的问题。
如上程序,如果没有输入Yes和no直接按回车键,会出现什么结果呢?程序会有出错信息。
原因是。
避免该问题产生的方法对变量使用双引号“$Str”4、for语句使用for语句可以循环处理一组值,这组值可以是任意字符串的集合。
它的语法形式如下:for variable in valuesdosomethingdone示例:#!/bin/bashFor foo in hello myname 129doecho $foodoneexit 0使用通配符扩展for循环#!/bin/shfor file in $(ls *.sh);doecho $filedoneexit 05、while语句语法结构:while condition doDosometingdone示例:一个简陋的密码检查程序#!/bin/bashecho “Enter password”read pawordwhile [ “$paword”!= “secret” ]doecho “sorry, try again ”read paworddoneexit 0字符串比较:string = stringstring != string-n string-z string算数比较num1 –eq num2num1 –ne num26、until语句,与while循环类似,只是把测试条件反过来了。
语法形式如下:until conditiondoDosomethingdone7、case语句语法结构:case variable inpattern1[ | pattern]…) dosomething;;pattern2[ | pattern]…) dosomething;;esac该语句执行vairable与第一个pattern匹配上的语句。
例如:#!/bin/bashecho “is it morning? Please answer yes or no”read timeofdaycase “$timeofday” inyes ) echo “good moring”;;no ) echo “good afternoon”;;y) echo “good morning”;;n) echo “good afternoon”;;*)echo “sorry,answer not recognized” ;;esacexit 0匹配部分语句也可改写为:case “$timeofday”inyes | y | Yes | YES ) echo “good morning”;;n* | N* ) echo “good afternoon”;;*) echo “sorry, answer not recognized”;; esac也可以将匹配行改为; [yY]|[Yy][Ee][Ss]8、命令列表有时需要将好几条命令连接成一个序列。
如下if [ -f this_file ]; thenif [ -f that_file ]; thenif [ -f the_other_file ]; thenecho “all files present, and correct”fififi9、AND列表只有在前一条命令执行成功返回true才执行下一条。
语法结构:Statement1 &&Statement1 &&Statement1 && …示例:#!/bin/bashtouch file_onerm –f file_twoif [ -f file_one ] && echo “hello” && [ -f file_two ] && echo “there”thenecho “in if”elseecho “in else”fiexit 010、OR列表,直到有一条命令执行成功为止。
语法结构:Statement1 || Statement2 || Statement3 || …示例:if [ -f file_one ] || echo “hello” || echo “there”thenecho “in if”elseecho “in else”fi11、语句块如果想在只允许使用单个语句的地方(如AND,OR)使用多条语句,可以是花括号{}来构造一个块。
如下:Get_confirm && {e cho “hello”cat $tmp_file > $tracks_fileadd_record_tracks}12、函数要定义一个shell函数,只需写出它的名字,加上括弧,再把函数要实现的语句放在一对花括号中,如下所示:function_name () {Statements}示例:#!/bin/bashfoo(){echo “function foo is executing”}echo “ this is foo…”fooecho “foo end!”exit 0如果在函数中没有return命令指定一个返回值,则函数返回的就是执行的最后一条命令的退出码。
示例#!/bin/bash# 函数部分Yes_or_no(){Echo “is your name $* ?”while truedoecho –n “Enter yes or no: ”read xcase “$x” iny | yes ) return 0;;n | no ) return 1;;*) echo “please input yes or no”esacdone#主程序部分echo “original parameters are $*”if yes_or_no “$1”thenecho “HI $1,nice name”elseecho “never mind”fiexit 0在函数内部定义的变量,在函数运行后,变量依然有效,如果只想在本函数内使用临时变量,可在变量前加上local#!/bin/bashfoo(){local str=aaaa #如果此处无local 则最后的语句是可以输出aaaa的echo "in foo $str"}fooecho "over $str"$# 表示参数个数,$1为第一个参数依次类推。
($*,$@)脚本中无函数的声明,调用某个函数前,该函数需要先被定义,所以函数的定义可以写在脚本的开始。
13、一些命令1)break 命令在控制条件为满足之前,可以跳出for,while,until循环,后面可带参数值,来表明要跳出的循环层数,但我们不建议这么做。
2) :命令冒号(:)命令是一个空命令。
偶尔被运用简化逻辑,相当于true。