一般使用#!/bin/bash来解析shell语法,当然还有zsh、ksh等,但最常用的是bash1.变量-e参数:解析特殊字符在echo,如Linebreak:echo-e"Hello\nWorld"1.1、单引号'如果变量包含在单引号中,则变量不会被解析。$符号按原样输出。1.2.双引号"双引号会忽略大部分特殊字符,但不包括$、反引号、\所以不忽略$意味着shell可以替换双引号里面的变量名1.3.反引号`反引号要求shell执行它所包含的内容1.4、read请求输入,同时给多个变量赋值可以使用read命令一次性给多个变量赋值,read命令使用空格分隔每个单词-p参数:提示信息:promptread-p'PleaseenteraFruitandAnimal:'fruitanimalecho"fruitis$fruit,animalis$animal"-n参数:限制字符数n为数字的首字母-n限制用户输入的字符串的最大长度(字符数)read-p'Pleaseenteryourname(5charactersmax):'-n5nameecho-e"\nHello$name!"-t:limittheinputtimet是时间的第一个字母-t参数可以限制用户的输入时间(以秒为单位),超过这个时间,将不再读取任何输入#!/bin/bashread-p'请输入名字(你有5秒时间):'-t5nameecho-e"\nyournameis$name....!"-s:隐藏输入内容-s是secret的首字母#!/bin/bashread-p'请输入密码:'-spasswordecho-e"\npassword为$password....!"1.5.环境变量1.使用env命令查看shell文件中使用的所有环境变量environmentvariables#!/bin/bashecho"Javahomeis$JAVA_HOME"echo"shellenvis$SHELL"2.export:自定义环境variables3.参数变量我们可以这样用:./variable.shparameter1parameter2parameter3...variablemeaning$#numberofparameters$0isrun行的脚本名称$1第一个参数$2第二个参数$N第N个参数shift:shift#!/bin/bashecho"thisisafirstparam:$1"shiftecho"thisisafirstparam:$1"echo"paramsnumbersis:$#“4。算术运算:let#!/bin/bashleta="3+4"#a的平方letc="$a**2"echo"结果为:$c"5.array#!/bin/basharray=('apple','bonana','pear')array[3]="melon"echo"${array[2]}"echo"${array[3]}"#!/bin/basharray=('apple','bonana','pear')echo"${array[*]}"2.条件语句if[condition]thendothiselsedothatfiorif[condition];然后做这个fi或if[condition]then做这个elif[condition2]thendosomethingelse2elsedosomethingelsefidifferenttesttype1.teststringconditionmeaning$string1=$string2twostringsareequal,ShellCasesensitive$string1!=$string2两个字符串是否不相同-z字符串string是否为空,z为零的缩写-n字符串string是否为空,n为not#的缩写!/bin/bashname="Jack"if[$name="Jack"]thenecho"Hello$name!!!"fi#!/bin/bashname="Jack"if[$name="Jackson"]thenechoo"Hello$name!!!"elif[$name="Jacky"]thenecho"thisisJacky"elseecho"defaultisJack"fi#!/bin/bashif[-z$1]thenecho"NoParameter"elseecho"至少有一个参数!!!"fi2。检验数条件的含义$num1-eq$num2两个数是否相等$num1-ne$num2是否不等于$num1-lt$num2小于$num1-le$num2小于等于$num1-gt$num2大于$num1-ge$num2大于3测试文件条件含义-e$file存在,e是exist的缩写-d$file是目录,d是dir的缩写-f$file为文件,f为文件缩写-L$file为符号链接文件,L为链接缩写-r$file为可读,r->read-w$file是否可写,w->write-x$file文件是否可执行,x->executable$file1-nt$filefile1是否比file2新,nt->newerthan$file1-ot$file2file1是否比file2older,ot->olderthan多个测试条件&&||#!/bin/bashif[$#-ge1]&&[$1='love']然后echo"Great!"echo"你知道密码"elseecho"你不知道密码"ficase条件判断#!/bin/bashcase$1in"Jack")echo"HelloJack!!!";;“汤姆”)回声“你好汤姆!!!”;;"丽丽")echo"你好丽丽"#;;类似于Java中的break;;#*)类似于Java中的默认值*)echo"对不起,我不认识你!"esaclogic或|#!/bin/bashcase$1in"dog"|“猫”|“猪”)回声“这是一种动物!!!”;;“鸽子”|“燕子”)回声“这是一只鸟!!!”;;*)echo"我不知道它是什么";;esac3.循环语句3.1while循环while[条件测试]dodosomethingdoneorwhile[conditiontest];dodosomethingdone例子#!/bin/bashwhile[-z$response]||[$response!='yes']doread-p'Sayyes:'respondone3.2untilloopandwhile正好相反,until表示直到满足条件才结束循环#!/bin/bashuntil["$response"='yes']doread-p'Sayyes:'respondone3.3forloop#!/bin/bashforanimalin'dog''cat''pig'doecho"Animalis$animal"done循环遍历ls命令查询的文件#!/bin/bashforfilein`ls`doecho"Filefound:$file"完成循环以促进`ls-al|中dir的所有目录grep"^d"`doecho"Filefound:$dir-copy"done遍历所有以.sh结尾的文件#!/bin/bashforfilein`ls*.sh`doecho"Filefound:$file-copy"done3.4,seqsequence#!/bin/bash#其中2是`seq1210`doecho"$i"done4中i的步长。函数名后面的括号没有任何参数的函数的完整定义必须放在函数的调用之前/bashprint_something(){echoHello$1return100}print_somethingguoprint_somethingnanecho上一个函数的返回值为$?查看文件行数#!/bin/bashlines_in_file(){cat$1|wc-l}line_nums=$(lines_in_file$1)ech??o文件$1有$line_numslines4.1。变量作用域默认情况下,一个变量是全局的(global)。如果要top一个局部变量,需要使用local关键字#!/bin/bashlocal_global(){localvar1='local1'echo函数内部:var1是$var1,var2是:$var2var1='Changeagain'var2='2Changeagain'}var1='global1'var2='global2'echo函数调用前:var1为$var1,var2为$var2local_globalecho函数调用后:var1为$var1,var2为$var24。2、重载命令命令重载:就是取我们平时在命令行中使用的函数名command同名#!/bin/bashls(){commandls-lh}ls5、Shell实战练习#!/bin/bashif[-z$1]thenecho"Pleaseenterthefileofdir!"退出[!-e$1]thenecho"请确定目录下的文件存在!"exitfistatistics(){forcharin{a..z}do#>>tmp.txt输出到tmp.txt文件echo"$char-`grep-io"$char"$1|wc-l`"|tr/a-z//A-Z/>>tmp.txt完成#-rn(r:flashback,n:sortbynumber),#-k2(sortaccordingtowhichcolumns,sortaccordingtothesecondcolumn)#-t(指定分隔符)sort-rn-k2-t-tmp.txtrmtmp.txt}统计数据$1
