LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
楼主: KornLee

【shell脚本欣赏区】:[展示你的作品的好去处!欢迎投帖]

[复制链接]
发表于 2003-8-15 00:19:28 | 显示全部楼层

替换某个目录下所有文件中的某个字符串为另一个字符串。

使用方式如下:
frall.sh /work teststr testok
执行结果如下:
/work目录下所有文件(包括子目录下)中存在teststr字符串的位置,全部被testok字符串所替代.
  1. #!/bin/sh
  2. useage()
  3. {
  4. echo "useage:$0 dirname oldstr newstr"
  5. echo "attantion:dirname must haven't oldstr!"
  6. }
  7. tf1=/tmp/.f.tmp1
  8. tf2=/tmp/.f.tmp2
  9. workdir=`pwd`
  10. rm -f $tf1
  11. rm -f $tf2
  12. # do with /$2-xxx and /$2-xxx/$2-yyy
  13. if [ $# -eq 0 ];then
  14.         useage
  15.         exit
  16. fi
  17. for dir in `find $1/* -type d`
  18. do
  19.         echo $dir | awk 'BEGIN {FS="/"} ;{print $NF}' | grep $2
  20.         if [ $? = 0 ];then
  21.                 echo `echo $dir | wc -c ` $dir >> $tf1
  22.         fi
  23. done
  24. sort -r -n $tf1 -o $tf2
  25. while read LINE
  26. do
  27.         dir=`echo $LINE | cut -d " " -f 2`
  28.         olddir=`echo $dir | awk 'BEGIN {FS="/"};{print $NF}'`
  29.         newdir=`echo $olddir | sed s/$2/$3/g`
  30.         cd $dir;cd ..
  31.         mv $olddir $newdir
  32.         cd $workdir
  33. done < $tf2
  34. for file in `find $1/* -type f`
  35. do
  36. grep $2 $file 1>2 2>/dev/null
  37.   if [ $? = 0 ] && [ $file != $0 ]; then
  38. #       echo "$file have $2 " >> /tmp/nnn
  39. ed - $file << EO
  40. g/$2/s/$2/$3/g
  41. .
  42. w
  43. q
  44. EO
  45. fi
  46.         newname=`echo $file | sed s/$2/$3/g`
  47.         mv $file $newname
  48. done
复制代码
 楼主| 发表于 2003-8-19 11:26:56 | 显示全部楼层

backup version 0.59

感谢作者:alphatan
脚本说明
backup version 0.59
把backup脚本分两个文件(backup.sh跟backupFunc.sh,请使用者把下述语句分开保存),现版本更具移植性。
首先,脚本脱离了uMntFtpDirs与01MntFtpDirs.sh的束缚,通过读取/etc/mtab,用函数实现了在备份系统时不用备份--bind的文件夹。
另外,本版本中不直接使用命令名进行操作,而使用变量来代指,一来便于用户在自己的机上使用--只需要更改LEADINGDIR(指向放置.tar.gz 文件的绝对路径)、PATH2BCKUPSH(指向backup.sh的绝对路径)跟$SUCOM后接的用户名的值就可以了。而且如果要提高脚本的安全性,可以把本脚本要用到的程序备份到一个独立目录,再把变量指向它,然后就算系统后来感染了,也还可以保持备份的安全性。
最后,本脚本还具备信号处理能力,可以防止用户不小心错按CTRL-C等发出TERM, INT信号,当然,用户如果真要结束脚本,只需要按提示应答即可。
同时,本人为本脚本程序源用GPL协议发布,用以更方便大家传播与使用。
  1. ------------------------------------backup.sh------------------------------------------------
  2. #!/bin/sh
  3. #
  4. # "backup.sh" is a script written in bash to backup the whole system
  5. # Copyright (C) 2003 alphatan<alphatan@263.net> version 0.59
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. # You can get it by accessing [url]http://www.gnu.org/licenses/gpl.html[/url]
  11. #
  12. declare -i whereAmI=0
  13. # declare the script wide variables and definitions.
  14. LEADINGDIR=/backup/filebck/LinuxBck
  15. PATH2BCKUPSH=/etc/rc.d/init.d
  16. AWKCOM=$(which awk)
  17. CATCOM=$(which cat)
  18. CHOWNCOM=$(which chown)
  19. CPCOM=$(which cp)
  20. ECHOCOM=$(which echo)
  21. FINDCOM=$(which find)
  22. LNCOM=$(which ln)
  23. MKCOM=$(which mkdir)
  24. MOUNTCOM=$(which mount)
  25. RMCOM=$(which rm)
  26. SUCOM=$(which su)
  27. TARCOM=$($ECHOCOM $(which tar) -Pzcf )
  28. TOUCHCOM=$(which touch)
  29. UMOUNTCOM=$(which umount)
  30. declare -a mountedDirsFromMtab=( $(grep '/var/ftp/' /etc/mtab) )
  31. declare -i mountedDirsAmount=${#mountedDirsFromMtab[@]}
  32. declare -f backupSystemDirs operatingMountDirs calculateTm signalDealingFunc
  33. source ${0%/*.sh}/backupFunc.sh
  34. trap signalDealingFunc INT TERM QUIT
  35. # declare the copyright of this software
  36. $ECHOCOM -ne "\n\n"
  37. $ECHOCOM -ne "\t "backup.sh" is a script written in bash to backup the whole system \n"
  38. $ECHOCOM -ne "\t Copyright (C) 2003 alphatan<alphatan@263.net> version 0.59\n"
  39. $ECHOCOM -ne "\t This program is free software; you can redistribute it and/or modify \n"
  40. $ECHOCOM -ne "\t it under the terms of the GNU General Public License as published by \n"
  41. $ECHOCOM -ne "\t the Free Software Foundation; either version 2 of the License, or \n"
  42. $ECHOCOM -ne "\t (at your option) any later version. \n"
  43. $ECHOCOM -ne "\t You can get it by accessing [url]http://www.gnu.org/licenses/gpl.html[/url] \n"
  44. $ECHOCOM -ne "\n Input the backup dir name: "
  45. read BACKUPDIR
  46. # time calculating process starts
  47. calculateTm start
  48. let whereAmI=0
  49. # backup system wide files
  50. backupSystemDirs
  51. let whereAmI=0
  52. # Establish DETAILED and SIMPLE system list.
  53. SYSLISTDIR=sysList
  54. if [ ! -d $LEADINGDIR/$BACKUPDIR/$SYSLISTDIR ]; then
  55. $MKCOM $LEADINGDIR/$BACKUPDIR/$SYSLISTDIR
  56. fi
  57. operatingMountDirs umount
  58. let whereAmI=0
  59. $FINDCOM / -fprint "$LEADINGDIR/$BACKUPDIR/$SYSLISTDIR/$BACKUPDIR""$SYSLISTDIR""Simple.txt"
  60. $FINDCOM / -fls "$LEADINGDIR/$BACKUPDIR/$SYSLISTDIR/$BACKUPDIR""$SYSLISTDIR""Detailed.txt"
  61. operatingMountDirs mount
  62. let whereAmI=0
  63. # Backup this script.
  64. $CPCOM $PATH2BCKUPSH/backup*.sh $LEADINGDIR/$BACKUPDIR/
  65. # Establish sysChange.log by "su alphatan" at $BACKUPDIR;
  66. # Then, remove the former linkage, and "link" again at /home/alphatan;
  67. $TOUCHCOM $LEADINGDIR/$BACKUPDIR/sysChange.log
  68. $CHOWNCOM alphatan.root $LEADINGDIR/$BACKUPDIR/sysChange.log
  69. $SUCOM alphatan --command="$RMCOM -rf ~/sysChange.log"
  70. $SUCOM alphatan --command="$LNCOM -s $LEADINGDIR/$BACKUPDIR/sysChange.log ~/sysChange.log"
  71. # "empty line to end syntax hightlight of "
  72. # Calculate consuming time.
  73. calculateTm end
  74. let whereAmI=0
  75. ------------------------------------backup.sh------------------------------------------------
  76. ------------------------------------backupFunc.sh------------------------------------------------
  77. #
  78. # "backupFunc.sh" is a script of functions written in bash as an associated "backup.sh"
  79. # Copyright (C) 2003 alphatan<alphatan@263.net> version 0.59
  80. # This program is free software; you can redistribute it and/or modify
  81. # it under the terms of the GNU General Public License as published by
  82. # the Free Software Foundation; either version 2 of the License, or
  83. # (at your option) any later version.
  84. # You can get it by accessing [url]http://www.gnu.org/licenses/gpl.html[/url]
  85. #
  86. # Following is the definitions of functions.
  87. function calculateTm()
  88. {
  89. let whereAmI=1
  90. if [ $1 = 'end' ]; then
  91. ENDTIMEMARK=`date +%s`
  92. let USEDSECONDS=$ENDTIMEMARK-$STARTTIMEMARK
  93. let USEDSECOND=$USEDSECONDS%60
  94. let USEDMINUTE=($USEDSECONDS/60)%60
  95. let USEDHOUR=($USEDSECONDS/3600)%60
  96. if [ $USEDSECONDS == 0 ]; then
  97. $ECHOCOM "It doesn't need a second?? Is there something in trouble?"
  98. builtin exit 1
  99. else
  100. $ECHOCOM "It used $USEDHOUR:$USEDMINUTE:$USEDSECOND"
  101. builtin exit 0
  102. fi
  103. elif [ $1 = 'start' ]; then
  104. STARTTIMEMARK=`date +%s`
  105. else
  106. $ECHOCOM -e " calculateTM Usage: calculateTM [start|stop] " >&2
  107. exit 1
  108. fi
  109. }
  110. function operatingMountDirs()
  111. {
  112. let whereAmI=1
  113. declare -i i=0
  114. if [ $1 = mount ]; then
  115. local OPERATIONONTHEMOUNTDIRS='$MOUNTCOM --bind ${mountedDirsFromMtab[$i]} ${mountedDirsFromMtab[$i+1]}'
  116. elif [ $1 = umount ]; then
  117. local OPERATIONONTHEMOUNTDIRS='$UMOUNTCOM ${mountedDirsFromMtab[$i+1]}'
  118. else
  119. $ECHOCOM -e " operatingMountDirs Usage: operatingMountDirs [mount|umount] " >&2
  120. exit 1
  121. fi
  122. while [ $i -lt $mountedDirsAmount ] ; do
  123. eval $OPERATIONONTHEMOUNTDIRS
  124. let i+=6
  125. done
  126. }
  127. function backupSystemDirs()
  128. {
  129. let whereAmI=1
  130. if [ ! -d $LEADINGDIR/$BACKUPDIR ]; then
  131. $MKCOM $LEADINGDIR/$BACKUPDIR
  132. fi
  133. $TARCOM $LEADINGDIR/$BACKUPDIR/bin.tar.gz /bin
  134. $TARCOM $LEADINGDIR/$BACKUPDIR/boot.tar.gz /boot
  135. $TARCOM $LEADINGDIR/$BACKUPDIR/dev.tar.gz /dev
  136. $TARCOM $LEADINGDIR/$BACKUPDIR/etc.tar.gz /etc
  137. $TARCOM $LEADINGDIR/$BACKUPDIR/home.tar.gz /home
  138. $TARCOM $LEADINGDIR/$BACKUPDIR/initrd.tar.gz /initrd
  139. $TARCOM $LEADINGDIR/$BACKUPDIR/lib.tar.gz /lib
  140. $TARCOM $LEADINGDIR/$BACKUPDIR/misc.tar.gz /misc
  141. $TARCOM $LEADINGDIR/$BACKUPDIR/opt.tar.gz /opt
  142. $TARCOM $LEADINGDIR/$BACKUPDIR/root.tar.gz /root
  143. $TARCOM $LEADINGDIR/$BACKUPDIR/sbin.tar.gz /sbin
  144. $TARCOM $LEADINGDIR/$BACKUPDIR/tftpboot.tar.gz /tftpboot
  145. operatingMountDirs umount
  146. $TARCOM $LEADINGDIR/$BACKUPDIR/var.tar.gz /var
  147. operatingMountDirs mount
  148. $TARCOM $LEADINGDIR/$BACKUPDIR/usr_share.tar.gz /usr/share
  149. $TARCOM --exclude=/usr/share $LEADINGDIR/$BACKUPDIR/usr.tar.gz /usr
  150. }
  151. function signalDealingFunc()
  152. {
  153. local userChoice
  154. local lineNumber=$LINENO
  155. if (( $whereAmI==0 )); then local currentOperatingFileName=$0 #/etc/rc.d/init.d/backup.sh
  156. else local currentOperatingFileName=${0%.sh}Func.sh #/etc/rc.d/init.d/backupFunc.sh
  157. fi
  158. $ECHOCOM "Currently running LINE $LINENO within $currentOperatingFileName "
  159. $CATCOM $currentOperatingFileName |$AWKCOM "{if (NR==$lineNumber || NR==$lineNumber-1 || NR==$lineNumber+1) print NR":\ "\$0;}"
  160. builtin read -p "Are you sure you want to exit?(1:yes, 2:no) " userChoice
  161. until [ -n "$($ECHOCOM $userChoice | egrep '^(1|2|y|yes|n|no)$')" ]; do
  162. builtin read -p "Not a legal choice, (1:yes, 2:no) " userChoice
  163. done
  164. case $userChoice in
  165. 1 | y | yes ) builtin exit 1 ;;
  166. *) return ;;
  167. esac
  168. }
  169. ------------------------------------backupFunc.sh------------------------------------------------
复制代码
发表于 2003-8-23 03:00:08 | 显示全部楼层

回复: 替换某个目录下所有文件中的某个字符串为另一个字符串。

最初由 deepin 发表
使用方式如下:
frall.sh /work teststr testok
执行结果如下:
/work目录下所有文件(包括子目录下)中存在teststr字符串的位置,全部被testok字符串所替代.

  1. #!/bin/sh

  2. useage()
  3. {
  4. echo "useage:$0 dirname oldstr newstr"
  5. echo "attantion:dirname must haven't oldstr!"
  6. }

  7. tf1=/tmp/.f.tmp1
  8. tf2=/tmp/.f.tmp2
  9. workdir=`pwd`
  10. rm -f $tf1
  11. rm -f $tf2

  12. # do with /$2-xxx and /$2-xxx/$2-yyy
  13. if [ $# -eq 0 ];then
  14.         useage
  15.         exit
  16. fi

  17. for dir in `find $1/* -type d`
  18. do
  19.         echo $dir | awk 'BEGIN {FS="/"} ;{print $NF}' | grep $2
  20.         if [ $? = 0 ];then
  21.                 echo `echo $dir | wc -c ` $dir >> $tf1
  22.         fi
  23. done
  24. sort -r -n $tf1 -o $tf2

  25. while read LINE
  26. do
  27.         dir=`echo $LINE | cut -d " " -f 2`
  28.         olddir=`echo $dir | awk 'BEGIN {FS="/"};{print $NF}'`
  29.         newdir=`echo $olddir | sed s/$2/$3/g`
  30.         cd $dir;cd ..
  31.         mv $olddir $newdir
  32.         cd $workdir
  33. done < $tf2




  34. for file in `find $1/* -type f`
  35. do
  36. grep $2 $file 1>2 2>/dev/null
  37.   if [ $? = 0 ] && [ $file != $0 ]; then
  38. #       echo "$file have $2 " >> /tmp/nnn
  39. ed - $file << EO
  40. g/$2/s/$2/$3/g
  41. .
  42. w
  43. q
  44. EO
  45. fi
  46.         newname=`echo $file | sed s/$2/$3/g`
  47.         mv $file $newname
  48. done
复制代码
其实有个命令就是这个功能的
rename str1 str2 files
呵呵
发表于 2003-8-30 11:40:03 | 显示全部楼层

回复: shell命令中文帮助

最初由 javalee 发表
特别感谢作者: ilmargaret兄

备注: 附件datafile里你可以不断的添加新的命令,只要添加的命令与我原来的格式一致即可,时间久了就变成手册了,随时查阅,对shell编程很有用!


这个datafile是要自己加入中文说明吗?格式是什么样子的?
 楼主| 发表于 2003-9-3 17:25:01 | 显示全部楼层

Dialog经典--Slackware软件包管理程序

推荐者:Sandy
  1. #!/bin/sh
  2. #
  3. # Copyright 1993, 1994, 1995, 1996, 1997,
  4. #    1998, 1999  Patrick Volkerding,  Moorhead, MN  USA
  5. # Copyright 2001  Slackware Linux, Inc.,  Concord, CA  USA
  6. #    All rights reserved.
  7. #
  8. # Redistribution and use of this script, with or without modification, is
  9. # permitted provided that the following conditions are met:
  10. #
  11. # 1. Redistributions of this script must retain the above copyright
  12. #    notice, this list of conditions and the following disclaimer.
  13. #
  14. #  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  15. #  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. #  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
  17. #  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  18. #  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. #  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  20. #  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  21. #  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  22. #  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  23. #  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. #
  25. # Wed, 27 Apr 1994 00:06:50 -0700 (PDT)
  26. # Optimization by David Hinds.
  27. SOURCE_DIR=/var/log/mount
  28. ASK="tagfiles"
  29. if [ -L /bin/chmod -a -L /bin/chown ]; then # probably on the bootdisk using busybox
  30. TARGET_DIR=/mnt
  31. TMP=/mnt/var/log/setup/tmp
  32. if mount | grep "on /mnt" 1> /dev/null 2>&1 ; then # good
  33.   true
  34. else # bad
  35.   echo
  36.   echo
  37.   echo "You can't run pkgtool from the rootdisk until you've mounted your Linux"
  38.   echo "partitions beneath /mnt. Here are some examples of this:"
  39.   echo
  40.   echo "If your root partition is /dev/hda1, and is using ext2fs, you would type:"
  41.   echo "mount /dev/hda1 /mnt -t ext2"
  42.   echo
  43.   echo "Then, supposing your /usr partition is /dev/hda2, you must do this:"
  44.   echo "mount /dev/hda2 /mnt/usr -t ext2"
  45.   echo
  46.   echo "Please mount your Linux partitions and then run pkgtool again."
  47.   echo
  48.   exit
  49. fi
  50. else
  51. TARGET_DIR=/
  52. TMP=/var/log/setup/tmp
  53. fi
  54. if [ ! -d $TMP ]; then
  55. mkdir -p $TMP
  56. chmod 700 $TMP
  57. fi
  58. ADM_DIR=$TARGET_DIR/var/log
  59. LOG=$TMP/PKGTOOL.REMOVED
  60. # remove whitespace
  61. crunch() {
  62.   while read FOO ; do
  63.     echo $FOO
  64.   done
  65. }
  66. package_name() {
  67.   STRING=`basename $1 .tgz`
  68.   # Check for old style package name with one segment:
  69.   if [ "`echo $STRING | cut -f 1 -d -`" = "`echo $STRING | cut -f 2 -d -`" ]; then
  70.     echo $STRING
  71.   else # has more than one dash delimited segment
  72.     # Count number of segments:
  73.     INDEX=1
  74.     while [ ! "`echo $STRING | cut -f $INDEX -d -`" = "" ]; do
  75.       INDEX=`expr $INDEX + 1`
  76.     done
  77.     INDEX=`expr $INDEX - 1` # don't include the null value
  78.     # If we don't have four segments, return the old-style (or out of spec) package name:
  79.     if [ "$INDEX" = "2" -o "$INDEX" = "3" ]; then
  80.       echo $STRING
  81.     else # we have four or more segments, so we'll consider this a new-style name:
  82.       NAME=`expr $INDEX - 3`
  83.       NAME="`echo $STRING | cut -f 1-$NAME -d -`"
  84.       echo $NAME
  85.       # cruft for later
  86.       #VER=`expr $INDEX - 2`
  87.       #VER="`echo $STRING | cut -f $VER -d -`"
  88.       #ARCH=`expr $INDEX - 1`
  89.       #ARCH="`echo $STRING | cut -f $ARCH -d -`"
  90.       #BUILD="`echo $STRING | cut -f $INDEX -d -`"
  91.     fi
  92.   fi
  93. }
  94. remove_packages() {
  95. for pkg_name in $*
  96. do
  97.   if [ -r $ADM_DIR/packages/$pkg_name ]; then
  98.    dialog --title "PACKAGE REMOVAL IN PROGRESS" --cr-wrap --infobox \
  99. "\nRemoving package $pkg_name.\n\
  100. \n\
  101. Since each file must be checked \
  102. against the contents of every other installed package to avoid wiping out \
  103. areas of overlap, this process can take quite some time. If you'd like to \
  104. watch the progress, flip over to another virtual console and type:\n\
  105. \n\
  106. tail -f $TMP/PKGTOOL.REMOVED\n" 13 60
  107.    export ROOT=$TARGET_DIR
  108.    removepkg $pkg_name >> $LOG 2> /dev/null
  109.   else
  110.    echo "No such package: $pkg_name. Can't remove." >> $LOG
  111.   fi
  112. done
  113. }
  114. # Here, we read the list of arguments passed to the pkgtool script.
  115. if [ $# -gt 0 ]; then # there are arguments to the command
  116. while [ $# -gt 0 ]; do
  117.   case "$1" in
  118.   "-sets")
  119.    DISK_SETS=`echo $2 | tr "[A-Z]" "[a-z]"` ; shift 2 ;;
  120.   "-source_mounted")
  121.    SOURCE_MOUNTED="always" ; shift 1 ;;
  122.   "-ignore_tagfiles")
  123.    ASK="never" ; shift 1 ;;
  124.   "-tagfile")
  125.    USETAG=$2 ; shift 2 ;;
  126.   "-source_dir")
  127.    SOURCE_DIR=$2 ; shift 2 ;;
  128.   "-target_dir")
  129.    TARGET_DIR=$2
  130.    ADM_DIR=$TARGET_DIR/var/log
  131.    shift 2 ;;
  132.   "-source_device")
  133.    SOURCE_DEVICE=$2 ; shift 2 ;;
  134.   esac
  135. done
  136. else  # there were no arguments, so we'll get the needed information from the
  137.       # user and then go on.
  138. CMD_START="true"
  139. rm -f $TMP/SeT*
  140. while [ 0 ]; do
  141.   dialog --title "Slackware Package Tool (pkgtool version 9.0.0)" \
  142. --menu "\nWelcome to the Slackware package tool.\n\
  143. \nWhich option would you like?\n" 17 75 7 \
  144. "Current" "Install packages from the current directory" \
  145. "Other" "Install packages from some other directory" \
  146. "Floppy" "Install packages from floppy disks" \
  147. "Remove" "Remove packages that are currently installed" \
  148. "View" "View the list of files contained in a package" \
  149. "Setup" "Choose Slackware installation scripts to run again" \
  150. "Exit" "Exit Pkgtool" 2> $TMP/reply
  151.   if [ ! $? = 0 ]; then
  152.    rm -f $TMP/reply
  153.    dialog --clear
  154.    exit
  155.   fi
  156.   REPLY="`cat $TMP/reply`"
  157.   rm -f $TMP/reply
  158.   if [ "$REPLY" = "Exit" ]; then
  159.    dialog --clear
  160.    exit
  161.   fi
  162.   if [ "$REPLY" = "Setup" ]; then
  163.     echo 'dialog --title "SELECT SYSTEM SETUP SCRIPTS" --item-help --checklist \
  164.     "Please use the spacebar to select the setup scripts to run.  Hit enter when you \
  165. are done selecting to run the scripts." 17 70 9 ' > $TMP/setupscr
  166.     for script in $ADM_DIR/setup/setup.* ; do
  167.       BLURB=`grep '#BLURB' $script | cut -b8-`
  168.       if [ "$BLURB" = "" ]; then
  169.         BLURB=""""
  170.       fi
  171.       echo " "`basename $script | cut -f2- -d .`" $BLURB "no" $BLURB \" >> $TMP/setupscr
  172.     done
  173.     echo "2> $TMP/return" >> $TMP/setupscr
  174.     . $TMP/setupscr
  175.     if [ ! "`cat $TMP/return`" = "" ]; then
  176.       # Run each script:
  177.       for script in `cat $TMP/return` ; do
  178.         scrpath=$ADM_DIR/setup/setup.`echo $script | tr -d "`
  179.         rootdevice="`mount | head -1 | cut -f 1 -d ' '`"
  180.         ( color=on ; cd $TARGET_DIR ; . $scrpath / $rootdevice )
  181.       done
  182.     fi
  183.     rm -f $TMP/return $TMP/setupscr
  184.     continue
  185.   fi # end Setup
  186.   if [ "$REPLY" = "View" ]; then
  187.    DEFITEM=""
  188.    export DEFITEM
  189.    dialog --title "SCANNING" --infobox "Please wait while \
  190. Pkgtool scans your system to determine which packages you have \
  191. installed and prepares a list for you." 0 0
  192.    echo 'dialog $DEFITEM --item-help --menu "Please select the package you wish to view." 17 68 10 ' > $TMP/viewscr
  193.    for name in `ls $ADM_DIR/packages` ; do
  194.     pkg_name=`package_name $name`
  195.     BLURB="`sed -n "/$pkg_name:/{s/\"//g;p;q;}" $ADM_DIR/packages/$name | cut -f 2- -d : | crunch`"
  196.     # Let's have some backward compatibility with the interim beta (for now):
  197.     if [ "$BLURB" = "" ]; then
  198.      BLURB="`sed -n "/$name:/{s/\"//g;p;q;}" $ADM_DIR/packages/$name | cut -f 2- -d : | crunch`"
  199.     fi
  200.     echo " "$name" "$BLURB" "View information about package $name" \" >> $TMP/viewscr
  201.    done
  202.    echo "2> $TMP/return" >> $TMP/viewscr
  203.    while [ 0 ]; do
  204.     . $TMP/viewscr
  205.     if [ ! "`cat $TMP/return`" = "" ]; then
  206.      DEFITEM="--default-item `cat $TMP/return`"
  207.      dialog --title "CONTENTS OF PACKAGE: `cat $TMP/return`" --no-shadow --textbox "$ADM_DIR/packages/`cat $TMP/return`" \
  208.      0 0 2> /dev/null
  209.     else
  210.      break
  211.     fi
  212.    done
  213.    rm -f $TMP/return $TMP/viewscr $TMP/tmpmsg
  214.    # This will clean up after most defective packages:
  215.    chmod 755 /
  216.    chmod 1777 /tmp
  217.    continue
  218.   fi  
  219.   if [ "$REPLY" = "Remove" ]; then
  220.    dialog --title "SCANNING" --infobox "Please wait while Pkgtool scans \
  221. your system to determine which packages you have installed and prepares \
  222. a list for you." 0 0
  223.    # end section
  224.    cat << EOF > $TMP/rmscript
  225. dialog --title "SELECT PACKAGES TO REMOVE" --item-help --checklist \
  226. "Please select the \
  227. packages you wish to Remove. Use the \
  228. spacebar to select packages to delete, and the UP/DOWN arrow keys to \
  229. scroll up and down through the entire list." 20 75 11 \\
  230. EOF
  231.    for name in `ls $ADM_DIR/packages` ; do
  232.     pkg_name=`package_name $name`
  233.     BLURB="`sed -n "/$pkg_name:/{s/\"//g;p;q;}" $ADM_DIR/packages/$name | cut -f 2- -d : | crunch`"
  234.     # Let's have some backward compatibility with the interim beta (for now):
  235.     if [ "$BLURB" = "" ]; then
  236.      BLURB="`sed -n "/$name:/{s/\"//g;p;q;}" $ADM_DIR/packages/$name | cut -f 2- -d : | crunch`"
  237.     fi
  238.     echo " "$name" "$BLURB" off "Select/Unselect removing package $name" \" >> $TMP/rmscript
  239.    done
  240.    echo "2> $TMP/return" >> $TMP/rmscript
  241.    if [ -L $LOG -o -r $LOG ]; then
  242.      rm -f $LOG
  243.    fi
  244.    cat /dev/null > $LOG
  245.    chmod 600 $LOG
  246.    chmod 700 $TMP/rmscript
  247.    export ADM_DIR;
  248.    $TMP/rmscript
  249.    remove_packages `cat $TMP/return | tr -d "\042"`
  250.    if [ "`cat $TMP/PKGTOOL.REMOVED`" = "" ]; then
  251.     rm -f $TMP/PKGTOOL.REMOVED
  252.     dialog --title "NO PACKAGES REMOVED" --msgbox "Hit OK to return \
  253. to the main menu." 5 40
  254.    else
  255.     dialog --title "PACKAGE REMOVAL COMPLETE" --msgbox "The packages have \
  256. been removed. A complete log of the files that were removed has been created \
  257. in $TMP: PKGTOOL.REMOVED." 0 0
  258.    fi
  259.    rm -f $TMP/rmscript $TMP/return $TMP/tmpmsg $TMP/SeT*
  260.    chmod 755 /
  261.    chmod 1777 /tmp
  262. # No, return to the main menu:
  263. #   exit
  264.   elif [ "$REPLY" = "Floppy" ]; then
  265.    dialog --title "SELECT FLOPPY DRIVE" --menu "Which floppy drive would \
  266. you like to install from?" \
  267. 11 70 4 \
  268. "/dev/fd0u1440" "1.44 MB first floppy drive" \
  269. "/dev/fd1u1440" "1.44 MB second floppy drive" \
  270. "/dev/fd0h1200" "1.2 MB first floppy drive" \
  271. "/dev/fd1h1200" "1.2 MB second floppy drive" 2> $TMP/wdrive
  272.    if [ $? = 1 ]; then
  273.     dialog --clear
  274.     exit
  275.    fi
  276.    SOURCE_DEVICE="`cat $TMP/wdrive`"
  277.    rm -f $TMP/wdrive
  278.    cat << EOF > $TMP/tmpmsg
  279. Enter the names of any disk sets you would like to install.
  280. Separate the sets with a space, like this: a b oi x
  281. To install packages from one disk, hit [enter] without typing
  282. anything.
  283. EOF
  284.    dialog --title "SOFTWARE SELECTION" --inputbox "`cat $TMP/tmpmsg`" 13 70 2> $TMP/sets
  285.    DISK_SETS="`cat $TMP/sets`"
  286.    rm -f $TMP/sets
  287.    if [ "$DISK_SETS" = "" ]; then
  288.     DISK_SETS="disk"
  289.    else
  290.     DISK_SETS=`echo $DISK_SETS | sed 's/ /#/g'`
  291.     DISK_SETS="#$DISK_SETS"
  292.    fi
  293.    break;
  294.   elif [ "$REPLY" = "Other" ]; then
  295.    dialog --title "SELECT SOURCE DIRECTORY" --inputbox "Please enter the name of the directory that you wish to \
  296. install packages from:" 10 50 2> $TMP/pkgdir
  297.    if [ $? = 1 ]; then
  298.     rm -f $TMP/pkgdir $TMP/SeT*
  299.     dialog --clear
  300.     exit
  301.    fi
  302.    SOURCE_DIR="`cat $TMP/pkgdir`"
  303.    SOURCE_MOUNTED="always"
  304.    DISK_SETS="disk"
  305.    chmod 755 $TARGET_DIR
  306.    chmod 1777 $TARGET_DIR/tmp
  307.    rm -f $TMP/pkgdir
  308.    if [ ! -d $SOURCE_DIR ]; then
  309.     dialog --title "DIRECTORY NOT FOUND" --msgbox "The directory you want to \
  310. install from ($SOURCE_DIR) \
  311. does not seem to exist. Please check the directory and then try again." \
  312. 10 50
  313.     dialog --clear
  314.     exit
  315.    fi
  316.    break;
  317.   else # installing from current directory
  318.    SOURCE_MOUNTED="always"
  319.    SOURCE_DIR="$PWD"
  320.    DISK_SETS="disk"
  321.    chmod 755 $TARGET_DIR
  322.    chmod 1777 $TARGET_DIR/tmp
  323.    break;
  324.   fi
  325. done
  326. fi
  327. if [ "$DISK_SETS" = "disk" ]; then
  328. ASK="always"
  329. fi
  330. mount_the_source() {
  331. # is the source supposed to be mounted already?
  332. if [ "$SOURCE_MOUNTED" = "always" ]; then
  333.   # The source should already be mounted, so we test it
  334.   if [ ! -d $SOURCE_DIR ]; then # the directory is missing
  335.    cat << EOF > $TMP/tmpmsg
  336. Your source device cannot be accessed properly.
  337. Please be sure that it is mounted on $SOURCE_DIR,
  338. and that the Slackware disks are found in subdirectories
  339. of $SOURCE_DIR like specified.
  340. EOF
  341.    dialog --title "MOUNT ERROR" --msgbox "`cat $TMP/tmpmsg`" 11 67
  342.    rm -f $TMP/tmpmsg
  343.    exit 1;
  344.   fi
  345.   return 0;
  346. fi
  347. dialog --title "INSERT DISK" --menu "Please insert disk $1 and \
  348. press ENTER to continue." \
  349. 11 50 3 \
  350. "Continue" "Continue with the installation" \
  351. "Skip" "Skip the current disk series" \
  352. "Quit" "Abort the installation process" 2> $TMP/reply
  353. if [ ! $? = 0 ]; then
  354.   REPLY="Quit"
  355. else
  356.   REPLY="`cat $TMP/reply`"
  357. fi
  358. rm -f $TMP/reply
  359. if [ "$REPLY" = "Skip" ]; then
  360.   return 1;
  361. fi
  362. if [ "$REPLY" = "Quit" ]; then
  363.    dialog --title "ABORTING" --msgbox "Aborting software installation." 5 50
  364.    chmod 755 $TARGET_DIR
  365.    chmod 1777 $TARGET_DIR/tmp
  366.    exit 1;
  367. fi;
  368. # Old line:
  369. # mount -r -t msdos $SOURCE_DEVICE $SOURCE_DIR
  370. # New ones: (thanks to Andy Schwierskott!)
  371. go_on=y
  372. not_successfull_mounted=1
  373. while [ "$go_on" = y -a "$not_successfull_mounted" = 1 ]; do
  374.   mount -r -t msdos $SOURCE_DEVICE $SOURCE_DIR
  375.   not_successfull_mounted=$?
  376.   if [ "$not_successfull_mounted" = 1 ]; then
  377.    mount_answer=x
  378.    while [ "$mount_answer" != "y" -a "$mount_answer" != "q" ] ; do
  379.     dialog --title "MOUNT PROBLEM" --menu "Media was not successfully \
  380. mounted! Do you want to \
  381. retry, or quit?" 10 60 2 \
  382. "Yes" "Try to mount the disk again" \
  383. "No" "No, abort." 2> $TMP/mntans
  384.     mount_answer="`cat $TMP/mntans`"
  385.     rm -f $TMP/mntans
  386.     if [ "$mount_answer" = "Yes" ]; then
  387.      mount_answer="y"
  388.     else
  389.      mount_answer="q"
  390.     fi
  391.    done
  392.    go_on=$mount_answer
  393.   fi
  394. done
  395. test $not_successfull_mounted = 0
  396. }
  397. umount_the_source() {
  398. if [ ! "$SOURCE_MOUNTED" = "always" ]; then
  399.   umount $SOURCE_DEVICE 1> /dev/null 2>&1
  400. fi;
  401. }
  402. install_disk() {
  403. mount_the_source $1
  404. if [ $? = 1 ]; then
  405.   umount_the_source;
  406.   return 1;
  407. fi
  408. CURRENT_DISK_NAME="$1"
  409. PACKAGE_DIR=$SOURCE_DIR
  410. if [ "$SOURCE_MOUNTED" = "always" -a ! "$DISK_SETS" = "disk" ]; then
  411.    PACKAGE_DIR=$PACKAGE_DIR/$1
  412. fi
  413. # If this directory is missing or contains no *.tgz files, bail.
  414. if [ ! -d $PACKAGE_DIR ]; then
  415.   return 1
  416. fi
  417. if ls $PACKAGE_DIR/*.tgz 1> /dev/null 2> /dev/null ; then
  418.   true
  419. else
  420.   return 1
  421. fi
  422. #
  423. # look for tagfile for this series and copy into $TMP/tagfile
  424. #
  425. touch $TMP/tagfile
  426. if [ ! "$DISK_SETS" = "disk" ]; then
  427.   if [ -r $TMP/SeTtagext ]; then
  428.    if [ -r $PACKAGE_DIR/tagfile`cat $TMP/SeTtagext` ]; then
  429.     cat $PACKAGE_DIR/tagfile`cat $TMP/SeTtagext` >> $TMP/tagfile
  430.    else
  431.     if [ -r $PACKAGE_DIR/tagfile ]; then
  432.      cat $PACKAGE_DIR/tagfile >> $TMP/tagfile
  433.     fi
  434.    fi
  435.   #
  436.   # Do we need to follow a custom path to the tagfiles?
  437.   #
  438.   elif [ -r $TMP/SeTtagpath ]; then
  439.    custom_path=`cat $TMP/SeTtagpath`
  440.    short_path=`basename $PACKAGE_DIR`
  441.    # If tagfile exists at the specified custom path, copy it over.
  442.    if [ -r $custom_path/$short_path/tagfile ]; then
  443.     cat $custom_path/$short_path/tagfile >> $TMP/tagfile
  444.    else # well, I guess we'll use the default one then.
  445.     if [ -r $PACKAGE_DIR/tagfile ]; then
  446.      cat $PACKAGE_DIR/tagfile >> $TMP/tagfile
  447.     fi
  448.    fi
  449.   #
  450.   # We seem to be testing for this too often... maybe this code should
  451.   # be optimized a little...
  452.   #
  453.   elif [ -r $PACKAGE_DIR/tagfile ]; then
  454.    cat $PACKAGE_DIR/tagfile >> $TMP/tagfile
  455.   fi
  456.   #
  457.   # Execute menus if in QUICK mode:
  458.   #
  459.   if [ -r $TMP/SeTQUICK -a -r $PACKAGE_DIR/maketag ]; then
  460.    if [ ! "$MAKETAG" = "" -a -r $PACKAGE_DIR/$MAKETAG ]; then # use alternate maketag
  461.     sh $PACKAGE_DIR/$MAKETAG
  462.    else   
  463.     sh $PACKAGE_DIR/maketag
  464.    fi
  465.    if [ -r $TMP/SeTnewtag ]; then
  466.     mv $TMP/SeTnewtag $TMP/tagfile
  467.    fi
  468.   fi
  469.   #
  470.   # Protect tagfile from hacker attack:
  471.   #
  472.   if [ -r $TMP/tagfile ]; then
  473.    chmod 600 $TMP/tagfile
  474.   fi
  475. fi #  ! "$DISK_SETS" = "disk"
  476. # It's possible that the tagfile was specified on the command line.  If that's
  477. # the case, then we'll just override whatever we figured out up above.
  478. if [ ! "$USETAG" = "" ]; then
  479.    cat $USETAG > $TMP/tagfile
  480. fi
  481. # If there's a catalog file present, use it to check for missing files.
  482. # If not, forget about that and install whatever's there.
  483. if [ "$1" = "single_disk" -o -r $PACKAGE_DIR/disk$1 -o -r $PACKAGE_DIR/package-list.txt ]; then
  484.   if [ -r $PACKAGE_DIR/package-list.txt ]; then
  485.    CATALOG_FILE=$PACKAGE_DIR/package-list.txt
  486.   else
  487.    CATALOG_FILE=`basename $PACKAGE_DIR/disk*`;
  488.   fi
  489.   if [ -r $PACKAGE_DIR/$CATALOG_FILE -a ! -d $PACKAGE_DIR/$CATALOG_FILE ]; then
  490.    if grep CONTENTS: $PACKAGE_DIR/$CATALOG_FILE 1> /dev/null 2>&1 ; then
  491.     # First we check for missing packages...
  492.     for PKGTEST in `grep "^CONTENTS:" $PACKAGE_DIR/$CATALOG_FILE | cut -f2- -d : 2> /dev/null` ; do
  493.      # This is not a perfect test.  (say emacs is missing but emacs-nox is not)
  494.      if ls $PACKAGE_DIR/$PKGTEST*.tgz 1> /dev/null 2> /dev/null ; then # found something like it
  495.       true
  496.      else
  497.       cat << EOF > $TMP/tmpmsg
  498. WARNING!!!
  499. While looking through your index file ($CATALOG_FILE),
  500. I noticed that you might be missing a package:
  501. $PKGTEST-\*-\*-\*.tgz
  502. that is supposed to be on this disk (disk $1). You may go
  503. on with the installation if you wish, but if this is a
  504. crucial file I'm making no promises that your machine will
  505. boot.
  506. EOF
  507.       dialog --title "FILE MISSING FROM YOUR DISK" --msgbox \
  508. "`cat $TMP/tmpmsg`" 17 67
  509.      fi
  510.     done # checking for missing packages
  511.     # Now we test for extra packages:
  512.     ALLOWED="`grep CONTENTS: $PACKAGE_DIR/$CATALOG_FILE | cut -b10- 2> /dev/null`"
  513.     for PACKAGE_FILENAME in $PACKAGE_DIR/*.tgz; do
  514.      BASE="`basename $PACKAGE_FILENAME .tgz`"
  515.      BASE="`package_name $BASE`"
  516.      if echo $ALLOWED | grep $BASE 1> /dev/null 2>&1 ; then
  517.       true
  518.      else
  519.       cat << EOF > $TMP/tmpmsg
  520. WARNING!!!
  521. While looking through your index file ($CATALOG_FILE),
  522. I noticed that you have this extra package:
  523. ($BASE.tgz)
  524. that I don't recognize. Please be sure this package is
  525. really supposed to be here, and is not left over from an
  526. old version of Slackware. Sometimes this can happen at the
  527. archive sites.
  528. EOF
  529.       dialog --title "EXTRA FILE FOUND ON YOUR DISK" \
  530. --msgbox "`cat $TMP/tmpmsg`" 17 67
  531.       rm -f $TMP/tmpmsg
  532.      fi
  533.     done
  534.    fi
  535.   fi
  536. fi # check for missing/extra packages
  537. # Install the packages:
  538. for PACKAGE_FILENAME in $PACKAGE_DIR/*.tgz; do
  539.   if [ "$PACKAGE_FILENAME" = "$PACKAGE_DIR/*.tgz" ]; then
  540.    continue;
  541.   fi
  542.   if [ "$ASK" = "never" ]; then # install the package
  543.    installpkg -root $TARGET_DIR -infobox -tagfile $TMP/tagfile $PACKAGE_FILENAME
  544.    ERROR=$?
  545.   elif [ "$ASK" = "tagfiles" ]; then
  546.    installpkg -root $TARGET_DIR -menu -tagfile $TMP/tagfile $PACKAGE_FILENAME
  547.    ERROR=$?
  548.   else # ASK should be = always here, and that's how we'll treat it
  549.    installpkg -root $TARGET_DIR -menu -ask -tagfile $TMP/tagfile $PACKAGE_FILENAME
  550.    ERROR=$?
  551.   fi
  552.   # Check for abort:
  553.   if [ "$ERROR" = "99" ]; then
  554.    umount_the_source;
  555.    chmod 755 $TARGET_DIR
  556.    chmod 1777 $TARGET_DIR/tmp
  557.    exit 1;
  558.    fi
  559. done
  560. OUTTAHERE="false"
  561. if [ -r $PACKAGE_DIR/install.end ]; then
  562.   OUTTAHERE="true"
  563. fi
  564. umount_the_source;
  565. if [ "$OUTTAHERE" = "true" ]; then
  566.   return 1;
  567. fi
  568. }
  569. install_disk_set() { # accepts one argument: the series name in lowercase.
  570. SERIES_NAME=$1
  571. CURRENT_DISK_NUMBER="1";
  572. while [ 0 ]; do
  573.   # Don't start numbering the directories until 2:
  574.   if [ $CURRENT_DISK_NUMBER = 1 ]; then
  575.     DISKTOINSTALL=$SERIES_NAME
  576.   else
  577.     DISKTOINSTALL=$SERIES_NAME$CURRENT_DISK_NUMBER
  578.   fi
  579.   install_disk $DISKTOINSTALL
  580.   if [ ! $? = 0 ]; then # install.end was found, or the user chose
  581.         # to quit installing packages.
  582.    return 0;
  583.   fi
  584.   CURRENT_DISK_NUMBER=`expr $CURRENT_DISK_NUMBER + 1`
  585. done;
  586. }
  587. # /* main() */
  588. if [ "$DISK_SETS" = "disk" ]; then
  589. install_disk single_disk;
  590. ASK="always"
  591. else
  592. touch $TMP/tagfile
  593. chmod 600 $TMP/tagfile
  594. if echo $DISK_SETS | grep "#a#" 1> /dev/null 2>&1; then
  595.   A_IS_NEEDED="true"
  596. else
  597.   A_IS_NEEDED="false"
  598. fi
  599. while [ 0 ];
  600. do
  601.   while [ 0 ]; # strip leading '#'s
  602.   do
  603.    if [ "`echo $DISK_SETS | cut -b1`" = "#" ]; then
  604.     DISK_SETS="`echo $DISK_SETS | cut -b2-`"
  605.    else
  606.     break;
  607.    fi
  608.   done
  609.   if [ "$A_IS_NEEDED" = "true" ]; then
  610.    cat << EOF > $TMP/tmpmsg
  611. --- Installing package series ==>a<==
  612. EOF
  613.    dialog --infobox "`cat $TMP/tmpmsg`" 5 45
  614.    sleep 1
  615.    rm -f $TMP/tmpmsg
  616.    install_disk_set a;
  617.    A_IS_NEEDED="false"
  618.   fi
  619.   count="1"
  620.   if [ "`echo $DISK_SETS | cut -b$count`" = "" ]; then
  621.    break; # we be done here :^)
  622.   else
  623.    count="2"
  624.    while [ 0 ]; do
  625.     if [ "`echo $DISK_SETS | cut -b$count`" = "" -o "`echo $DISK_SETS | cut -b$count`" = "#" ]; then
  626.      count="`expr $count - 1`"
  627.      break;
  628.     else
  629.      count="`expr $count + 1`"
  630.     fi
  631.    done
  632.   fi
  633.   diskset="`echo $DISK_SETS | cut -b1-$count`"
  634.   count="`expr $count + 1`"
  635.   DISK_SETS="`echo $DISK_SETS | cut -b$count-`"
  636.   if [ "$diskset" = "a" ]; then
  637.    continue; # we expect this to be done elsewhere
  638.   fi
  639.   cat << EOF > $TMP/tmpmsg
  640. Installing package series ==>$diskset<==
  641. EOF
  642.   dialog --infobox "`cat $TMP/tmpmsg`" 5 45
  643.   sleep 1
  644.   rm -f $TMP/tmpmsg
  645.   install_disk_set $diskset;
  646. done
  647. fi
  648. if [ "$DISK_SETS" = "disk" -o "$CMD_START" = "true" ]; then
  649. if [ -r $TMP/tagfile ]; then
  650.   rm $TMP/tagfile
  651. fi
  652. dialog --clear
  653. fi
  654. chmod 755 $TARGET_DIR $TARGET_DIR/var $TARGET_DIR/usr
  655. chmod 1777 $TARGET_DIR/tmp
  656. installpkg
  657. 源码:
  658. #!/bin/sh
  659. # Copyright 1994, 1998, 2000  Patrick Volkerding, Concord, CA, USA
  660. # Copyright 2001  Slackware Linux, Inc., Concord, CA, USA
  661. # All rights reserved.
  662. #
  663. # Redistribution and use of this script, with or without modification, is
  664. # permitted provided that the following conditions are met:
  665. #
  666. # 1. Redistributions of this script must retain the above copyright
  667. #    notice, this list of conditions and the following disclaimer.
  668. #
  669. #  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  670. #  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  671. #  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
  672. #  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  673. #  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  674. #  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  675. #  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  676. #  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  677. #  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  678. #  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  679. #
  680. # Sun Nov 26 12:38:25 CST 1995
  681. # Added patch from Glenn Moloney <glenn@physics.unimelb.edu.au> to allow
  682. # packages to be installed to directories other than /.
  683. #
  684. # Wed Mar 18 15:15:51 CST 1998
  685. # Changed $TMP directory to /var/log/setup/tmp, and chmod'ed it 700 to close
  686. # some security holes.
  687. # If installpkg encounters a problem, it will return a non-zero error code.
  688. # If it finds more than one problem (i.e. with a list of packages) you'll only
  689. # hear about the most recent one.
  690. # 1 = tar returned error code
  691. # 2 = failed 'gzip -l package'
  692. # 3 = does not end in .tgz
  693. # 4 = not a file
  694. # 99 = user abort from menu mode
  695. EXITSTATUS=0
  696. TAR=tar-1.13
  697. $TAR --help 1> /dev/null 2> /dev/null
  698. if [ ! $? = 0 ]; then
  699.   TAR=tar
  700. fi
  701. if [ ! "`LC_MESSAGES=C $TAR --version`" = "tar (GNU tar) 1.13
  702. Copyright (C) 1988, 92,93,94,95,96,97,98, 1999 Free Software Foundation, Inc.
  703. This is free software; see the source for copying conditions.  There is NO
  704. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  705. Written by John Gilmore and Jay Fenlason." ]; then
  706.   echo "WARNING: pkgtools are unstable with tar > 1.13."
  707.   echo "         You should provide a "tar-1.13" in your \$PATH."
  708.   sleep 5
  709. fi
  710. usage() {
  711. cat << EOF
  712. Usage: installpkg [options] package_name
  713. Installpkg is used to install a .tgz package like this:
  714.    installpkg xf_bin.tgz
  715. options:      -warn (warn if files will be overwritten, but do not install)
  716.               -root /mnt (install someplace else, like /mnt)
  717.               -infobox (use dialog to draw an info box)
  718.               -menu (confirm package installation with a menu, unless
  719.                     the priority is [required] or ADD)
  720.               -ask (used with menu mode: always ask if a package should be
  721.                    installed regardless of what the package's priority is)
  722.               -priority ADD|REC|OPT|SKP  (provide a priority for the entire
  723.                     package list to use instead of the priority in the
  724.                     tagfile)
  725.               -tagfile /somedir/tagfile (specify a different file to use
  726.                     for package priorities.  The default is "tagfile" in
  727.                     the package's directory)
  728. EOF
  729. }
  730. # Eliminate whitespace function:
  731. crunch() {
  732.   while read FOO ; do
  733.     echo $FOO
  734.   done
  735. }
  736. package_name() {
  737.   STRING=`basename $1 .tgz`
  738.   # Check for old style package name with one segment:
  739.   if [ "`echo $STRING | cut -f 1 -d -`" = "`echo $STRING | cut -f 2 -d -`" ]; then
  740.     echo $STRING
  741.   else # has more than one dash delimited segment
  742.     # Count number of segments:
  743.     INDEX=1
  744.     while [ ! "`echo $STRING | cut -f $INDEX -d -`" = "" ]; do
  745.       INDEX=`expr $INDEX + 1`
  746.     done
  747.     INDEX=`expr $INDEX - 1` # don't include the null value
  748.     # If we don't have four segments, return the old-style (or out of spec) package name:
  749.     if [ "$INDEX" = "2" -o "$INDEX" = "3" ]; then
  750.       echo $STRING
  751.     else # we have four or more segments, so we'll consider this a new-style name:
  752.       NAME=`expr $INDEX - 3`
  753.       NAME="`echo $STRING | cut -f 1-$NAME -d -`"
  754.       echo $NAME
  755.       # cruft for later
  756.       #VER=`expr $INDEX - 2`
  757.       #VER="`echo $STRING | cut -f $VER -d -`"
  758.       #ARCH=`expr $INDEX - 1`
  759.       #ARCH="`echo $STRING | cut -f $ARCH -d -`"
  760.       #BUILD="`echo $STRING | cut -f $INDEX -d -`"
  761.     fi
  762.   fi
  763. }
  764. # Parse options:
  765. MODE=install # standard text-mode
  766. while [ 0 ]; do
  767.   if [ "$1" = "-warn" ]; then
  768.     MODE=warn
  769.     shift 1
  770.   elif [ "$1" = "-infobox" ]; then
  771.     MODE=infobox
  772.     shift 1
  773.   elif [ "$1" = "-menu" ]; then
  774.     MODE=menu
  775.     shift 1
  776.   elif [ "$1" = "-ask" ]; then
  777.     ALWAYSASK="yes"
  778.     shift 1
  779.   elif [ "$1" = "-tagfile" ]; then
  780.     if [ -r "$2" ]; then
  781.       USERTAGFILE="$2"
  782.     elif [ -r "`pwd`/$2" ]; then
  783.       USERTAGFILE="`pwd`/$2"
  784.     else
  785.       usage
  786.       exit
  787.     fi
  788.     shift 2
  789.   elif [ "$1" = "-priority" ]; then
  790.     if [ "$2" = "" ]; then
  791.       usage
  792.       exit
  793.     fi
  794.     USERPRIORITY="$2"
  795.     shift 2
  796.   elif [ "$1" = "-root" ]; then
  797.     if [ "$2" = "" ]; then
  798.       usage
  799.       exit
  800.     fi
  801.     ROOT="$2"
  802.     shift 2
  803.   else
  804.     break
  805.   fi
  806. done
  807. # Set the prefix for the package database directories (packages, scripts).
  808. ADM_DIR="$ROOT/var/log"
  809. # If the directories don't exist, "initialize" the package database:
  810. for PKGDBDIR in packages removed_packages removed_scripts scripts setup ; do
  811.   if [ ! -d $ADM_DIR/$PKGDBDIR ]; then
  812.     rm -rf $ADM_DIR/$PKGDBDIR # make sure it's not a symlink or something stupid
  813.     mkdir -p $ADM_DIR/$PKGDBDIR
  814.     chmod 755 $ADM_DIR/$PKGDBDIR
  815.   fi
  816. done
  817. # Make sure there's a proper temp directory:
  818. TMP=$ADM_DIR/setup/tmp
  819. # If the $TMP directory doesn't exist, create it:
  820. if [ ! -d $TMP ]; then
  821.   rm -rf $TMP # make sure it's not a symlink or something stupid
  822.   mkdir -p $TMP
  823.   chmod 700 $TMP # no need to leave it open
  824. fi
  825. # usage(), exit if called with no arguments:
  826. if [ $# = 0 ]; then
  827.   usage;
  828.   exit
  829. fi
  830. # If -warn mode was requested, produce the output and then exit:
  831. if [ "$MODE" = "warn" ]; then
  832.   while [ -f "$1" ]; do
  833.     echo "#### Scanning the contents of $1..."
  834.     mkdir -p $TMP/scan$$
  835.     ( cd $TMP/scan$$ ; $TAR xzf - install ) < $1 2> /dev/null
  836.     if [ -r $TMP/scan$$/install/doinst.sh ]; then
  837.       if cat $TMP/scan$$/install/doinst.sh | grep ' rm -rf ' 1>/dev/null 2>/dev/null ; then
  838.         cat $TMP/scan$$/install/doinst.sh | grep ' rm -rf ' > $TMP/scan$$/install/delete
  839.         echo "The following locations will be completely WIPED OUT to allow symbolic"
  840.         echo "links to be made. (We're talking 'rm -rf') These locations may be files,"
  841.         echo "or entire directories.  Be sure you've backed up anything at these"
  842.         echo "locations that you want to save before you install this package:"
  843.         cat $TMP/scan$$/install/delete | cut -f 3,7 -d ' ' | tr ' ' '/'
  844.       fi
  845.       if [ -d $TMP/scan$$ ]; then
  846.         ( cd $TMP/scan$$ ; rm -rf install ) 2> /dev/null
  847.         ( cd $TMP ; rmdir scan$$ ) 2> /dev/null
  848.       fi
  849.     fi
  850.     echo "The following files will be overwritten when installing this package."
  851.     echo "Be sure they aren't important before you install this package:"
  852.     ( $TAR tzvvf - ) < $1 | grep -v 'drwx'
  853.     echo
  854.     shift 1
  855.   done
  856.   exit
  857. fi
  858. # Main loop:
  859. for package in $* ; do
  860.   # If someone left off the .tgz, try to figure that out:
  861.   if [ ! -r "$package" -a -r "$package.tgz" ]; then
  862.     package=$package.tgz
  863.   fi
  864.   # "shortname" isn't really THAT short... it's just the full name without ".tgz"
  865.   shortname="`basename $package .tgz`"
  866.   packagedir="`dirname $package`"
  867.   # This is the base package name, used for grepping tagfiles and descriptions:
  868.   packagebase="`package_name $shortname`"
  869.   # Reject package if it does not end in '.tgz':
  870.   if [ ! -r "`dirname $package`/$shortname.tgz" ]; then
  871.     EXITSTATUS=3
  872.     if [ "$MODE" = "install" ]; then
  873.       echo "Cannot install $package: package does not end in .tgz"
  874.     fi
  875.     continue;
  876.   fi
  877.   # Determine package's priority:
  878.   unset PRIORITY
  879.   if [ "$USERPRIORITY" = "" ]; then
  880.     if [ "$USERTAGFILE" = "" ]; then
  881.       TAGFILE="`dirname $package`/tagfile"   
  882.     else
  883.       TAGFILE="$USERTAGFILE"
  884.     fi
  885.     if [ ! -r "$TAGFILE" ]; then
  886.       TAGFILE=/dev/null
  887.     fi
  888.     if grep "^$packagebase:" "$TAGFILE" | grep ADD > /dev/null 2> /dev/null ; then
  889.       PRIORITY="ADD"
  890.     elif grep "^$packagebase:" "$TAGFILE" | grep REC > /dev/null 2> /dev/null ; then
  891.       PRIORITY="REC"
  892.     elif grep "^$packagebase:" "$TAGFILE" | grep OPT > /dev/null 2> /dev/null ; then
  893.       PRIORITY="OPT"
  894.     elif grep "^$packagebase:" "$TAGFILE" | grep SKP > /dev/null 2> /dev/null ; then
  895.       PRIORITY="SKP"
  896.     fi
  897.   else
  898.     PRIORITY="$USERPRIORITY"
  899.   fi
  900.   if [ "$PRIORITY" = "ADD" ]; then
  901.     #PMSG="Priority: [required]"
  902.     PMSG="[required]"
  903.   elif [ "$PRIORITY" = "REC" ]; then
  904.     #PMSG="Priority: [recommended]"
  905.     PMSG="[recommended]"
  906.   elif [ "$PRIORITY" = "OPT" ]; then
  907.     #PMSG="Priority: [optional]"
  908.     PMSG="[optional]"
  909.   elif [ "$PRIORITY" = "SKP" ]; then
  910.     #PMSG="Priority: [skip]"
  911.     PMSG="[skip]"
  912.   else
  913.     #PMSG="Priority: [unknown]"
  914.     PMSG=""
  915.   fi
  916.   # Locate package description file:
  917.   DESCRIPTION="/dev/null"
  918.   # First check the usual locations outside the package, since this is faster:
  919.   for file in $packagedir/disk* $packagedir/package_descriptions $packagedir/$shortname.txt $packagedir/$packagebase.txt ; do
  920.     if grep "^$packagebase:" "$file" 1> /dev/null 2> /dev/null ; then
  921.       DESCRIPTION="$file"
  922.     elif grep "^$shortname:" "$file" 1> /dev/null 2> /dev/null ; then
  923.       DESCRIPTION="$file"
  924.     fi
  925.   done
  926.   # If we still don't have anything, look inside the package.  This requires a costly untar.
  927.   if [ "$DESCRIPTION" = "/dev/null" ]; then
  928.     mkdir -p $TMP/scan$$
  929.     ( cd $TMP/scan$$ ; $TAR xzf - install ) < $package 2> /dev/null
  930.     if grep "^$packagebase:" "$TMP/scan$$/install/slack-desc" 1> /dev/null 2> /dev/null ; then
  931.       DESCRIPTION="$TMP/scan$$/install/slack-desc"
  932.     elif grep "^$shortname:" "$TMP/scan$$/install/slack-desc" 1> /dev/null 2> /dev/null ; then
  933.       DESCRIPTION="$TMP/scan$$/install/slack-desc"
  934.     fi
  935.   fi
  936.   # Simple package integrity check:
  937.   if [ ! -f $package ]; then
  938.     EXITSTATUS=4
  939.     if [ "$MODE" = "install" ]; then
  940.       echo "Cannot install $package: package is not a regular file"
  941.     fi
  942.     continue;
  943.   fi
  944.   gzip -l $package 1> /dev/null 2> /dev/null
  945.   if [ ! "$?" = "0" ]; then
  946.     EXITSTATUS=2 # failed gzip -l
  947.     if [ "$MODE" = "install" ]; then
  948.       echo "Cannot install $package: package is corrupt (failed 'gzip -l $package')"
  949.     fi
  950.     continue;
  951.   fi
  952.   # Collect the package information into a temp file:
  953.   COMPRESSED=`gzip -l $package | grep -v uncompressed_name | crunch | cut -f 1 -d ' '`
  954.   UNCOMPRESSED=`gzip -l $package | grep -v uncompressed_name | crunch | cut -f 2 -d ' '`
  955.   COMPRESSED="`expr $COMPRESSED / 1024` K"
  956.   UNCOMPRESSED="`expr $UNCOMPRESSED / 1024` K"
  957. #  MD5SUM=`md5sum $package | cut -f 1 -d ' '`
  958.   cat $DESCRIPTION | grep "^$packagebase:" | cut -f 2- -d : | cut -b2- 1> $TMP/tmpmsg$$ 2> /dev/null
  959.   if [ "$shortname" != "$packagebase" ]; then
  960.     cat $DESCRIPTION | grep "^$shortname:" | cut -f 2- -d : | cut -b2- 1>> $TMP/tmpmsg$$ 2> /dev/null
  961.   fi
  962.   # Adjust the length here.  This allows a slack-desc to be any size up to 13 lines instead of fixed at 11.
  963.   LENGTH=`cat $TMP/tmpmsg$$ | wc -l`
  964.   while [ $LENGTH -lt 12 ]; do
  965.     echo >> $TMP/tmpmsg$$
  966.     LENGTH=`expr $LENGTH + 1`
  967.   done
  968.   echo "Size: Compressed: $COMPRESSED, uncompressed: $UNCOMPRESSED." >> $TMP/tmpmsg$$
  969.   # For recent versions of dialog it is necessary to add \n to the end of each line
  970.   # or it will remove repeating spaces and mess up our careful formatting:
  971.   cat << EOF > $TMP/controlns$$
  972. \n
  973. \n
  974. \n
  975. \n
  976. \n
  977. \n
  978. \n
  979. \n
  980. \n
  981. \n
  982. \n
  983. \n
  984. \n
  985. EOF
  986.   paste -d "" $TMP/tmpmsg$$ $TMP/controlns$$ > $TMP/pasted$$
  987.   rm -f $TMP/controlns$$
  988.   mv $TMP/pasted$$ $TMP/tmpmsg$$
  989.   # Emit information to the console:
  990.   if [ "$MODE" = "install" ]; then
  991.     if [ "$PMSG" = "" ]; then
  992.       echo "Installing package $shortname... "
  993.     else
  994.       echo "Installing package $shortname ($PMSG)... "
  995.     fi
  996.     echo "PACKAGE DESCRIPTION:"
  997.     cat $DESCRIPTION | grep "^$packagebase:" | uniq
  998.     if [ "$shortname" != "$packagebase" ]; then
  999.       cat $DESCRIPTION | grep "^$shortname:" | uniq
  1000.     fi
  1001.   elif [ "$MODE" = "infobox" -a ! "$PRIORITY" = "SKP" ]; then # install non-SKP infobox package
  1002.     dialog --title "Installing package ==>$shortname<== $PMSG" --infobox "`cat $TMP/tmpmsg$$`" 0 0
  1003.   elif [ "$MODE" = "menu" -a "$PRIORITY" = "ADD" -a ! "$ALWAYSASK" = "yes" ]; then # ADD overrides menu mode unless -ask was used
  1004.     dialog --title "Installing package ==>$shortname<== $PMSG" --infobox "`cat $TMP/tmpmsg$$`" 0 0
  1005.   elif [ "$MODE" = "menu" -a "$PRIORITY" = "SKP" -a ! "$ALWAYSASK" = "yes" ]; then # SKP overrides menu mode unless -ask used
  1006.     rm -f $TMP/tmpmsg$$
  1007.     continue # next package
  1008.   elif [ "$MODE" = "infobox" -a "$PRIORITY" = "SKP" -a ! "$ALWAYSASK" = "yes" ]; then # SKP overrides infobox mode, too
  1009.     rm -f $TMP/tmpmsg$$
  1010.     continue
  1011.   else # we must need a full menu:
  1012.     dialog --title "Package Name: ==>$shortname<== $PMSG" --menu "`cat $TMP/tmpmsg$$`" 0 0 3 \
  1013.     "Yes" "Install package $shortname" \
  1014.     "No" "Do not install package $shortname" \
  1015.     "Quit" "Abort software installation completely" 2> $TMP/reply$$
  1016.     if [ ! $? = 0 ]; then
  1017.       echo "No" > $TMP/reply$$
  1018.     fi
  1019.     REPLY="`cat $TMP/reply$$`"
  1020.     rm -f $TMP/reply$$ $TMP/tmpmsg$$
  1021.     if [ "$REPLY" = "Quit" ]; then
  1022.       exit 99 # EXIT STATUS 99 = ABORT!
  1023.     elif [ "$REPLY" = "No" ]; then
  1024.       continue # skip the package
  1025.     fi
  1026.   fi
  1027.   # Test tarball integrity, and make sure we're not installing files on top of existing symbolic links:
  1028.   $TAR tzf $package 1> $TMP/tmplist$$ 2> /dev/null
  1029.   TARERROR=$?
  1030.   if [ ! "$TARERROR" = "0" ]; then
  1031.     EXITSTATUS=1 # tar file corrupt
  1032.     if [ "$MODE" = "install" ]; then
  1033.       echo "Unable to install $package: tar archive is corrupt (tar returned error code $TARERROR)"
  1034.     fi
  1035.     rm -f $TMP/tmplist$$
  1036.     continue
  1037.   fi
  1038.   cat $TMP/tmplist$$ | grep -v "/$" | while read file ; do
  1039.     if [ -L "$ROOT/$file" ]; then
  1040.       rm -f "$ROOT/$file"
  1041.     fi
  1042.   done
  1043.   rm -f $TMP/tmplist$$
  1044.   # Write the package file database entry and install the package:
  1045.   echo "PACKAGE NAME:     $shortname" > $ADM_DIR/packages/$shortname
  1046.   echo "COMPRESSED PACKAGE SIZE:     $COMPRESSED" >> $ADM_DIR/packages/$shortname
  1047.   echo "UNCOMPRESSED PACKAGE SIZE:     $UNCOMPRESSED" >> $ADM_DIR/packages/$shortname
  1048.   echo "PACKAGE LOCATION: $package" >> $ADM_DIR/packages/$shortname
  1049. #  echo "PACKAGE MD5SUM: $MD5SUM" >> $ADM_DIR/packages/$shortname
  1050.   echo "PACKAGE DESCRIPTION:" >> $ADM_DIR/packages/$shortname
  1051.   cat $DESCRIPTION | grep "^$packagebase:" >> $ADM_DIR/packages/$shortname 2> /dev/null
  1052.   if [ "$shortname" != "$packagebase" ]; then
  1053.     cat $DESCRIPTION | grep "^$shortname:" >> $ADM_DIR/packages/$shortname 2> /dev/null
  1054.   fi
  1055.   echo "FILE LIST:" >> $ADM_DIR/packages/$shortname
  1056.   ( cd $ROOT/ ; $TAR -xzlUpvf - ) < $package >> $TMP/$shortname 2> /dev/null
  1057.   if [ "`cat $TMP/$shortname | grep '^./' | wc -l | tr -d ' '`" = "1" ]; then
  1058.     # Good.  We have a package that meets the Slackware spec.
  1059.     cat $TMP/$shortname >> $ADM_DIR/packages/$shortname
  1060.   else
  1061.     # Some dumb bunny built a package with something other than makepkg.  Bad!
  1062.     # Oh well.  Bound to happen.  Par for the course.  Fix it and move on...
  1063.     echo './' >> $ADM_DIR/packages/$shortname
  1064.     cat $TMP/$shortname | grep -v '^./$' | cut -b3- >> $ADM_DIR/packages/$shortname
  1065.   fi
  1066.   rm -f $TMP/$shortname
  1067.   if [ -x /sbin/ldconfig ]; then
  1068.     /sbin/ldconfig
  1069.   fi
  1070.   if [ -f $ROOT/install/doinst.sh ]; then
  1071.     if [ "$MODE" = "install" ]; then
  1072.       echo "Executing install script for $shortname..."
  1073.     fi
  1074.     ( cd $ROOT/ ; sh install/doinst.sh -install; )
  1075.   fi
  1076.   # Clean up the mess...
  1077.   if [ -d $ROOT/install ]; then
  1078.     if [ -r $ROOT/install/doinst.sh ]; then
  1079.       cp $ROOT/install/doinst.sh $ADM_DIR/scripts/$shortname
  1080.       chmod 755 $ADM_DIR/scripts/$shortname
  1081.     fi
  1082.     # /install/doinst.sh and /install/slack-* are reserved locations for the package system.
  1083.     ( cd $ROOT/install ; rm -f doinst.sh slack-* 1> /dev/null 2>&1 )
  1084.     rmdir $ROOT/install 1> /dev/null 2>&1
  1085.   fi
  1086.   # If we used a scan directory, get rid of it:
  1087.   if [ -d "$TMP/scan$$" ]; then
  1088.     rm -rf "$TMP/scan$$"
  1089.   fi
  1090.   rm -f $TMP/tmpmsg$$ $TMP/reply$$
  1091.   if [ "$MODE" = "install" ]; then
  1092.     echo
  1093.   fi
  1094. done
  1095. exit $EXITSTATUS
复制代码
 楼主| 发表于 2003-9-8 15:56:36 | 显示全部楼层

一个用阿拉伯数字转换中文大写的脚本(2)[转]

感谢作者: lvgq2001
来自: www.chinaunix.net
  1. #!/bin/ksh
  2. # Program Name : Num2Chn.sh
  3. # Programmer   : 吕永辉(仙桃市信用联社)
  4. # 首先判断输入的是否为数字
  5. printf "%.2f" $1 >/dev/null 2>&1 3>&1 || {
  6.    echo "输入非法!!!";exit
  7. }
  8. # 定义数字数组
  9. Num[0]="零" Num[1]="壹" Num[2]="贰" Num[3]="叁" Num[4]="肆" Num[5]="伍"
  10. Num[6]="陆" Num[7]="柒" Num[8]="捌" Num[9]="玖"
  11. # 定义金额单位数组
  12. Unit[1]="分" Unit[2]="角" Unit[3]="元" Unit[4]="拾" Unit[5]="佰" Unit[6]="仟"
  13. Unit[7]="万" Unit[11]="亿"
  14. # 处理负数的情况(红字)
  15. [ `echo $1 | grep "-"` ] && {
  16. NumStr=`echo $1 | sed 's/^.//'`
  17. RedFlag="(红)"
  18. } || {
  19. NumStr=$1
  20. RedFlag=""
  21. }
  22. # 初始化输入数字数组
  23. i=1
  24. for TmpC in `printf "%.2f" $NumStr | awk -F "." '{print $1$2}' | sed 's/./& /g'`
  25. do
  26.   Nums[$i]=$TmpC
  27.   i=`expr $i + 1`
  28. done
  29. j=${#Nums[@]}       # 为循环计数变量附初值为输入数字的长度
  30. k=1                 # 为循环定位变量附初值(用于确定金额单位)
  31. ChnNum=""           # 初始化大写金额字符串
  32. while
  33.   [ $j -eq 0 ] && break
  34.   [ $k -gt 11 ] && {
  35.     ChnNum=${Num[${Nums[$j]}]}${Unit[`expr $k - 8 `]}$ChnNum
  36.     j=`expr $j - 1`
  37.     k=`expr $k + 1`
  38.     continue
  39. }
  40.   [ $k -gt 7 -a $k -lt 11 ] && {
  41.     ChnNum=${Num[${Nums[$j]}]}${Unit[`expr $k - 4 `]}$ChnNum
  42.     j=`expr $j - 1`
  43.     k=`expr $k + 1`
  44.     continue
  45. }
  46.   ChnNum=${Num[${Nums[$j]}]}${Unit[$k]}$ChnNum
  47. do
  48.   j=`expr $j - 1`
  49.   k=`expr $k + 1`
  50. done
  51. ChnNum=`echo $RedFlag$ChnNum | sed 's/零分/整/' | sed 's/零角/零/g' \
  52.             | sed 's/零拾/零/g' | sed 's/零佰/零/g' | sed 's/零仟/零/g'\
  53.             | sed 's/零整/整/' | sed 's/零零零零万/零/'`
  54. # 下面的循环用于去掉字符串中连续的零
  55. while
  56.   [ `echo $ChnNum | grep "零零"` ] || {
  57.      ChnNum=`echo $ChnNum  | sed 's/零元/元/'| sed 's/零万/万/' \
  58.        | sed 's/零亿/亿/' | sed 's/^元//' | sed 's/^零//' | sed 's/^整$/零元&/`
  59.      break
  60. }
  61. do
  62.   ChnNum=`echo $ChnNum | sed 's/零零/零/g'`
  63. done
  64. echo $ChnNum
复制代码
(调试环境:SCO OpenServer 5.0.5 、ksh )
发表于 2003-9-9 21:15:37 | 显示全部楼层
如果在控制台下没有中文平台,因为local是中文的,所以会出现乱码,在用户目录下的.bashrc文件加上下面一行,可以自动判断是否是控制台,是的话,会把local设为en,可以防止出现乱码,在X下还是中文local,和平常一样。
  1. for tty in /dev/tty[1-6];do test `tty` = $tty&&LANG=en;done
复制代码
发表于 2003-9-9 21:17:24 | 显示全部楼层
一个脚本,用来方便的启动停止一些常用服务,基于对话框的.
  1. #!/bin/bash
  2. #echo server manage
  3. #echo
  4. smsg()
  5. {
  6. dialog --msgbox "         $1 start sucessful" 5 50
  7. }
  8. tmsg()
  9. {
  10. dialog --msgbox "         $1 stop sucessful" 5 50
  11. }
  12. emsg()
  13. {
  14. dialog --msgbox "         something erro " 5 50
  15. }
  16. ##启动服务
  17. start(){
  18. exno=0
  19. while [ $exno -eq 0 ];do
  20. dialog --stdout --ok-label "start" --help-button --help-label "exit" --cancel-label "back" --menu "start server" 0 0 6 1 apache 2 sendmail 3 mysqld 4 wine 5 proftpd 6 sshd >/tmp/dialog_001.tmp
  21. exno=$?
  22. key=`cat /tmp/dialog_001.tmp`
  23. echo "nothing">/tmp/dialog_001.tmp
  24. if [ $exno -eq 1 ];then
  25. main
  26. fi
  27. if [ $exno -eq 2 ];then
  28. exit 0
  29. fi
  30. case $key in
  31.    [1])httpd -k start&&smsg "apache"||emsg;;
  32.    [2])/etc/init.d/sendmail start&&smsg "sendmail"||emsg;;
  33.    [3])/etc/init.d/mysqld start&&smsg "mysqld"||emsg;;
  34.    [4])/etc/init.d/wine start&&smsg "wine"||emsg;;
  35.    [5])proftpd&&smsg "proftpd"||emsg;;
  36.    [6])/etc/init.d/sshd start&&smsg "sshd"||emsg;;   
  37.      *)test
  38. esac
  39. done
  40. }      
  41. ##停止服务
  42. stop(){
  43. exno=0
  44. while [ $exno -eq 0 ];do
  45. dialog --stdout --ok-label "stop" --help-button --help-label "exit" --cancel-label "back" --menu "stop server" 0 0 6 1 apache 2 sendmail 3 mysqld 4 wine 5 proftpd 6 sshd >/tmp/dialog_001.tmp
  46. exno=$?
  47. key=`cat /tmp/dialog_001.tmp`
  48. echo "nothing">/tmp/dialog_001.tmp
  49. if [ $exno -eq 1 ];then
  50. main
  51. fi
  52. if [ $exno -eq 2 ];then
  53. exit 0
  54. fi
  55. case $key in
  56.    [1])httpd -k stop&&tmsg "apache"||emsg;;
  57.    [2])/etc/init.d/sendmail stop&&tmsg "sendmail"||emsg;;
  58.    [3])/etc/init.d/mysqld stop&&tmsg "mysqld"||emsg;;
  59.    [4])/etc/init.d/wine stop&&tmsg "wine"||emsg;;
  60.    [5])killall proftpd&&tmsg "proftpd"||emsg;;
  61.    [6])/etc/init.d/sshd stop&&tmsg "sshd"||emsg;;
  62.      *)test
  63. esac
  64. done
  65. }
  66. ##主函
  67. main(){
  68. exno=0
  69. dialog --stdout --ok-label "start" --cancel-label "stop" --help-button --help-label "exit" --menu "this will start or stop some of your sevice" 4 50 0 "" ""
  70. exno=$?
  71. case $exno in
  72.         0)start;;
  73.         1)stop;;
  74.         *)test
  75. esac
  76. }
  77. main
复制代码
发表于 2003-9-9 21:18:32 | 显示全部楼层
用来方便的启动停止一些常用服务,基于X的。
#!/bin/bash
#echo server manage
#echo

smsg()
{
kdialog --msgbox "$1 启动成功"
}

tmsg()
{
kdialog --msgbox "$1 停止成功"
}

emsg()
{
kdialog --error "出错 "
}

start(){
exno=0
while [ $exno -eq 0 ];do
kdialog --menu "启动服务" 1 apache 2 sendmail 3 mysqld 4 wine 5 proftpd 6 sshd >/tmp/dialog_001.tmp
exno=$?
key=`cat /tmp/dialog_001.tmp`
echo "nothing">/tmp/dialog_001.tmp
if [ $exno -eq 1 ];then
main
fi
if [ $exno -eq 2 ];then
exit 0
fi
case $key in
   [1])httpd -k start&&smsg "apache"||emsg;;
   [2])/etc/init.d/sendmail start&&smsg "sendmail"||emsg;;
   [3])/etc/init.d/mysqld start&&smsg "mysqld"||emsg;;
   [4])/etc/init.d/wine start&&smsg "wine"||emsg;;
   [5])proftpd&&smsg "proftpd"||emsg;;
   [6])/etc/init.d/sshd start&&smsg "sshd"||emsg;;   
     *)test
esac
done
}       

stop(){
exno=0
while [ $exno -eq 0 ];do
kdialog --menu "停止服务" 1 apache 2 sendmail 3 mysqld 4 wine 5 proftpd 6 sshd >/tmp/dialog_001.tmp
exno=$?
key=`cat /tmp/dialog_001.tmp`
echo "nothing">/tmp/dialog_001.tmp
if [ $exno -eq 1 ];then
main
fi
if [ $exno -eq 2 ];then
exit 0
fi
case $key in
   [1])httpd -k stop&&tmsg "apache"||emsg;;
   [2])/etc/init.d/sendmail stop&&tmsg "sendmail"||emsg;;
   [3])/etc/init.d/mysqld stop&&tmsg "mysqld"||emsg;;
   [4])/etc/init.d/wine stop&&tmsg "wine"||emsg;;
   [5])killall proftpd&&tmsg "proftpd"||emsg;;
   [6])/etc/init.d/sshd stop&&tmsg "sshd"||emsg;;
     *)test
esac
done
}


main(){
exno=0
kdialog --yesnocancel "启动服务/停止服务/退出程序"
exno=$?
case $exno in
        0)start;;
        1)stop;;
        *)test
esac
}

main
发表于 2003-9-9 21:19:41 | 显示全部楼层
一个基于对话框的进度条,没实际作用。
function count (){
i=0
while [ $i -lt 100 ]
do echo $i&&sleep 0.1&&i=`expr $i + 1`
done
}
count |dialog --gauge "进度" 12 50
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表