2022年Shell经典实例 .pdf

上传人:C****o 文档编号:32492640 上传时间:2022-08-09 格式:PDF 页数:20 大小:130.35KB
返回 下载 相关 举报
2022年Shell经典实例 .pdf_第1页
第1页 / 共20页
2022年Shell经典实例 .pdf_第2页
第2页 / 共20页
点击查看更多>>
资源描述

《2022年Shell经典实例 .pdf》由会员分享,可在线阅读,更多相关《2022年Shell经典实例 .pdf(20页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、Shell ?- ? y.txt21o?a?1?- 1y? ? Do?1?D?D? o ? ?1?y?3? D? ? D?a?2?Thizlinux ? 3?3 Shell ?-? y -Milo ?- ?D?shell 1 D? ?shell ?o ?#!/bin/sh ?# dtree: Usage: dtree any directory ?dir=$1:-. ?(cd $dir; pwd) find $dir -type d -print | sort -f | sed -e s,$1, -e /$/d -e s,/*/(/*)$,-1, -e s,/*/,| ,g 2 while?D

2、 1?read (file?t) cat file | while read line do echo $line echo : Please input any key(s):c str4read= while true do chr4read=dd if=/dev/tty bs=1 count=1 2/dev/null str4read=$str4read$chr4read 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 20 页 - - - - - - - - -

3、if $chr4read = ;then break; fi done echo : |$str4read| done 3 ? ?a? sed s/ */ /g ?1?tab12 ? ? sed -e s/:space:space:*/ /g filename 4? ?t #!/bin/bash if $# -ne 2 ; then echo Usage: split file size(in bytes) exit fi file=$1 size=$2 if ! -f $file ; then echo $file doesnt exist exit 名师资料总结 - - -精品资料欢迎下载

4、 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 20 页 - - - - - - - - - fi #TODO: test if $size is a valid integer filesize=/bin/ls -l $file | awk print $5 echo filesize: $filesize let pieces=$filesize/$size let remain=$filesize-$pieces*$size if $remain -gt 0 ; then let pieces=$piec

5、es+1 fi echo pieces: $pieces i=0 while $i -lt $pieces ; do echo split: $file.$i: dd if=$file of=$file.$i bs=$size count=1 skip=$i let i=$i+1 done 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 20 页 - - - - - - - - - echo #!/bin/bash merge echo i=0 merge echo wh

6、ile $i -lt $pieces ; merge echo do merge echo echo merge: $file.$i merge echo if ! -f $file.$i ; then merge echo echo merge: $file.$i missed merge echo rm -f $file.merged merge echo exit merge echo fi merge echo dd if=$file.$i of=$file.merged bs=$size count=1 seek=$i merge echo let i=$i+1 merge echo

7、 done merge chmod u+x merge 5? ? ?aYYYYMMDD get_lastday_of_lastmonth() yy=date +%Y mm=date +%m-1|bc 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 20 页 - - - - - - - - - $mm -lt 1 & mm=12;yy=expr $yy - 1 aaa=cal $mm $yy dd=echo $aaa|awk print $NF echo $yy$mm$dd

8、 print $NF ?$NF? ?o ?D? ?aawk?NF ?D? y?$NF? ?o ?D 6 ?backup?tar? ? ?Y D a 3? ?D?t ? ? 1?2? ?112? 1y1 ?taro backup? ?2?D a?D?archive ? ? ?1? ? (cd /source & tar cf - .) |(cd /dest & tar zxfp -) ?D backup ? ? tar ?1 ? ? tar -cf - dir1 | ( cd dir2; tar -xvf - ) ? more aaa.sh #?D? #date1,date2:yyyymmdd

9、#Usage:command date1 date2 str=$1 yy1=echo $str|cut -c 1-4 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 20 页 - - - - - - - - - mm1=echo $str|cut -c 5-6 dd1=echo $str|cut -c 7-8 str=$2 yy2=echo $str|cut -c 1-4 mm2=echo $str|cut -c 5-6 dd2=echo $str|cut -c 7-8

10、count_day=expr $dd2 - $dd1 while $yy2 -ne $yy1 -o $mm2 -ne $mm1 do mm2=expr $mm2 - 1 $mm2 -eq 0 & mm2=12 & yy2=expr $yy2 - 1 aaa=cal $mm2 $yy2 bbb=echo $aaa|awk print $NF count_day=expr $count_day + $bbb done echo $count_day 7 D ?D ?DDtelnet ?shell ?a? ? ?2 DD?a ?unxi? ? ?1 ? ?o ?shell? ? ?1?telnet?

11、 ?2?D ? cd?ls ? ? ?3 ?1 ? ?shell2? ? .profile?D? ?o read addr telnet $addr 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 20 页 - - - - - - - - - exit 8 ?D?t ? ?T?2?600 ls -l filename | awk if($1 -rw-) . ls -l filename | grep -rw- -c #!/usr/bin/bash #showmod $#

12、-eq 0 & echo Usage: $0 filelist . ; exit ; show() -d $1 & ls -ld $1 ; -f $1 & ls -la $1 ; | awk umask=0 umask_= for(i=1;i %s ,$9,$1,umask_); ; for file_dir in $* do show $file_dir done 9 ? ? maxcount=$1; count=1; if $# -eq 1 then while $count -le $maxcount do echo $count ? $count ? , expr $count * 2

13、?|,expr $count * 4 ? ; count=expr $count + 1; done; else 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 20 页 - - - - - - - - - echo usage: sendos count fi 10 ? SHELL3 D?D ? ?a?D? ?D ?SHELL3 D ? ?a?READ? ? ?D? ?3?2? ?D a?a ? ? ?TD? ?3?D2?o? ? #!/bin/sh get_char(

14、) SAVEDSTTY=stty -g stty -echo stty raw dd if=/dev/tty bs=1 count=1 2 /dev/null stty -raw stty echo stty $SAVEDSTTY echo Press any key to continue. char=get_char ?1? ?2?stty raw? ?o y?D?|3? ?raw?3cbreak? 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 20 页 - - -

15、 - - - - - - 11 ? linux? ?3? ?anumlock ? 3? ? ?aNumLock ? ?/etc/rc.d/rc.local?D? ? Yo ?for t in 1 2 3 4 5 6 7 8 ?do ? setleds +num ? $t/dev/null ?done 12 ? shell ?o?T?3? ?y ? ? ? ?8?1?3?1y?8?1a ? ?1? ? ?2?D? y?3?a?o D?SHELL ? ? ? ?1?3?3?1a ? ? ?o ? ?D?D? 1?y?D?y ? ? ?32?3?1y8? ?6?o?3 ?2? ? a? 8? ?32

16、? ? ? ?t?: input.sh ? oinput.sh ?T?3? yoinput.sh 8 ?T?8? #?/bin/ksh # Program Name : input.sh trap 1 2 3 5 7 9 15 13 getcon() stty raw -echo conchar=dd if=/dev/tty bs=1 count=1 2/dev/null 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 20 页 - - - - - - - - - st

17、ty sane echo $conchar while i=0;clear /dev/tty echo 13 ? (? ?) ?¥ ?Y? ? ao ?1?D? LINUXt? ?t? ?D?2? ?Y? a?e?a? DMySQL y?Y?a? ? 2a?a?MySQL ?Y?a? ? ?o ?host ? ? ? D?Y?Y?ship? ? ?host? ? ?Y? o ? 1yshell? ?1?a?Y?t? ?o 1yftp2 ? 3?a? ? ?root 1ycrontab ? ? ?DD? ? host ? ?3?DD? ? ship ? ?6?DD? a?o ? t? ? 3 ?

18、o?2?2?a3?1y1D? ? ? ?Y6?DD ?Y? ?2? ?DT?3? ?Y? 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 20 页 - - - - - - - - - ? ?2? ?DT?3 ?Y? ?o ?ahost? ? #/host?/ #! /bin/sh HOST= USER=backup_use #ship?backup_use ? PASSWORD=backup_use #ship? ?| ? BACKUPDIR=/home/backup_

19、use/ship #host?backup_use ?BACKUPDIR2=/home/backup_use/host #host?backup_use ?MYSQLPASS=abc #ship ? ?MySQL root ? PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/nusphere/MySQL/bin DOW=date +%a DM=date +%Y%b%d FILE1=ship-virtual-$DM.tar.gz FILE2=ship-MySQLdata-$DM.tar.gz FILE3=ship-szeasy-$DM.tar.gz FI

20、LE4=ship-other-$DM.tar.gz # FIL1=$BACKUPDIR2/host-main-$DM.tar.gz 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 12 页,共 20 页 - - - - - - - - - FIL2=$BACKUPDIR2/host-MySQLdata-$DM.tar.gz FIL3=$BACKUPDIR2/host-other-$DM.tar.gz # DIRECTORIE1=/www /home/jjd # a?Y?1 DIREC

21、TORIE2=/etc /var/named /usr/local/nusphere/apache/conf # a?Y?2 # if $DOW = Sat ; then # ?Y ?Y tar -zcpf $FIL1 $DIRECTORIE1 tar -zcpf $FIL3 $DIRECTORIE2 MySQLdump -all-databases -q -uroot -p$MYSQLPASS |gzip $FIL2 ftp -i -n tempfile1 sed s/a/ tempfile1 tempfile2 awk printf(mv a%s a%04s , $0, $0) tempf

22、ile2 tempfile3chmod 700 tempfile3 ./tempfile3 rm tempfile* $chmod test 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 13 页,共 20 页 - - - - - - - - - -ls a*|awk num=substr($1,2,length($1)-1); printf mv %s a%04d ,$1,num rename.sh sh rename.sh -for file in a* do newfile=

23、echo $file | awk printf a%04d, substr($1, 2, length($1)-1) mv $file $newfile done ?2?awk?D? ? ls -1 a*|while read j do num=echo $j|cut -b 2- num=printf a%04s $num mv $j $num 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 14 页,共 20 页 - - - - - - - - - done -?o for fil

24、e in a* do mv $file echo $file|awk printf a%04d, substr($1,2,length($1)-1) done ?1?a1,a2,a3. ?t ? ?D2?1?a1.pxx,a2,baa,a3.tga. ?a? ?2? ?o for file in a* do nam=echo $file|cut -d. -f1 exe=echo $file|cut -d. -f2 mv $file echo $nam|awk printf a%04d, substr($1,2,length($1)-1).$exe done 15 ?1 ?IP?shell 名师

25、资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 15 页,共 20 页 - - - - - - - - - ?a?0o ?shell ? ?a?1?Dtcpwrapper ? 3,? ?/etc/hosts.deny ?a?1o ?/etc/profile? ?shell? ?(sco openserver) ?a?D ?a? ?C?shell,? ?. who=who am i|awk print $1 myterm=who am i|awk print substr($2,4,2)

26、if x$who = xroot then subnet=finger|grep $myterm|awk print substr($8,1,8)else subnet=finger|grep $myterm|awk print substr($7,1,8) fi test x$subnet != x & test x$who != xroot & test x$subnet != x46.8.44. & echo Please login from local network & exit 16 eval? yy # shell:/bin/sh # #y?: #? ?o?t ?, ?o?3

27、? 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 16 页,共 20 页 - - - - - - - - - v1=aaa v2=bbb c=1 if $c -eq 1 then vname=v$c #? ?o?t ?av1 eval vvv=$vname ; echo vvv: $vvv #?v1?3 vvv,? , 1vvv=aaaeval vvv=$vname ; echo vvv: $vvv #?v1?3 vvv,? , 1vvv=aaa#eval vvv=$vname ;

28、 echo vvv: $vvv # ? ? fi #y?t:# ?v1?aaa ?a?,? ?vaaa?3 ?a?D? ?aaa v1=aaa ; vaaa=This is aaa #eval $v1=$vaaa ; echo aaa: $aaa # ? ? #eval $v1=$vaaa ; echo aaa: $aaa # ? ? eval $v1=$vaaa ; echo aaa: $aaa #yy: #?v1?aaa ?a?,2 ? ?a?3 ? v1=aaa ; vaaa=This is aaa eval $v1=$v1 ; echo aaa: $aaa #?y?t ? ? ? 2?

29、,?a ? ?y?eval $v1=$v1 ; echo aaa: $aaa #?y?t ? ? ? 2?,?a ? ?y?名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 17 页,共 20 页 - - - - - - - - - #?e? ?y?t?D?vaaa?D?D? eval $v1=$v1 ; echo aaa: $aaa 17 ? ?UNIX? a. ?echo 31m2a?37m echo Xm YourChar (X=30,31.36?) ? a?a?a? D? ?3

30、0m echo ;m a?D?a? ?D?a; ? ?o ? echo 31;5m2a? b. ? ? 3? ? ? - 30 40 o ? 31 41 ?t? 32 42 ?G ? 33 43 S? 34 44 ? 35 45 ?t ? 36 46 ? ? ? 37 47 ? 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 18 页,共 20 页 - - - - - - - - - ? a? - 0 OFF 1 ? ? 4 underline 5 ? 7 ? 8 2? ? c. 2

31、 ?(o ?3? ? ?) o sco: setcolor red; echo abcd; setcolor white linux/BSD: /usr/bin/echo -e 18 ? ?bash ? export PS1=uh W$ ?e?/.bashrco/.bash_profile 19 grep/awk/sed? ?t2 ? # cat gt one two three four five six one two 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 19 页,共

32、 20 页 - - - - - - - - - one seven three # grep one three gt grep: three: No such file or directory gt:one two three gt:one two gt:one seven three ? 3?oneothree ?DD ?1?|? ?o one two three one seven three a. ?grep cat gt | grep one | grep three b. ?awk awk /one/&/three/ gt c. ?sed sed -ne /one/three/p gt ? 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 20 页,共 20 页 - - - - - - - - -

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 高考资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁