当前位置:文档之家› Shell 经典实例

Shell 经典实例

Thizlinux 系统教程 Shell 经典实例----------------Milo经典小shell1 列目录树的shell脚本如下:#!/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中使用read (file是一个文件)cat file | while read linedoecho $lineecho " :: Please input any key(s):c"str4read=""while truedochr4read=`dd if=/dev/tty bs=1 count=1 2>/dev/null`str4read=$str4read$chr4readif [ "$chr4read" = "" ] ;then break; fidoneecho " :: |$str4read|"done3 将多个空格替换为字符sed 's/[ ][ ]*/ /g'如果空格与tab共存时用sed -e 's/[[:space:]][[:space:]]*/ /g' filename4用脚本实现分割文件#!/bin/bashif [ $# -ne 2 ]; thenecho 'Usage: split file size(in bytes)'exitfifile=$1size=$2if [ ! -f $file ]; thenecho "$file doesn't exist"exitfi#TODO: test if $size is a valid integerfilesize=`/bin/ls -l $file | awk '{print $5}'` echo filesize: $filesizelet pieces=$filesize/$sizelet remain=$filesize-$pieces*$sizeif [ $remain -gt 0 ]; thenlet pieces=$pieces+1fiecho pieces: $piecesi=0while [ $i -lt $pieces ];doecho split: $file.$i:dd if=$file of=$file.$i bs=$size count=1 skip=$i let i=$i+1doneecho "#!/bin/bash" > mergeecho "i=0" >> mergeecho "while [ $i -lt $pieces ];" >> mergeecho "do" >> mergeecho " echo merge: $file.$i" >> mergeecho " if [ ! -f $file.$i ]; then" >> mergeecho " echo merge: $file.$i missed" >> mergeecho " rm -f $file.merged" >> mergeecho " exit" >> mergeecho " fi" >> mergeecho " dd if=$file.$i of=$file.merged bs=$size count=1 seek=$i" >> merge echo " let i=$i+1" >> mergeecho "done" >> mergechmod u+x merge'5得到上月未日期,格式为YYYYMMDDget_lastday_of_lastmonth(){yy=`date +%Y`mm=`date +%m-1|bc`[ $mm -lt 1 ] && mm=12;yy=`expr $yy - 1`aaa=`cal $mm $yy`dd=`echo $aaa|awk '{print $NF}'`echo $yy$mm$dd}print $NF的$NF是打印最后一个列。

因为awk的内置变量NF是列的总数,而$NF就代表着最后一列6 实现用backup或tar命令来做目录备份需要保持两个目录当中的文件以及属组关系不变。

子目录结构不变,通过管道控制tar和backup命令,不需要中间的archive,(考虑到速度以及空间的关系)(cd /source && tar cf - .) |(cd /dest && tar zxfp -)偶没有 backup 命令,但是 tar 用管道可以,tar -cf - dir1 | ( cd dir2; tar -xvf - )搬移大法more aaa.sh#计算两个日期间有多少天#date1,date2:yyyymmdd#Usage:command date1 date2str=$1yy1=`echo $str|cut -c 1-4`mm1=`echo $str|cut -c 5-6`dd1=`echo $str|cut -c 7-8`str=$2yy2=`echo $str|cut -c 1-4`mm2=`echo $str|cut -c 5-6`dd2=`echo $str|cut -c 7-8`count_day=`expr $dd2 - $dd1`while [ $yy2 -ne $yy1 -o $mm2 -ne $mm1 ]domm2=`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`doneecho $count_day7 编写一个只允许用户执行telnet的shell为了监视用户网络操作行为,指定unxi主机给设备管理员登陆,然后用shell控制他。

只可以使用telnet命令,其他一概不许,包括cd,ls等。

就是一个用来远程登陆的管理平台。

我对shell不熟,请指导。

.profile中加入:read addrtelnet $addrexit8 判断文件的访问权限是不是600ls -l filename | awk '{ if($1 ~ "-rw-------") ..... }'ls -l filename | grep "^-rw------" -c#!/usr/bin/bash#showmod[ $# -eq 0 ] && { echo "Usage: $0 filelist ... "; exit ;} show(){{ [ -d $1 ] && ls -ld $1 ; [ -f $1 ] && ls -la $1 ;} | awk '{ umask=0umask_=""for(i=1;i<length($1);i++){if(substr($1,i+1,1)=="r")umask+=4;if(substr($1,i+1,1)=="w")umask+=2;if(substr($1,i+1,1)=="x")umask+=1;if(i%3==0){umask_=sprintf("%s%d",umask_,umask);umask=0;}}printf("%-20.20s: %-10.10s --> %s ",$9,$1,umask_);}';}for file_dir in $*doshow $file_dirdone9 算青蛙的脚本maxcount=$1;count=1;if [ $# -eq 1 ]thenwhile [ $count -le $maxcount ]do echo $count 只青蛙 $count 张嘴, `expr $count * 2`只眼睛,`expr $count * 4`条腿; count=`expr $count + 1`;done;elseecho "usage: sendos count"fi10 在SHELL程序中实现‘按任意键继续’在写一个SHELL程序,可是遇到了一个难题,在READ接受输入时,必须按回车键才能确认,而我需要只要按一个键就能得到用户的输入,无须按回车键!有什么好办法呢#!/bin/shget_char(){SAVEDSTTY=`stty -g`stty -echostty rawdd if=/dev/tty bs=1 count=1 2> /dev/nullstty -rawstty echostty $SAVEDSTTY}echo "Press any key to continue..."char=`get_char`如果你的机器上不认stty raw那么把函数中两处出现的raw换成cbreak。

11 在linux环境下启动时打开numlock想在系统启动时自动打开NumLock,可以在/etc/rc.d/rc.local中加入以下内容:for t in 1 2 3 4 5 6 7 8dosetleds +num$t>/dev/nulldone12 在shell里如何限制输入的长度举个例子,比如用户输入用户名时只能给他输入8个字符,如果超过了8个字符光标就停止在第八个字符那儿,不继续,一直等待回车只怕要自己来另写一个SHELL了。

相关主题