LinuxSir.Org  
| 网站首页 | 论坛帮助 |

欢迎来到LinuxSir.Org!
您还未登录,请登录后查看论坛,或者点击论坛上方的注册链接注册新账号。


发表新主题 回复
置顶的主题 精华主题  
主题工具
旧 03-04-21, 12:30 第 16 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

标题: 一段shell脚本[转自:UNIX中文社区,特此感谢]


jhpm()
{
echo " [21;08H 若屏幕正常而激活它, 将中断该屏幕所有进程 输入 y 继续 [ ] "
echo " [21;68H\c"
read jx
if [ $jx ] && [ $jx = "y" ]
then
echo " [21;06H┃ 请输屏幕号(按Alt+F1进入第一屏,屏幕号为01,以此类推)--> [ ] ┃"
echo " [21;68H\c"
read PMH
while [ $PMH ]
do
PID=`ps -t tty$PMH | awk 'BEGIN {ORS=" "} NR>1 {print $1}'`
kill -9 $PID;sleep 2
echo " [21;06H┃ 该屏幕已被激活 ! \07\c ┃"
sleep 2
return
done
else return 0
fi
}
  KornLee 当前离线   回复时引用此帖
旧 03-04-26, 22:36 第 17 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

标题: 如何停止终端多个进程[转自:Linuxaid.com特此感谢]


echo "系统当前用户"
echo "---------------"
who | awk '{print $2}'
echo "---------------"
echo "输入要杀死终端的终端号:"
read $TTY
kill -9 ${K}=`ps -t $TTY | grep [0-9] | awk '{print $1}'`

此帖于 03-04-26 22:39 被 KornLee 编辑.
  KornLee 当前离线   回复时引用此帖
旧 03-04-26, 22:38 第 18 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

标题: 如何查找日期为某一天的文件[转自:Linuxaid.com特此感谢]


#!/bin/sh
# The right of usage, distribution and modification is here by granted by the author.
# The author deny any responsibilities and liabilities related to the code.
#
OK=0
A=`find $1 -print`
if expr $3 == 1 >/dev/null ; then M=Jan ; OK=1 ; fi
if expr $3 == 2 >/dev/null ; then M=Feb ; OK=1 ; fi
if expr $3 == 3 >/dev/null ; then M=Mar ; OK=1 ; fi
if expr $3 == 4 >/dev/null ; then M=Apr ; OK=1 ; fi
if expr $3 == 5 >/dev/null ; then M=May ; OK=1 ; fi
if expr $3 == 6 >/dev/null ; then M=Jun ; OK=1 ; fi
if expr $3 == 7 >/dev/null ; then M=Jul ; OK=1 ; fi
if expr $3 == 8 >/dev/null ; then M=Aug ; OK=1 ; fi
if expr $3 == 9 >/dev/null ; then M=Sep ; OK=1 ; fi
if expr $3 == 10 >/dev/null ; then M=Oct ; OK=1 ; fi
if expr $3 == 11 >/dev/null ; then M=Nov ; OK=1 ; fi
if expr $3 == 12 >/dev/null ; then M=Dec ; OK=1 ; fi
if expr $3 == 1 >/dev/null ; then M=Jan ; OK=1 ; fi
if expr $OK == 1 > /dev/null ; then
ls -l --full-time $A 2>/dev/null | grep "$M $4" | grep $2 ;
else
echo Usage: $0 path Year Month Day;
echo Example: $0 ~ 1998 6 30;
fi

此帖于 03-04-26 22:45 被 KornLee 编辑.
  KornLee 当前离线   回复时引用此帖
旧 03-04-26, 22:42 第 19 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

标题: 如何列出目录树 [转自:linuxaid 特此感谢]


下面的短小的shell程序可以列出目录树, 充分利用了sed强大的模式匹配能力.

脚本如下:
#!/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"

注:此脚本有点小问题,希望大家还是自己实践一下吧~

此帖于 03-04-26 23:09 被 KornLee 编辑.
  KornLee 当前离线   回复时引用此帖
旧 03-04-27, 20:51 第 20 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

标题: 列出目录树的脚本


特别感谢作者:LYOO兄
代码:
#!/bin/bash redir () { #tab是真正的步长计算器 tab=$tab$singletab line=${tab%"$singletab"}"|-------" #local比较关键,它规定了count是当前的参数列表值 local count=$# for file in "$@"; do thisfile=${thisfile:-$PWD}/$file #判断当前文件是否为目录,如果是就开始递归 if [ -d "$thisfile" ]; then #如果当前目录是分枝列表的最底层,则需进行特殊处理。 if [ $count -eq 1 ]; then echo -e $line$file/ #将前一个|符号去掉,看看目录树就知道为什么了。 tab=${tab%"$singletab"}"\t" redir $(ls $thisfile) else echo -e $line$file/ redir $(ls $thisfile) fi else echo -e $line$file fi thisfile=${thisfile%/*} let count=count-1 done #这一步比较有意思,因为从递归出来的tab结尾可能是TAB也可能是$singletab,所以分成两步来去掉。 tab=${tab%"\t"} tab=${tab%"|"} line=${tab%"$singletab"}"|-------" } singletab="|\t" userinput="$@" if ls $userinput; then for file in ${userinput:-.}; do echo $file echo '|' if [ -d "$file" ]; then cd $file redir $(ls) cd .. fi done else echo "$userinput is wrong" fi
  KornLee 当前离线   回复时引用此帖
旧 03-04-28, 18:10 第 21 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

标题: 自动挂载WINDOWS下的分区的脚本


特别感谢作者:ywchen2000兄

Linux开机自动挂载WINDOWS下的分区
笔者有两快硬盘,其中第一块硬盘装了WindowsMe,第二快装了Win2000和red hat
linux7.2,在WINDOWS环境中,笔者划分了四个分区,每次在LINUX环境中要想看这四个区的内
容都要输入一大堆的命令,非常麻烦.近来学习了SHELL编程,想到了一个解决方法.现在写出
来与大家分享.
首先,打开一个虚拟终端,输入vi mymount,然后按insert键,输入以下代码:
#!/bin/bash
case $1 in
m)
mount -o iocharset=cp936 -t vfat /dev/hda5 /mnt/winme
mount -o iocharset=cp936 -t vfat /dev/hda6 /mnt/temp
mount -o iocharset=cp936 -t vfat /dev/hdc1 /mnt/win2000
mount -o iocharset=cp936 -t vfat /dev/hdc2 /mnt/share
;;
u)
umount -o iocharset=cp936 -t vfat /dev/hda5 /mnt/winme
umount -o iocharset=cp936 -t vfat /dev/hda6 /mnt/temp
umount -o iocharset=cp936 -t vfat /dev/hdc1 /mnt/win2000
umount -o iocharset=cp936 -t vfat /dev/hdc2 /mnt/share
;;
esac
按Esc输入:wq回车就可以了.
现在让我来解释一下这些代码.第一行#!/bin/bash指定以bash shell执行此文
件.case $1 in 为取的命令行参数.若为m则开始挂载,若为u则卸载.其中-o
iocharset=cp936能够显示中文名.-t vfat 为指定文件系统类型为WINDOWS下的VFAT文件系
统.winme temp win2000和share为目录/mnt下的子目录.

好了,该程序已经写好了.但是它还没有执行权利.我们只要输入下面这个命令就可以了.
chmod u+x mymount
呵呵,到这步你只要输入./mymount m,就可以挂载了.不过笔者比较懒,觉得这样做比较麻
烦.于是就请教了一些高手,终于解决了这个问题呀.呵呵.在/etc/rc.d/rc.local 文件中输
入 sh ./root/mymount m就可以了.
重新启动你的电脑,进入LINUX看看是否自动挂载.呵呵.笔者以后就非常轻松,不需要输入
那么多的命令了.爽呀.
  KornLee 当前离线   回复时引用此帖
旧 03-04-30, 09:40 第 22 帖
plan9
 
plan9 的头像
 
 
注册会员  
  注册日期: Jan 2003
  帖子: 208
  精华: 3
 

作者 plan9 [原创]
输出目录树:

一个输效果基本和tree命令的输出完全一样(除了目录未的工作不同),文件带色彩输出,文件、目录数统计,贴上来给大家做个参考:

tree.sh
------------------------------------------------------------
代码:
#!/bin/sh # # tree.sh # A tool that display the dictionary structure in dos's # tree command style. # By Matthew <matthew@linuxforum.net> # # __@ # _ \<_ # (_)/(_) # Apr 29 2003 # # Tested on slackware, openbsd, netbsd, freebsd. # # Just for fun. # # The name of the ls program, please use # the absolute path, otherwise, there # may be get some strange errors. # LSPROG="/bin/ls" # COLOR DEFINE # ============ # DIR="\033[01;34m" EXE="\033[01;32m" DEV="\033[01;33m" LNK="\033[01;36m" ZIP="\033[01;31m" SOCK="\033[01;35m" NULL="\033[00m" ROOT=${1:-.} TRUE=0 FALSE=1 LAYERS=0 FILECOUNT=0 DIRCOUNT=0 # print_dash # ========== # Print the structure lines # print_dash() { local i=0 local num=$1 while [ $i -lt $num ]; do echo -n "|" for j in 1 2 3; do echo -n " " done i=`expr $i + 1` done echo -n "|-- " } # ispkg # ===== # Test if the file is a package like: # .gz .tar .tgz .tar.gz .zip .rar .rpm # and etc. # ispkg() { local f=$1 local i # Package extension list, you can add your coustom # extensions in it. # local pkg__ext=".gz .tar .tgz .tar.gz .zip .rar .rpm" # if the file's suffix contain any package extension # then cut it. for i in $pkg__ext; do f=${f%$i} done if [ "$f" != "$1" ]; then return $TRUE else return $FALSE fi } # mktree # ====== # The main function, that print the # dictionary structure in dos's tree # command style. It's runs in nesting. # mktree() { local f for f in `$LSPROG -1 $1 2> /dev/null`; do f=${f%/} f=${f##*/} # If dictionary then print it and enter # the nesting block. if [ -d $1/$f ]; then print_dash $LAYERS echo -e "${DIR}$f${NULL}" DIRCOUNT=`expr $DIRCOUNT + 1` LAYERS=`expr $LAYERS + 1` mktree $1/$f else print_dash $LAYERS # file is a symbol link if [ -L $1/$f ]; then echo -e "${LNK}$f${NULL}" # file is executable elif [ -x $1/$f ]; then echo -e "${EXE}$f${NULL}" # file is a device elif [ -c $1/$f -o -b $1/$f ]; then echo -e "${DEV}$f${NULL}" # file is a socket elif [ -S $1/$f ]; then echo -e "${SOCK}$f${NULL}" # file is a package elif `ispkg $f`; then echo -e "${ZIP}$f${NULL}" else echo -e "$f" fi FILECOUNT=`expr $FILECOUNT + 1` fi done LAYERS=`expr $LAYERS - 1` } echo $ROOT mktree $ROOT echo "\`" echo "$DIRCOUNT directories, $FILECOUNT files"
显示效果:
代码:
/home/matthew/ |-- src | |-- asm | | |-- hello.s | | |-- shell.s | | |-- hello | | |-- shell | |-- shell | | |-- tree.sh |-- xfce.tar.gz |-- mbox ` 3 directories, 7 files







__________________
Robert is on the way...

此帖于 03-04-30 13:19 被 plan9 编辑.
  plan9 当前离线   回复时引用此帖
旧 03-05-06, 02:02 第 23 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

标题: BASH调试器


感谢作者:LYOO兄[原创]
代码:
bashdb.sh负责生成debug档 #!/bin/bash #bashdb - bash debugger #该脚本将bashbd.pre和目标脚本处理成调试脚本 echo 'bash Debugger version 1.0' _dbname=${0##*/} if (( $# < 1 )); then echo "$_dbname: Usage: $_dbname filename" >&2 exit 1 fi _guineapig=$1 if [ ! -r $1 ]; then echo "$_dbname: Cannot read file '$_guineapig'." >&2 exit 1 fi shift _tmpdir=/tmp _libdir=. _debugfile=$_tmpdir/bashdb.$$ #正在被调试脚本的临时文件 cat $_libdir/bashdb.pre $_guineapig > $_debugfile exec bash $_debugfile $_guineapig $_tmpdir $_libdir "$@" bashdb.pre负责对被调试函数进行预处理 源码: #!/bin/bash #bashdb预处理部分 #本文件预处理被调试的shell脚本 #参数: #$1=初始试验脚本的名字 #$2=临时文件所保存在的目录 #$3=bashdb.pre和bashdb.fns被保存的目录 _debugfile=$0 _guineapig=$1 _tmpdir=$2 _libdir=$3 shift 3 source $_libdir/bashdb.fns declare -a _linebp let _trace=0 let _i=1 while read; do _lines[$_i]=$REPLY let _i=$_i+1 done < $_guineapig trap _cleanup EXIT let _steps=1 LINENO=-2 trap '_steptrap $LINENO' DEBUG bashdb.fns包含了DEBUG调用的调试函数 源码: #!/bin/bash #测试脚本的每行被执行之后,shell进入本函数 function _steptrap { _curline=$1 #当前运行行的行号 (( $_trace )) && _msg "$PS4 line $_curline: ${_lines[$_curline]}" if (( $_steps >= 0 )); then let _steps=$_steps-1 fi #首先查看是否达到行编号断点 #如果达到,则进入调试器 if _at_linenumbp ; then _msg "Reached breakpoint at line $_curline" _cmdloop #如果没有达到,则检查是否有中断条件存在且为真 #如果是,则进入调试器 elif [ -n "$_brcond" ] && eval $_brcond; then _msg "Bread condition $_brcond true at line $_curline" _cmdloop #如果不是,则检查是否在采用步进方式,步数是否达到。如果是,则进入调试器 elif (( $_steps == 0 )); then _msg "Stopped at line $_curline" _cmdloop fi } #调试器命令循环 function _cmdloop { local cmd args while read -e -p "bashdb> " cmd args; do case $cmd in h ) _menu ;; #打印命令菜单 bc) _setbc $args ;; #设置中断条件 bp) _setbp $args ;; #设置断点在给定行 cb) _clearbp $args ;; #清除一个或所有断点 ds) _displayscript ;; #列出脚本并显示断点 g ) return ;; #开始/再继续执行脚本 q ) exit ;; #退出 s ) let _steps=${args:-1} #单步执行N次(默认为1) return ;; x ) _xtrace ;; #切换执行追踪 !*) eval ${cmd#!} $args ;; #传递给shell * ) _msg "Invalid command: '$cmd'" ;; esac done } #查看这个行编号是否有一个断点 function _at_linenumbp { local i=0 #循环遍历断点数组并查看它们是否与当前行编号匹配。如果匹配就返回真(0), #否则就返回假 if [ "$_linebp" ]; then while (( $i < ${#_linebp[@]} )); do if (( ${_linebp[$i]} == $_curline )); then return 0 fi let i=$i+1 done fi return 1 } #设置断点在给定的行编号或列出断点 function _setbp { local i #如果无参数,调用断点列表函数。否则查看参数是否为正数 #如果不是,则打印错误消息。如果是,则查看行编号是否包含文本 #如果不是则打印错误信息。如果是,则回应当前断点和新的附加。并将它们 #输送到"排序",并将结果赋值给断点列表。这将导致断点按数字顺序排列 #注意,使用-u选项可以删除重复的断点 if [ -z "$1" ]; then _listbp elif [ $(echo $1 | grep '^[0-9]*') ]; then if [ -n "${_lines[$1]}" ]; then _linebp=($(echo $( (for i in "${_linebp[*]} $1"; do echo $i; done) | sort -n) )) _msg "Breakpoint set at line $1" else _msg "Breakpoints can only be set on non-blank lines" fi else _msg "Please specify a numeric line number" fi } #列出断点及中断条件 function _listbp { if [ -n "$_linebp" ]; then _msg "Breakpoints at lines: ${_linebp[*]}" else _msg "No breakpoints have been set" fi _msg "Break on condition:" _msg "$_brcond" } #清除单个或所有断点 function _clearbp { local i bps #如果没有参数,那么删除所有断点。否则查看参数是否为正数,如果不是 #则打印错误消息。如果是,则回应除被传递的那个之外的所有当前断点 #并将它们赋值给局部变量。(我们需要这样做是因为将它们赋值给_linebp #将使数组保持在同一大小并将值向回移动一位置,导致重复值)。然后销毁旧数组 #并将局部数组中的元素赋值,于是我们高效地重创了它,减掉了被传递的断点 if [ -z "$1" ]; then unset _linebp[*] _msg "All breakpoints have been cleared" elif [ $(echo $1 | grep '^[0-9]*') ]; then bps=($(echo $(for i in ${_linebp[*]}; do if (( $1 != $i )); then echo $i; fi; done) )) unset _linebp[*] _linebp=(${bps[*]}) _msg "Breakpoint cleared at line $1" else _msg "Please specify a numeric line number" fi } #设置或清除中断条件 function _setbc { if [ -n "$*" ]; then _brcond=$args _msg "Break when true: $_brcond" else _brcond= _msg "Break condition cleared" fi } #打印出shell脚本并标出断点的位置以及当前行 function _displayscript { local i=1 j=0 bp cl ( while (( $i <= ${#_lines[@]} )); do if [ ${_linebp[$j]} ] && (( ${_linebp[$j]} == $i )); then bp='*' let j=$j+1 else bp=' ' fi if (( $_curline == $i )); then cl=">" else cl=" " fi echo "$i:$bp $cl ${_lines[$i]}" let i=$i+1 done ) | more } #切换执行追踪on/off function _xtrace { let _trace="! $_trace" _msg "Execution trace " if (( $_trace )); then _msg "on" else _msg "off" fi } #打印传进来的参数到标准错误 function _msg { echo -e "$@" >&2 } #打印命令菜单 function _menu { _msg 'bashdb commands: bp N set breakpoint at line N bp list breakpoints and break condition bc string set break condition to string bc clear break condition cb N clear breakpoint at line N cb clear all breakpoints ds displays the test script and breakpoints g start/resume execution s [N] execute N statements (default 1) x toggle execution trace on/off h,? print this menu ! string passes string to a shell q quit' } #退出之前删除临时文件 function _cleanup { rm $_debugfile 2>/dev/null }
  KornLee 当前离线   回复时引用此帖
旧 03-05-08, 23:22 第 24 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

标题: 一个查看分区信息的脚本



来自:http://www.redhat.com

The msinfo.sh script

This shell script analyses the boot sector of the given partition and displays some information along with the "Total Special Sectors" in a message box. It
assumes that the filesystem on the given partition is a FAT16. If not, it will print an error message and exit. Invoke it as

# msinfo <partition name>

To run this script, you will need the "dialog" program that displays dialog boxes. You can get it from here.
代码:
#!/bin/sh # # msinfo.sh This shell script displays the boot sector of the # given partition. # # Author: Rahul U. Joshi # # Modifications Removed the use of expr and replaced it by the let # command. # # ------------------------------------------------------------------------ # This program is a free software, you can redistribute it and/or modify # it under the eterms of the GNU General Public Liscence as published by # the Free Software Foundation; either version 2 or (at your option) any # later version. # # This program is being distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY without even the implied warranty of # MERCHANTIBILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General # Public Liscence for more details. # ------------------------------------------------------------------------- # check for command line arguments if [ $# -ne 1 ]; then echo "Usage: msinfo <partition name>" exit 1 fi # check whether the input name is a block device if [ ! -b $1 ]; then echo "msinfo: $1 is not a block device" exit 1 fi # create two temporary files for use TMPFILE=`mktemp -q /tmp/$0.XXXXXX` if [ $? -ne 0 ]; then echo "msinfo: Can't create temp file, exiting..." exit 1 fi TXTFILE=`mktemp -q /tmp/$0.XXXXXX` if [ $? -ne 0 ]; then echo "msinfo: Can't create temp file, exiting..." rm -f $TMPFILE exit 1 fi backtitle="`printf "%78s" "msinfo, Information about FAT16 filesystem -- Rahul Joshi"`" dialog --title "Boot sector of $1" --backtitle "$back_title" \ --infobox "\nAnalysing boot sector for $1\nPlease wait..." 14 60 # truncate TXTFILE to zero length echo > $TXTFILE # get Formatting DOS version dd 2>/dev/null if=$1 bs=1 count=8 skip=3 | dd 2>/dev/null of=$TMPFILE printf >>$TXTFILE "%30s : %s\n" "Formatting DOS version" "`cat $TMPFILE`" # get file system dd 2>/dev/null if=$1 bs=1 count=8 skip=54 | dd 2>/dev/null of=$TMPFILE printf >>$TXTFILE "%30s : %s\n" "Filesystem" "`cat $TMPFILE`" # check if filesystem in a FAT16 if [ "`cat $TMPFILE`" != "FAT16 " ]; then dialog --title "Boot sector of $1" --backtitle "$back_title" \ --infobox "\nCan't find a FAT16 filesystem on $1" 14 60 exit 2 fi # get volume label in boot sector dd 2>/dev/null if=$1 bs=1 count=11 skip=43 | dd 2>/dev/null of=$TMPFILE printf >>$TXTFILE "%30s : %s\n" "Volume label in boot sector" "`cat $TMPFILE`" # get Sector size dd 2>/dev/null if=$1 bs=1 count=2 skip=11| od -An -tdS | dd 2>/dev/null of=$TMPFILE printf >>$TXTFILE "%30s : %d\n" "Sector size" `cat $TMPFILE` sector_size=`cat $TMPFILE` # get Reserved sectors dd 2>/dev/null if=$1 bs=1 count=2 skip=14| od -An -tdS | dd 2>/dev/null of=$TMPFILE printf >>$TXTFILE "%30s : %d\n" " Reserved sectors" `cat $TMPFILE` reserved_sectors=`cat $TMPFILE` # get FAT sectors dd 2>/dev/null if=$1 bs=1 count=1 skip=16| od -An -tdS | dd 2>/dev/null of=$TMPFILE fat_count=`cat $TMPFILE` dd 2>/dev/null if=$1 bs=1 count=2 skip=22| od -An -tdS | dd 2>/dev/null of=$TMPFILE sectors_per_fat=`cat $TMPFILE` # calculate the no of sectors allocated for FAT's let fat_sectors=fat_count*sectors_per_fat printf >>$TXTFILE "%30s : %u (%u x %u) \n" "FAT sectors" "$fat_sectors" \ "$fat_count" "$sectors_per_fat" # get root directory sectors dd 2>/dev/null if=$1 bs=1 count=2 skip=17| od -An -tdS | dd 2>/dev/null of=$TMPFILE root_sectors=`cat $TMPFILE` # calculate the no of sectors allocated for root directory let root_sectors=root_sectors*32/sector_size printf >>$TXTFILE "%30s : %u\n" "Root directory sectors" "$root_sectors" # get Total special sectors let total=reserved_sectors+fat_sectors+root_sectors printf >>$TXTFILE "%30s : %u\n" "Total special sectors" "$total" # display the information dialog --title "Boot sector of $1" --backtitle "$back_title" --msgbox "`cat $TXTFILE`" 14 60 # delete temporary files rm -f $TMPFILE rm -f $TXTFILE # end of msinfo.sh
看看老外是怎么写脚本的,值得我们学习借鉴
  KornLee 当前离线   回复时引用此帖
旧 03-05-14, 02:10 第 25 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

标题: 如何编写函数


来自http://academic.strose.edu/academic/...22/linux10.htm

Functions
你可以在bash shell的环境下编写函数,有两种定义函数的格式:
代码:
function functname() { shell commands } functname() { shell commands }
下面是一个简单函数的脚本(ex1)示例:
代码:
message() { echo "message" } let i=1 while [ $i -le 3 ] do message let i=$i+1 done
函数同样可以接受参数,$1存放第一个参数,$2存放第二个参数,$*存放输入参数的列表,...
代码:
$ more ex2 power() { x=$1 y=$2 count=1 result=1 while [ $count -le $y ] do result=`expr ${result} \* $x` count=`expr ${count} + 1` done echo $result } echo "Enter two numbers" read num1 num2 echo -n "power is: " power $num1 $num2
$ ex2
Enter two numbers
3 4
power is: 81

函数同样也可以返回值,使用return语句,在主程序(块)中,在调用函数之后保存返回状态$?的值.
代码:
power() { x=$1 y=$2 count=1 result=1 while [ $count -le $y ] do result=`expr ${result} \* $x` count=`expr ${count} + 1` done return $result } echo "Enter two numbers" read num1 num2 power $num1 $num2 answer=$? echo "$num1 to $num2 is $answer"
你也可以编写递归函数:
代码:
power() { x=$1 y=$2 if [ $y -eq 1 ] then return $x else y=`expr $y - 1` power $x $y result=`expr $? \* $x` return $result fi } echo "Enter two numbers" read num1 num2 power $num1 $num2 answer=$? echo "$num1 to $num2 is $answer"
默认情况下,所有函数的变量都是全局变量.你可以用typeset去声明一个局部变量:
代码:
easy() { typeset a a=`expr $1 + $2` b=`expr $1 + $2` echo "easy a is " $a echo "easy b is " $b } a=10 b=20 easy $a $b echo "global a is " $a echo "global b is " $b
Output:
easy a is 30
easy b is 30
global a is 10
global b is 30

如果要在脚本中多次使用函数,可以把它放在一个函数目录中,像一个普通文件一样,使用的时候把它放在脚本开始的地方:
. functionfile

$ more power
代码:
power() { x=$1 y=$2 if [ $y -eq 1 ] then return $x else y=`expr $y - 1` power $x $y result=`expr $? \* $x` return $result fi }
$ more small
代码:
. power echo "Enter two numbers:" read x y power $x $y result=$? echo "Answer is:" $result
$ small.ksh
2 5
Answer is: 32
注:翻译的水平有限,望多包涵;)

此帖于 03-05-16 20:34 被 KornLee 编辑.
  KornLee 当前离线   回复时引用此帖
旧 03-05-15, 23:33 第 26 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

标题: 一个备份文件的脚本示例


来自:http://www.europe.redhat.com/
代码:
#!/bin/bash # Backs up all files in current directory # modified within last 24 hours # in a tarred and gzipped file. if [ $# = 0 ] then echo "Usage: `basename $0` filename" exit 1 fi tar cvf - `find . -mtime -1 -type f -print` > $1.tar gzip $1.tar exit 0
  KornLee 当前离线   回复时引用此帖
旧 03-05-17, 23:48 第 27 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

标题: 删除文件与恢复文件


作者 pupilzeng
这一共有三个脚本,他们是一套的
1)del:删除文件,其实是把它移动到/trash/$user/下,不同的用户有不同的存放目录,同时用该目录下的.record文件记录文件原来的路径,删除时间,以备恢复只用。
2)recover:恢复文件,通过.record文件找到足够的信息,从而把它恢复
3)erase:这个是彻底的删除文件,从/trash/$user/目录下,相当于windows下的清空回收站。
为了安全,这个脚本是要在/trash/$user/目录下运行。
以下是这三个脚本
del:
代码:
#!/bin/bash #move the file(s) to the /trash/~ instead of deleting. #Author: pupilzeng #E-mail: shixinzeng@sjtu.edu.cn USER=`whoami` TRASH=/trash/$USER RECORD=/trash/$USER/.record #record file ORIG=`pwd` DATE=`date +%T---%Y/%m/%d` Usage () { echo "Usage: `basename $0` file(s)" } if [ "$1" = "-h" -o "$1" = "--help" ];then Usage exit 0 fi if [ $# -le 0 ];then Usage exit 1 fi if [ ! -d $TRASH ];then mkdir -p $TRASH fi for i in "$@" do if [ -w "$i" ];then mv "$i" $TRASH if [ $? -ne 0 ];then echo "Something wrong occurred while delete file $i" #but now i won't exit,because there may be other files to be deleted! else #now write the record file #the lines below were modified! CURRENTDIR=$PWD #* cd `basename "$i"` echo -e "$PWD/`basename "$i"`\t\t$DATE ">>$RECORD cd $CURRENTDIR #* return to original working directory #The lines marked * can be omitted! For safety,they remain there. #--------------------------------------------------------------- fi else echo "You have not enough permission to delete $i!" fi done exit 0
recover:
代码:
#!/bin/bash #recover #Author: pupilzeng #E-mail: shixinzeng@sjtu.edu.cn #To recover the removed file(s) by script myrm USER=`whoami` TRASH=/trash/$USER RECORD=$TRASH/.record TEMP=$TRASH/.temp Usage () { echo "Usage:`basename $0` file(s)" } if [ "$1" = "-h" -o "$1" = "--help" ];then Usage exit 0 fi for i in "$@" do DEST=`grep "$i" $RECORD |awk '{ print $1}'` mv -f "$i" $DEST if [ $? -ne 0 ];then echo "Something occurred!" exit 1 else echo "Recovered $DEST" #remove record from $RECORD grep -v "$i" $RECORD >$TEMP mv -f $TEMP $RECORD fi done exit 0
erase:
代码:
#!/bin/bash #erase #Author: pupilzeng #E-mail: shixinzeng@sjtu.edu.cn #erase the files in trash that you are sure they needn't at all. #for assurance,you should do it in /trash/user directory. Usage () { cat <<END Usage:`basename $0` [Option] file(s) Options: -f :don't prompt before erase files END } USER=`whoami` TRASH=/trash/$USER RECORD=$TRASH/.record FORCE=no TEMP=$TRASH/.temp if [ $# -lt 1 ] then echo "Wrong parameters" Usage exit 1 fi if [ $PWD != $TRASH ] then echo "you should do it in $TRASH directory!" exit 1 fi if [ "$1" = "-h" -o "$1" = "--help" ] then Usage exit 0 fi if [ "$1" = "-f" ];then FORCE=yes shift fi for i in "$@" do ANS=no if [ $FORCE = "yes" ];then rm -fr "$i" else echo -n "Do you really wanna erase "$i"? Yes|[No]:" read ANS case $ANS in "Y"|"y"|"Yes"|"yes") rm -fr "$i" ;; *) continue ;; esac fi if [ $? -eq 0 ];then #now remove the records grep -v "$i" $RECORD >$TEMP mv -f $TEMP $RECORD fi done exit 0

此帖于 03-05-21 02:28 被 KornLee 编辑.
  KornLee 当前离线   回复时引用此帖
旧 03-05-20, 22:39 第 28 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

标题: 用shell脚本编写的进度条


转自:www.chinaunix.net
drawper ()
{
_per=`expr $1 \* 100 / $2`
case `expr $_per / 4 % 4` in
0) _char="|" ;;
1) _char="/" ;;
2) _char="-" ;;
3) _char="\\" ;;
esac
printf "\r$_char $_per%%"
if [ $1 -eq $2 ];then
printf "\n"
fi
}

i=1
while [ $i -le 100 ]
do
drawper $i 100
i=`expr $i + 1`
done
看看人家是怎么玩shell的,值得学习!
  KornLee 当前离线   回复时引用此帖
旧 03-05-23, 00:20 第 29 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

标题: 一个加密文本文件的脚本


作者:javalee
利用vim中对key=的设置,可以达到加密文本文件的目的,
代码:
#!/bin/ksh # 利用vi中的key=命令,对文本进行加密处理. #scriptname:jmfile (($# != 2)) && { print "格式: jmfile password filename";exit 1; } vim -e -s -c ":set key=$1" -c ":wq" $2 print "文件$2已经加密!"
如:
javalee//home/javalee/lx>echo aaaaa>tmp
javalee//home/javalee/lx>cat tmp
aaaaa
javalee//home/javalee/lx>jmfile 123 tmp
文件tmp已经加密!
javalee//home/javalee/lx>cat tmp
VimCrypt~01!!x蹥o鮦avalee//home/javalee/lx> //加密后的文件用cat显示全是乱字符.
javalee//home/javalee/lx>vi tmp //编辑
in vim...
输入密码:***
如果密码正确就可以编辑tmp文件.
关于VIM的使用,请vim --help

此帖于 03-05-23 00:23 被 KornLee 编辑.
  KornLee 当前离线   回复时引用此帖
旧 03-05-24, 19:31 第 30 帖
hdw1978
 
 
 
注册会员  
  注册日期: Jan 2003
  帖子: 47
  精华: 0
 

标题: 一个简单的notify程序


作者:hdw1978
if [ $1 = 0 ]; then
esdplay /home/hdw1978/tada.wav > /dev/null
else
esdplay /home/hdw1978/chord.wav > /dev/null
fi

用法:
在make一个大软件的时候可以用make;notify, 然后你就可以去忙别的事了,make完成后会播放一个音效通知你。而且还可以附带*出一个通知窗口,以免临时走开没听到(不过我没有做)

还可以用在其它耗时较长又不需要一直观察的任务后面

很简单,但是我经常用

此帖于 03-05-25 01:05 被 KornLee 编辑.
  hdw1978 当前离线   回复时引用此帖
发表新主题 回复


主题工具

发帖规则
您 [不可以] 发表新主题
您 [不可以] 回复主题
您 [不可以] 上传附件
您 [不可以] 编辑您的帖子

已 [启用] BB 代码
已 [启用] 表情符号
已 [启用] IMG 代码
已 [禁用] HTML 代码
[论坛跳转…]


所有时间均为[北京时间]。现在的时间是 12:32


Powered by vBulletin 版本 3.6.8
版权所有 ©2000 - 2012, Jelsoft Enterprises Ltd.
官方中文技术支持: vBulletin 中文
版权所有 ©2002 - 2011, LinuxSir.Org