linux操作系统一个shell脚本_第1页
linux操作系统一个shell脚本_第2页
linux操作系统一个shell脚本_第3页
linux操作系统一个shell脚本_第4页
linux操作系统一个shell脚本_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、一个。为什么编Linuxsell灵活的工具。使用下面,一起来看看是如何工作的Linux 中有好多中不同的,但是通使用bash (bourne again )编程,因为 bash 一个。为什么编Linuxsell灵活的工具。使用下面,一起来看看是如何工作的Linux 中有好多中不同的,但是通使用bash (bourne again )编程,因为 bash 是免费的并且很容易使用。所以在本文中笔者所提都是使用bash(同样可以在 bash s使用任意一种文字编辑器,比如nedit、kedit、 等来编的程序用/bin/sh 来执行程序。使od+x./filename。地在其他编程语言中您必须使用变

2、量。在 编程中,所有的变量都由字符串组成,o# 现在打印变量aecho $a在您的编辑器中输入以上内容,然后将其保存为一个文件。之后执行。Aoo# 现在打印变量aecho $a在您的编辑器中输入以上内容,然后将其保存为一个文件。之后执行。Aoechothisisthe这并不会打印出thisisthe2nd,而仅仅打印thisisthe,因为s索变量numnd 的值,但是这个变量时没有值的。可以使用花括号来告诉s打印的是 num 变量:echothisisthethisisthe要。如果您需要处理数学表达式,那么您需要使用诸如expr等程序(见下面 S命令和流中可以使用三类命令:1)Unix 命

3、令echosometext: ls: wclfilewcwfilewccfile: cpsourcefiledestfile: mvoldnamenewnamermechosometext: ls: wclfilewcwfilewccfile: cpsourcefiledestfile: mvoldnamenewnamermfile: greppatternfile: 在文件内搜索字符串比如:grepsearchstringcutbcolnumfile: 比如:输出每行第 5 个到第 9 个字cut -b5-9 file.txt 千万不cat 命,令catfile.txt: 输出文件内容到标准

4、输出设备(屏幕)filesomefile: readvarsortfile.txt: 对file.txtuniq: sortfile.txt|expr: 进行算le:add2and3expr2+find: 搜索文件比如:根据文件名搜索findnamefilenametee: 将数据输出到标准输出设备(屏幕) | basename file: 返回不包含路径的文件名比如:basename ux 将返dirnamefile: 返回文件所在路径比如:dirnameuxheadfile: tailfile : sedSed是一个基本的查找替换程序。可以从标准输入(比如命令管道)本,并将结果输出到标准输

5、出(屏幕。该命令采用正则表达式(见参考)不要和 。比如:将 linuxfocus 替换为 LinuxFocus text.file|seds/linuxfocus/LinuxFocus/awk: awk -F指定其他分割符。catsedSed是一个基本的查找替换程序。可以从标准输入(比如命令管道)本,并将结果输出到标准输出(屏幕。该命令采用正则表达式(见参考)不要和 。比如:将 linuxfocus 替换为 LinuxFocus text.file|seds/linuxfocus/LinuxFocus/awk: awk -F指定其他分割符。catfile.txt|awkF$1$3AdamBor

6、, 2) 概念: 管道, (|) grepofile.txt|wc-在中搜索包含有o”的行并计算其行数。在这里grep命令的输出作为wc find.-mtime-1-typef-:# Theticksarebackticks()notnormal quotes(): tar-zcvflastmod.tar.gzfind.-mtime-1-typef-pr3) if 表达式 如果条件为真则执行then;通常用来表示条件测试。注意这里的空格很重要。要确保方括号的空格。 -f somefile :判断是否是一个文件xbin/ls:判断/bin/lsn$var :判断$var$a$b :判断$a和$b

7、执行mantestif $S:=/bin/bash;echoyourlogins echo通常用来表示条件测试。注意这里的空格很重要。要确保方括号的空格。 -f somefile :判断是否是一个文件xbin/ls:判断/bin/lsn$var :判断$var$a$b :判断$a和$b执行mantestif $S:=/bin/bash;echoyourlogins echoyourloginisthebash(bourneagainisnotbashbut变量包含了的名称和/bin/bash 进行了比较熟悉C-f/etc/shadow&echoThiscomputer uses这里 & 就是一

8、个快捷操作符,如果左边的表达式为真则执行右边的语句。您也computerusesshadosswors”。同样或操作(|)在 -r $ echo$folderechoCannotread folder;exit1;foldergrepFrom 该须也可以用 if 表达式作任何事情,但是使用与或操作符 case.)dosomethinghere; .)dosomethinghere; filelf.gz:gzipcompresseddata,deflated,originalfilename, last modified: Mon Aug 27 23:09:18 2001, os: Unix z

9、ip类型的压缩文件: ftype=file $1 case $ftype in $1Ziparchive*)unzip$1$1:gzipcompressed*) gunzip $1 ;$1:bzip2compressed*) bunzip2 $1 ;bzip2,和*)errorFile$1cannotbe pressedwithsmartzip$1 selectbash的扩展应用,尤其擅长于交互式使用。用户可以从一selectvarin.;do .now $varcanbeused echoWhatisyourfavouriteselectvarinLinuxGnuHurdFreeBSDOth

10、er;do echoYouhaveselectedWhatisyourfavouriteGnuFree#? 1Youhaveselected您也可s whiledo中使用如下#? 1Youhaveselected您也可s whiledo中使用如下的loop 表达式while-loop将运行直到表达式测试为真。willrunwhilethettestfor istrue. 关键字break 用来跳出循环。而关键字”continue”) forvar;在下面的例子中,将分别打印ABCforvarinABC;do echovar is下面是一个更为有用showrpm,其功能是打印一些RPM包的统计信

11、息#listacontentsummaryofanumberofRPMpackages # USAGE: showrpm rpmfile1 rpmfile2 .#forrpmpackagein$*;if-r$rpmpackageecho=$rpmpackage= rpm -qi -p $rpmpackageechoERROR:cannotreadfile行showrpmopenssh.rpmw3m.rpm$* 包含了 3 个字符串,即openssh.rpm,w3m.rpmand把通配符(比如*).jpg 和tux.jpg这将打印出(单引号和双引号) echo *.jpg这将打印*.jpg ec

12、ho $Secho把通配符(比如*).jpg 和tux.jpg这将打印出(单引号和双引号) echo *.jpg这将打印*.jpg echo $Sechoecho*.jpg echo$Ss 以 开头,后面接上一个字符串,这个字符串还必须出现在 s 使用#wehaven3arguments.thehelpif$#-lt3;then cat shiftby-)shift;break;#endof-*)echoerror:nosuchoption$1.-hforhelp;exitechoopt_fis$opt_f echoopt_l isechoargisecho2nd argiscmdparser

13、-o-f-somefile1opt_fisopt_l oargis-somefile1 2nd arg is somefile2来(famework.sh,来(famework.sh,cpframework.sh时让(#vim:setsw=4ts=4et: catb2h-convertbinarytoUSAGE:b2h-hOPTIONS:-hhelpLE:b2hexit#anerrorandecho$1 exit 1#returnthelastcharacter#returnthelastcharacterofastringin$rval if -z $1 ; then#wcputssomes

14、pacebehindtheoutputthisiswhyweneedsed: numofchar=echo -n $1 | wc -c | sed s/ /g # now cutoutthelastrval=echo-n$1|cut-b#removethelastcharacterinstringandreturnitin$rval if -z $1 ; then#wcputssomespacebehindtheoutputthisiswhyweneedsed: numofchar=echo -n $1 | wc -c | sed s/ /g if$numofchar=1;then # onl

15、y one char in string numofcharminus1=expr$numofchar-1 # now cut all but the last char:rval=echo-n$1|cut-b0-while-n$1;do case $1 in-h)help;shift1;#functionhelpis-)shift;break;# endof-*)errorerror:nosuchoption$1.-hfor#Themain#oneargmustbegiven: -z $1 & help while-n$binnum;do lastchar $binnumif$rval= 1

16、; sum=expr$weight+#removethelast chop $binnum ition#oneargmustbegiven: -z $1 & help while-n$binnum;do lastchar $binnumif$rval= 1; sum=expr$weight+#removethelast chop $binnum itioninweight=expr$weight*echobinary$binnumorigisdecimal$sum 该0 * 1 + 1 * 2 = 2是用了lastchar函数。该函数使用wcc计算字符 月以后,这个文件可能会变得很大以至于使对

17、该文件的速度变慢。下面的efile )为out .1,而对于.1 就变成了.2 等等等等#vim:setsw=4ts=4et: catefile-ethefileUSAGE:efile-hOPTIONS:-hhelpLE:efileThiswille.grenameout.2toout.3,out.1toout.2,outtoandcreateanemptyout-Themaxnumber andcreateanemptyout-Themaxnumber isexitexit 1while-n$1;do case $1 in-h)help;shift-)-*)echoerror:nosuchoption$1.-hforhelp;exit# if-z$1;errorERROR:youmustspecifyafile,use-hforhelp #renameany.1,.2etcfile: for nin987654321; if-f$filen.$n;then p=expr $n + 1echomv$filen.$n$filen.$p mv $filen.$n $f

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论