linuxshell编程.doc_第1页
linuxshell编程.doc_第2页
linuxshell编程.doc_第3页
linuxshell编程.doc_第4页
linuxshell编程.doc_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

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

文档简介

shellLinuxshellShellshellshellshellLinuxshellbash (bourne again shell) shellbashbashbashbourne shellneditkeditemacsvishell:#!/bin/sh#!/bin/sh:chmod +x filename ./filename shell# shell:=$:#!/bin/sh#a=hello world# aecho A is: $afirst chmod +x first ./first :A is: hello world:num=2echo this is the $numndthis is the 2ndthis is the shellnumndshellnum:num=2echo this is the $numnd this is the 2nd expr shellexportShellshell1) Unix :shellunixecho some text: ls: wc Cl file wc -w file wc -c file: linewordcharactercp sourcefile destfile: mv oldname newname : rm file: grep pattern file: cut -b column file: 59 cut -b5-9 file.txtcatcat file.txt: file somefile: somefileread var: varsort file.txt: file.txtuniq: sort file.txt | uniqexpr: 2+3: expr 2 + 3find: find . -name filename -printtee: () somecommand | tee outfilebasename file: basename /bin/tux tuxdirname file: dirname /bin/tux /binhead file: tail file : sed: Sedshell Ubuntu Ubuntu cat text.file | sed s/ubuntu/Ubuntu/ newtext.fileawk: awk -F cat file.txt | awk -F, print $1 , $3 , Adam Bor, 34, IndiaKerry Miller, 22, USAAdam Bor, IndiaKerry Miller, USA2) : , backtick (|) :grep hello file.txt | wc -lfile.txthellogrepwc: :find . -mtime -1 -type f -print24-mtime C248:#!/bin/sh# The ticks are backticks () not normal quotes ():tar -zcvf lastmod.tar.gz find . -mtime -1 -type f -print3) if then:if .; then .elif .; then .else .fi : -f somefile -x /bin/ls /bin/ls -n $var $var $a = $b $a$bman test:#!/bin/shif $SHELL = /bin/bash ; thenecho your login shell is the bash (bourne again shell)elseecho your login shell is not bash but $SHELLfi$SHELLshell/bin/bashC: -f /etc/shadow & echo This computer uses shadow passwors & /etc/shadow This computer uses shadow passwors(|)shell:#!/bin/shmailfolder=/var/spool/mail/james -r $mailfolder | echo Can not read $mailfolder ; exit 1; echo $mailfolder has mail from:grep From $mailfoldermailfolderFrom -ifcase:case . in.) do something here ;esacfilefile lf.gz:lf.gz: gzip compressed data, deflated, original filename,last modified: Mon Aug 27 23:09:18 2001, os: Unixsmartzipbzip2, gzip zip #!/bin/shftype=file $1case $ftype in$1: Zip archive*)unzip $1 ;$1: gzip compressed*)gunzip $1 ;$1: bzip2 compressed*)bunzip2 $1 ;*) error File $1 can not be uncompressed with smartzip;esac$1smartzip articles.zip$1 articles.zipselect bashselect var in . ; dobreakdone. now $var can be used .#!/bin/shecho What is your favourite OS?select var in Linux Gnu Hurd Free BSD Other; dobreakdoneecho You have selected $varWhat is your favourite OS?1) Linux2) Gnu Hurd3) Free BSD4) Other#? 1You have selected Linuxshellloopwhile .; do.donewhile-loop will run while the expression that we test for is true. break continuefor-loop () for var in .; do.doneABC#!/bin/shfor var in A B C ; doecho var is $vardoneshowrpmRPM#!/bin/sh# list a content summary of a number of RPM packages# USAGE: showrpm rpmfile1 rpmfile2 .# EXAMPLE: showrpm /cdrom/RedHat/RPMS/*.rpmfor rpmpackage in $*; doif -r $rpmpackage ;thenecho = $rpmpackage =rpm -qi -p $rpmpackageelseecho ERROR: cannot read file $rpmpackagefidone$*showrpm openssh.rpm w3m.rpm webgrep.rpm $* 3 openssh.rpm, w3m.rpm and webgrep.rpm.*jpg mail.jpg tux.jpg#!/bin/shecho *.jpgmail.jpg tux.jpg () #!/bin/shecho *.jpgecho *.jpg*.jpg #!/bin/shecho $SHELLecho $SHELLecho $SHELL/bin/bash/bin/bash$SHELLecho *.jpgecho $SHELL*.jpg$SHELLHere documentshere documents here documentsecho Here document here documenthere documents#!/bin/sh# we have less than 3 arguments. Print the help text:if $# -lt 3 ; thencat ren - renames a number of files using sed regular expressionsUSAGE: ren regexp replacement files.EXAMPLE: rename all *.HTM files in *.html:ren HTM$ html *.HTMHELPexit 0fiOLD=$1NEW=$2# The shift command removes one argument from the list of# command line arguments.shiftshift# $* contains now all the files:for file in $*; doif -f $file ; thennewfile=echo $file | sed s/$OLD/$NEW/gif -f $newfile ; thenecho ERROR: $newfile exists alreadyelseecho renaming $file to $newfile .mv $file $newfilefifidoneif3 ($# ) 3catcat3OLDNEWshift$*$filesednewfilemvfunctionname()# inside the body $1 is the first argument given to the function# $2 the second .bodyxtitlebarhelp#!/bin/sh# vim: set sw=4 ts=4 et:help()cat xtitlebar - change the name of an xterm, gnome-terminal or kde konsoleUSAGE: xtitlebar -h string_for_titelbarOPTIONS: -h help textEXAMPLE: xtitlebar cvsHELPexit 0# in case of error or if -h is given we call the function help: -z $1 & help $1 = -h & help# send the escape sequence to change the xterm titelbar:echo -e 330;$107#$* $1, $2 . $9 -h ()case#!/bin/shhelp()cat shift by 2-) shift;break; # end of options-*) echo error: no such option $1. -h for help;exit 1;*) break;esacdoneecho opt_f is $opt_fecho opt_l is $opt_lecho first arg is $1echo 2nd arg is $2cmdparser -l hello -f - -somefile1 somefile2opt_f is 1opt_l is hellofirst arg is -somefile12nd arg is somefile2caseunixframework.shcopycp framework.sh myscript b2d ( 1101) expr#!/bin/sh# vim: set sw=4 ts=4 et:help()cat b2h - convert binary to decimalUSAGE: b2h -h binarynumOPTIONS: -h help textEXAMPLE: b2h 111010will return 58HELPexit 0error()# print an error and exitecho $1exit 1lastchar()# return the last character of a string in $rvalif -z $1 ; then# empty stringrval=returnfi# wc puts some space behind the output this is why we need sed:numofchar=echo -n $1 | wc -c | sed s/ /g # now cut out the last charrval=echo -n $1 | cut -b $numofcharchop()# remove the last character in string and return it in $rvalif -z $1 ; then# empty stringrval=returnfi# wc puts some space behind the output this is why we need sed:numofchar=echo -n $1 | wc -c | sed s/ /g if $numofchar = 1 ; then# only one char in stringrval=returnfinumofcharminus1=expr $numofchar - 1# now cut all but the last char:rval=echo -n $1 | cut -b 0-$numofcharminus1while -n $1 ; docase $1 in-h) help;shift 1; # function help is called-) shift;break; # end of options-*) error error: no such option $1. -h for help;*) break;esacdone# The main programsum=0weight=1# one arg must be given: -z $1 & helpbinnum=$1binnumorig=$1while -n $binnum ; dolastchar $binnumif $rval = 1 ; thensum=expr $weight + $sumfi# remove the last position in $binnumchop $binnumbinnum=$rvalweight=expr $weight * 2doneecho binary $binnumorig is decimal $sum# (1,2,4,8,16,.)100 * 1 + 1 * 2 = 2lastchar wc CccutChoprotatefile outmailoutmail.1outmail.1outmail.2 .#!/bin/sh# vim: set sw=4 ts=4 et:ver=0.1help()cat rotatefile - rotate the file nameUSAGE: rotatefile -h filenameOPTIONS: -h help textEXAMPLE: rotatefile outThis will e.g rename out.2 to out.3, out.1 to out.2, out to out.1and create an empty out-fileThe max number is 10version $verHELPexit 0error()echo $1exit 1while -n $1 ; docase $1 in-h) help;shift 1;-) break;-*) echo error: no such option $1. -h for help;exit 1;*) break;esacdone# input check:if -z $1 ; thenerror ERROR: you must

温馨提示

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

评论

0/150

提交评论