LinuxSir.cn,穿越时空的Linuxsir!

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

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

[复制链接]
发表于 2006-4-5 19:42:21 | 显示全部楼层

一个挂载windows分区的脚本

以前写的,大家可以在修改一下。


  1. #!/bin/sh
  2. # Name : mount.sh
  3. # Usage : sh mount.sh
  4. # Purpose : This script is used to mount Windows disk in your computer      
  5. # Author:  Kong Pengju <Email:kongpengju1983@yahoo.com>
  6. # Date: 2003/06/08

  7. trap "my_exit" 2 3 15

  8. # Below are some functions used in the script

  9. #-----------Check if  "root" run this script----------------------------
  10. is_root()
  11. {
  12. if [ "$LOGNAME" = "root" ]; then :
  13. else
  14.     colour white_red
  15.     echo -e "\tSorry , you don't have the permisson to run this shell script !"
  16.     echo -e "\t\t\tYou must be the root!"
  17.     colour white_blue
  18.     exit 1
  19. fi
  20. }
  21. #---------Some colour used in this script-----------------------------------
  22. colour()
  23. {
  24.   case $1 in
  25.   white_blue)
  26.     echo ''
  27.    ;;
  28.   white_red)
  29.     echo ''
  30.    ;;
  31. esac
  32. }
  33. #------------To process the answer you entered--------------------------------
  34. continue_prompt()
  35. {
  36.   _STR=$1
  37.   _DEFAULT=$2

  38. if [ $# -lt 1 ]; then
  39.    echo "continue_prompt : I need a string to display "
  40.    return 1
  41. fi

  42. while :
  43. do
  44.    echo -n "$_STR [Y...N] [$_DEFAULT]:"
  45.    read _ANS
  46.    
  47.    if [ "$_ANS" = "" ]; then
  48.          return 0
  49.    fi

  50.   case $_ANS in
  51.      y|Y|yes|Yes)   return 0   ;;
  52.      n|N|no|No)     return 1   ;;
  53.      *)             colour white_red
  54.                     echo "Answer either Y or N , use default $_DEFAULT"
  55.                     colour white_blue
  56.                     return 0   ;;
  57.    esac
  58. done
  59. }
  60. #----------exit and remove all the tmp files-------
  61. my_exit()
  62. {
  63.   echo -e "\nNow removing all the tmp files used in this script......"
  64.   rm -f *.$$
  65.   echo -e "Exiting......\n"
  66.   exit 1
  67. }



  68. #-------------------main----------------------------

  69. # check if root run this script , others must be denied .
  70. is_root
  71. colour white_blue
  72. echo -e "\nStarting......\n"

  73. # a tmp file used to restore the "fdisk -l" output

  74. TMPFILE=tmpFile.$$
  75. #HERE=`pwd`/backup.$$
  76. #cp /home/wo/practice/etc/fdiskMy $HERE
  77. #cat < $HERE | grep 'Win.* FAT.*' | awk '{print $1}' > $TMPFILE
  78. fdisk -l | grep 'Win.* FAT.*' | awk '{print $1}' > $TMPFILE
  79. # count how many partitions in your computer by using a "while" loop

  80. COUNTER=0   # the variable to record the number of partitions

  81. while read DATA
  82. do
  83.   COUNTER=`expr "$COUNTER" + 1`
  84. done < $TMPFILE

  85. # tell the user how many partition in his/her disk can be mounted
  86. colour white_red
  87. echo -e "\tThere are  $COUNTER  partition in your Disk can be mounted !\n"
  88. colour white_blue
  89. # mount the filesystems using a "for" loop
  90. ORDER=0                 # the variable to indicate the process
  91. TMPVAR=`cat $TMPFILE`   # a tmp varaable to ouput the data to "for"

  92. for PARA in $TMPVAR
  93. do
  94.   # add 1 to the variable every loop
  95.    
  96.   ORDER=`expr $ORDER + 1`

  97.   # check the user's answer
  98.   if continue_prompt "Now prepare mounting the $ORDER disk--$PARA" "Y"
  99.      then :   # do nothing
  100.   else
  101.      colour white_red
  102.      echo "Now skiping this step ......"
  103.      colour white_blue
  104.      sleep 1
  105.      continue
  106.   fi
  107.    
  108.   echo -n "Please input a name to descript it (it must not contine space) : "
  109.   read ANS

  110. if [ "$ANS" = "" ]
  111. then
  112.     colour white_red
  113.     echo -e "\t\tPlease input the description !"
  114.     colour white_blue
  115.     sleep 1
  116.     my_exit
  117. else :
  118. fi

  119. DEST=/mnt/"$ANS"

  120. # make directory where the filesysterm will be mounted
  121. # and check if the direction is really make
  122.   if mkdir $DEST  >/dev/null 2>&1
  123.      then :
  124.   else
  125.      colour white_red
  126.      echo -e "\tError accured in making direction"
  127.      colour white_blue
  128.      sleep 1
  129.      my_exit
  130.   fi
  131.   
  132.   echo "Mounting $PARA to $DEST......"

  133. # Mount and check if it really mounted
  134. if mount -t vfat $PARA $DEST
  135.   then
  136.       colour white_red
  137.       echo -e "\tMount $PARA Successfully !"
  138.       colour white_blue
  139.   else
  140.       colour white_red
  141.       echo -e "\tMount $PARA Failed !"
  142.       colour white_blue
  143. fi
  144. # ask the user if he/her would like to add the info to the /etc/fstab
  145. if continue_prompt "Would you like to auto mount $PARA when computer start ? " "Y"
  146.   then
  147.     echo -e "$PARA\t\t $DEST\t\t vfat\t defaults,codepage=936,iocharset=cp936\t 0 0" >>/etc/fstab
  148.   else
  149.       colour white_red
  150.       echo "Did not auto mount $PARA !"
  151.       colour white_blue
  152.   fi

  153.   # check if has mounted all the partitions
  154.   if [ "$ORDER" = "$COUNTER" ]; then
  155.      colour white_red
  156.      echo -e "All partitions have been mounted ."
  157.      colour white_blue
  158.      sleep 1
  159.      break
  160.   fi

  161. done
  162. # remove all the tmp files
  163. rm -f *.$$
  164. #-------------------End-of-this-script !-------------------------#
复制代码
回复 支持 反对

使用道具 举报

发表于 2006-4-5 19:46:08 | 显示全部楼层
Post by kys_9138
编写脚本的朋友们:谢谢你们提供的脚本,不过我代表菜鸟们说想说的话:能在脚本里添加注释吗????最好能详细一点,这样有利于想我这样的菜鸟学习。希望你们能考虑考虑...真希望能。
我代表菜鸟们感谢你们。



脚本中一行开头加
  1. #
复制代码

就可以了。
多看些书,入门的推荐
《Linux与Unix Shell编程指南》,机械工业出版社,David Tansley著,徐焱等译
回复 支持 反对

使用道具 举报

发表于 2006-4-22 12:32:14 | 显示全部楼层

一个del/restore脚本

一般的del/undel都是把“删除”的文件移到某一个目录下。
这样就有可能会出现一些跨文件系统移动大文件的情况,而且还可能有文件属性改变的情况。

我这个脚本用的是就地“刨坑埋”的办法。只是将“删除”的文件改个名,并在统一的目录记录下来,当删除记录文件时才真正把文件删掉。

日志目录是$HOME/.trash/
下面是脚本,
del删除文件,做个叫restore的链接,实现恢复。
emptytrash清空回收站。

del

  1. #!/bin/bash
  2. wd="$PWD"
  3. while [ $# != "0" ]
  4. do
  5.         if [ "${1#/}" != "$1" ]
  6.         then
  7.                 file="$1"
  8.         else
  9.                 file="$wd/$1"
  10.         fi
  11.         bname="`basename "$file"`"
  12.         cd "`dirname "$file"`"
  13.         if [ "$PWD" == "$HOME/.trash" ]
  14.         then
  15.                 if ! [ -f "$file" ]
  16.                 then
  17.                         echo "$file" no exit
  18.                         exit
  19.                 fi
  20.                 exec 3<&0
  21.                 exec <"$file"
  22.                 read deldir
  23.                 read delfile
  24.                 read ofile
  25.                 exec <&3-
  26.                 if [ xx"$deldir" == xx ] || [ "${delfile#".del_$ofile"}" == "${delfile}" ]
  27.                 then
  28.                         echo file "$file" Error
  29.                         exit
  30.                 fi
  31.                 if [ `basename $0` == del ]
  32.                 then
  33.                         /bin/rm -fr "$deldir/$delfile"
  34.                         /bin/rm "$bname"
  35.                 elif [ `basename $0` == restore ]
  36.                 then
  37.                         mv -i "$deldir/$delfile" "$deldir/$ofile"
  38.                         if ! [ -a "$deldir/$delfile" ]
  39.                         then
  40.                                 /bin/rm "$bname"
  41.                         fi
  42.                 fi
  43.         elif [ `basename $0` == del ]
  44.         then
  45.                 tl=`date +%F-%H-%M-%S`
  46.                 mv "$bname" ".del_${bname}_$tl" && echo -e "$PWD\n.del_${bname}_$tl\n$bname" > "$HOME/.trash/_${bname}_${tl}.ash"
  47.         fi
  48.         shift
  49. done
复制代码


emptytrash

  1. #!/bin/bash
  2. echo -n 'Empty Trash?(Y/N) '
  3. read ans
  4. if [ "$ans" == "Y" ]
  5. then
  6.         cd $HOME/.trash
  7.         del *
  8. fi
复制代码
回复 支持 反对

使用道具 举报

发表于 2006-7-6 15:11:12 | 显示全部楼层
一个判断网络中存活主机的shell,借鉴网中人的netmash问题

#!/bin/bash
#Author by Luweinet@hhstu.edu.cn
#Using GPL
#you should change hping path and have root private.
#su - root -c "./addr.sh"
netid2=$1
pin=/usr/sbin/hping

netid=`echo $netid2 |awk -F \/ '{print $1}'`
iplist1=/tmp/iplist1
iplist2=/tmp/iplist2
iplist3=/tmp/iplist3
tmp=/tmp/diff

base=`echo $netid | awk -F \. '{print $1,$2,$3}'`
i=`echo $netid |awk -F \. '{print $4}'`
                                                                                                                                      
all=(${@//[!0-9]/ })
[ "${#all[@]}" != "8" ] && {
    echo "Usage: "
    echo "${0##*/} ip.ip.ip.ip/mask.mask.mask.mask"
    exit 1
}
                                                                                                                                      
get_addr () {
    if [ "$1" = "-b" ]; then
          op='|'; op1='^'; arg='255'
          shift
    else
          op='&'
    fi
    unset address
    while [ "$5" ]; do
          num=$(( $1 $op ($5 $op1 $arg) ))
          shift
          address="$address.$num"
    done
}
                                                                                                                                      
get_addr -b ${all[@]}
#broadcast=${address#.}
bt1=`echo ${address#.} | awk -F \. '{print $4}'`
bt=`expr $bt1 - 1 `
#loop while bt < $bt
while [ $i -lt $bt ]
do
    i=`expr $i + 1`
    #echo $i
    ipbase=`echo $netid | awk -F \. '{print $1,$2,$3}'`
    ip=`echo $ipbase $i | tr " " "."`
    echo $ip >> $iplist1
    $pin -c 1 -S -d 32 $ip >> $iplist2 #must echo every ping.
    #echo $ip
done

#if have blank line,means maybe drop by firewall.
cat $iplist2 | sed -n '/=/p' | awk '{print $2}' | awk -F \= '{print $2}' > $iplist3
echo -ne "`diff $iplist1 $iplist3 > /tmp/diff`"

#cat $tmp | sed -n '/ /p' |awk '{print $2,"\t","is NOT alive now"}'
echo -e "##############################################################\r"
echo -e "\r"
echo -ne "!!!This is e result!!!"
echo -e "\r"
cat $tmp | sed -n '/ /p' |awk '{print $2,"\t","is NOT alive"}'
echo -e "\r"
echo -e "##############################################################\r"
#cat $tmp | sed -n '/^\>/p' |awk '{print $2,"\t","is alive"}' #in iplist1,which all ip.

rm -rf $iplist1
rm -rf $iplist2
rm -rf $iplist3
rm -rf $tmp


得有hping,hping -s选项可以快速发icmp包
回复 支持 反对

使用道具 举报

发表于 2006-7-31 00:02:41 | 显示全部楼层
del/restore 的升级版.
改进:
1. 将删除文件移到相应分区的根目录下的回收站目录, 可以防止一些文件被"丢"了.
2. 删除被删除过一次的文件同时也会把日志删掉.

存为del并链接为restore.
  1. #!/bin/bash
  2. if [ "${1#-f}" != "$1" ] && [ `basename $0` == "del" ]
  3. then
  4.         rm "$@"
  5.         exit
  6. fi
  7. exe="`basename $0`"
  8. wd="$PWD"
  9. logdir="$HOME/.trash"
  10. gettrashdir()
  11. {
  12.         dname="`dirname "$file"`"
  13.         pdname="$dname"
  14.         dname="$(readlink -m "$dname")"
  15.         for dir in $HOME `mount|awk '$1 ~ /\/dev\/hd/ {print $3}'|sort -u -r`
  16.         do
  17.                 if [ "${dname#$dir}" != "$dname" ]
  18.                 then
  19.                         trashdir="$dir/.trash-$USER"
  20.                         break
  21.                 fi
  22.         done
  23. }
  24. intrash()
  25. {
  26.                 exec 3<&0
  27.                 exec <"$logdir/$1"
  28.                 read deldir
  29.                 read delfile
  30.                 read odir
  31.                 read ofile
  32.                 exec <&3-
  33.                 if [ xx"$deldir" == xx ] || [ xx"$odir" == xx ] || \
  34.                         [ "${delfile#"_$ofile"}" == "${delfile}" ]
  35.                 then
  36.                         echo file "$logdir$1" Error
  37.                         exit
  38.                 fi
  39.                 if [ "$exe" == del ]
  40.                 then
  41.                         /bin/rm -fr "$deldir/$delfile"
  42.                         /bin/rm "$logdir/$1"
  43.                 elif [ "$exe" == restore ]
  44.                 then
  45.                         if [ ! -d "$odir" ]
  46.                         then
  47.                                 ! mkdir -p "$odir" && exit
  48.                         fi
  49.                         if [ -f "$odir/$ofile" ]
  50.                         then
  51.                                 echo Origin file recreated, backup to $ofile.bak
  52.                                 if [ -f "$odir/$ofile.bak" ]
  53.                                 then
  54.                                         echo last backup is removed
  55.                                         /bin/rm -fr "$odir/$ofile.bak"
  56.                                 fi
  57.                                 mv "$odir/$ofile" "$odir/$ofile.bak"
  58.                         fi
  59.                         mv "$deldir/$delfile" "$odir/$ofile"
  60.                         if ! [ -a "$deldir/$delfile" ]
  61.                         then
  62.                                 /bin/rm "$logdir/$1"
  63.                         fi
  64.                 fi
  65. }
  66. while [ $# != "0" ]
  67. do
  68.         if [ "${1#/}" != "$1" ]
  69.         then
  70.                 file="$1"
  71.         else
  72.                 file="$wd/$1"
  73.         fi
  74.         if ! [ -e "$file" ] && ! [ -h "$file" ]
  75.         then
  76.                 echo "$file" no exit
  77.                 exit
  78.         fi
  79.         bname="`basename "$file"`"
  80.         if [ "$(dirname "$file")" != "$pdname" ]
  81.         then
  82.                 gettrashdir
  83.         fi
  84.         if [ ! -d "$trashdir" ]
  85.         then
  86.                 if ! mkdir -p "$trashdir" 2>/dev/null
  87.                 then
  88.                         echo 'The file '$dname/$bname' will be delete permanently(a,y,s,c)'
  89.                         read -n 1 ans
  90.                         echo
  91.                         case "$ans"xx in
  92.                                 axx) /bin/rm -fr "$@";exit ;;
  93.                                 yxx) /bin/rm -fr "$1";;
  94.                                 sxx) ;;
  95.                                 *) exit ;;
  96.                         esac
  97.                         shift
  98.                         continue
  99.                 fi
  100.         fi
  101.         if [ "$dname/$bname" == "$trashdir" ] ||\
  102.                 [ "$dname/$bname" == "$logdir" ] ||\
  103.                 [ "`readlink -m "$file"`" == "`readlink -m "$0"`" ]
  104.         then
  105.                 echo Are you kidding?
  106.                 exit
  107.         fi
  108.         if [ "$dname" == "$trashdir" ] && [ -f "$logdir/$bname".ash ]
  109.         then
  110.                 intrash "${bname}.ash"
  111.         elif [ "$dname" == "$logdir" ] && [ "${bname%.ash}" != "${bname}" ]
  112.         then
  113.                 intrash "$bname"
  114.         elif [ "$exe" == del ]
  115.         then
  116.                 tl=`date +%F-%H-%M-%S`
  117.                 mv "$dname/$bname" "$trashdir/_${bname}_$tl" && \
  118.                 echo -e "$trashdir\n_${bname}_$tl\n$dname\n$bname" \
  119.                 > "$logdir/_${bname}_${tl}.ash"
  120.         fi
  121.         shift
  122. done
复制代码
lstrash显示回收站的内容
  1. #!/bin/bash
  2. cd ~/.trash
  3. if [ -z "`ls *.ash 2>/dev/null`" ]
  4. then
  5.         echo trash is empty
  6.         exit
  7. fi
  8. awk 'FNR==1{printf("%s/",$0)}
  9.         FNR==2{print $0" :"}
  10.         FNR==3{printf("\t%s/",$0)}
  11.         FNR==4{print $0}' *.ash
复制代码
emptytrash同上.
回复 支持 反对

使用道具 举报

发表于 2006-8-10 19:22:54 | 显示全部楼层

我也发一个!!!

一个检查拼写错误的脚本
#!/bin/bash
#directory.sh
#check the word spelling errors in the given text
E_BADARGS=65
[ $# -lt 2 ]&&echo "Usage:./directory filename1 filename2."&&exit \
$E_BADARGS

echo "###################Mcknight##############################"
path="/usr/share/dict/linux.words"
#modify the text file to suit the search
cat $1|tr A-Z a-z|sed 's/[,!?.]/ /g'|sed 's/  */ /g' >$2

for word in $(cat $2);do
    if [ "$word" == "i" ];then
        continue
    fi                          #donnot want "i" to bother!
    grep -q "\<$word\>" $path
    if [ "$?" -eq 0 ];then
        continue
    else
        echo -e "\033[0;31m$word \033[1;37mspells wrongly!"
    fi
done
echo "###################Check done!###########################"
回复 支持 反对

使用道具 举报

发表于 2006-9-8 16:55:47 | 显示全部楼层
将while ((n<=$2)) 改为 while [ $n -lt $2 ]
回复 支持 反对

使用道具 举报

发表于 2006-9-8 16:59:24 | 显示全部楼层
ping测试的报警
#!/bin/bash
n=1
while [ $n -lt $2 ]; do
ping -c 1  $1 >/dev/null
case $? in
0) echo -n '!' ;;
1) echo -n '.' && echo -ne "\a" ;;
*) exit ;;
esac
((n+=1))
done
echo
保存为pingg 执行./pingg 10.71.56.36 10 如果ping不通,则会报警。
回复 支持 反对

使用道具 举报

发表于 2006-9-14 19:24:53 | 显示全部楼层
我想写一个脚本实现添加用户这个功能:
执行脚本后,能自动添加的用户ID,密码,group的名字,全名. (useraccname:password:groupname:fullname), 而且是要批量添加(一次要自动加5个).用户ID,密码,group的名字,全名.都需要指定好的. 脚本只能被 root 或管理员执行!那位朋友能不能给个例子?多谢!
回复 支持 反对

使用道具 举报

发表于 2006-9-17 10:43:36 | 显示全部楼层
Post by LYOO
不错!用Linux就该多写写脚本,天天想着怎么玩X,不如去装Windows算了

说的很好
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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