几个不错的Shell脚本

几个Shell脚本的例子,觉得还不错。

例子:001判断输入为数字,字符或其他

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #!/bin/bash  
  2. read -p "Enter a number or string here:" input  
  3.   
  4. case $input in  
  5.    [0-9]) echo -e "Good job, Your input is a numberic! \n" ;;  
  6. [a-zA-Z]) echo -e "Good job, Your input is a character! \n" ;;  
  7.        *) echo -e "Your input is wrong, input again!   \n"  ;;  
  8. esac  

例子:002求平均数

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #!/bin/bash  
  2.   
  3. # Calculate the average of a series of numbers.  
  4.   
  5. SCORE="0"  
  6. AVERAGE="0"  
  7. SUM="0"  
  8. NUM="0"  
  9.   
  10. while true; do  
  11.   
  12.   echo -n "Enter your score [0-100%] ('q' for quit): "; read SCORE;  
  13.   
  14.   if (("$SCORE" < "0"))  || (("$SCORE" > "100")); then  
  15.     echo "Be serious.  Common, try again: "  
  16.   elif [ "$SCORE" == "q" ]; then  
  17.     echo "Average rating: $AVERAGE%."  
  18.     break  
  19.   else  
  20.     SUM=$[$SUM + $SCORE]  
  21.     NUM=$[$NUM + 1]  
  22.     AVERAGE=$[$SUM / $NUM]  
  23.   fi  
  24.   
  25. done  
  26.   
  27. echo "Exiting."  

例子:003 】自减输出

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [scriptname: doit.sh]  
  2. while (( $# > 0 ))  
  3. do  
  4.   echo $*  
  5.   shift  
  6. done   
  7.           
  8. /> ./doit.sh a b c d e  
  9. a b c d e  
  10. b c d e  
  11. c d e  
  12. d e  
  13. e  
例子:004 】在文件中添加前缀

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. # 人名列表  
  2. # cat namelist  
  3. Jame  
  4. Bob  
  5. Tom  
  6. Jerry  
  7. Sherry  
  8. Alice  
  9. John  
  10.   
  11. # 脚本程序  
  12. # cat namelist.sh  
  13. #!/bin/bash  
  14. for name in $(cat namelist)  
  15. do  
  16.         echo "name= " $name  
  17. done  
  18. echo "The name is out of namelist file"  
  19.   
  20. # 输出结果  
  21. # ./namelist.sh  
  22. name=  Jame  
  23. name=  Bob  
  24. name=  Tom  
  25. name=  Jerry  
  26. name=  Sherry  
  27. name=  Alice  
  28. name=  John  
例子:005 】批量测试文件是否存在

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [root@host ~]# cat testfile.sh        
  2. #!/bin/bash  
  3.   
  4.   
  5. for file in test*.sh  
  6. do  
  7.   if [ -f $file ];then  
  8.      echo "$file existed."  
  9.   fi  
  10. done  
  11.   
  12. [root@host ~]# ./testfile.sh  
  13. test.sh existed.  
  14. test1.sh existed.  
  15. test2.sh existed.  
  16. test3.sh existed.  
  17. test4.sh existed.  
  18. test5.sh existed.  
  19. test78.sh existed.  
  20. test_dev_null.sh existed.  
  21. testfile.sh existed.  
例子:005 】用指定大小文件填充硬盘

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [root@host ~]# df -ih /tmp  
  2. Filesystem            Inodes   IUsed   IFree IUse% Mounted on  
  3. /dev/mapper/vg00-lvol5  
  4.                        1000K    3.8K    997K    1% /tmp  
  5. [root@host ~]# cat cover_disk.sh  
  6. #!/bin/env bash  
  7. counter=0  
  8. max=3800  
  9. remainder=0  
  10. while true  
  11. do  
  12.     ((counter=counter+1))  
  13.     if [ ${#counter} -gt $max ];then  
  14.         break  
  15.     fi  
  16.     ((remainder=counter%1000))  
  17.     if [ $remainder -eq 0 ];then  
  18.         echo -e "counter=$counter\tdate=" $(date)  
  19.     fi  
  20.     mkdir -p /tmp/temp  
  21.     cat < testfile > "/tmp/temp/myfile.$counter"  
  22.     if [ $? -ne 0 ];then  
  23.         echo "Failed to write file to Disk."  
  24.         exit 1  
  25.     fi  
  26. done  
  27. echo "Done!"  
  28. [root@host ~]# ./cover_disk.sh  
  29. counter=1000    date= Wed Sep 10 09:20:39 HKT 2014  
  30. counter=2000    date= Wed Sep 10 09:20:48 HKT 2014  
  31. counter=3000    date= Wed Sep 10 09:20:56 HKT 2014  
  32. cat: write error: No space left on device  
  33. Failed to write file to Disk.  
  34. dd if=/dev/zero of=testfile bs=1M count=1  
例子:006 】通过遍历的方法读取配置文件

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [root@host ~]# cat hosts.allow  
  2. 127.0.0.1  
  3. 127.0.0.2  
  4. 127.0.0.3  
  5. 127.0.0.4  
  6. 127.0.0.5  
  7. 127.0.0.6  
  8. 127.0.0.7  
  9. 127.0.0.8  
  10. 127.0.0.9  
  11. [root@host ~]# cat readlines.sh  
  12. #!/bin/env bash  
  13. i=0  
  14. while read LINE;do  
  15.     hosts_allow[$i]=$LINE  
  16.     ((i++))  
  17. done < hosts.allow  
  18. for ((i=1;i<=${#hosts_allow[@]};i++)); do  
  19.     echo ${hosts_allow[$i]}  
  20. done  
  21. echo "Done"  
  22. [root@host ~]# ./readlines.sh  
  23. 127.0.0.2  
  24. 127.0.0.3  
  25. 127.0.0.4  
  26. 127.0.0.5  
  27. 127.0.0.6  
  28. 127.0.0.7  
  29. 127.0.0.8  
  30. 127.0.0.9  
  31. Done  
例子:007 】简单正则表达式应用

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [root@host ~]# cat regex.sh  
  2. #!/bin/env sh  
  3. #Filename: regex.sh  
  4. regex="[A-Za-z0-9]{6}"  
  5. if [[ $1 =~ $regex ]]  
  6. then  
  7.   num=$1  
  8.   echo $num  
  9. else  
  10.   echo "Invalid entry"  
  11.   exit 1  
  12. fi  
  13. [root@host ~]# ./regex.sh 123abc  
  14. 123abc  
  15.   
  16. #!/bin/env bash  
  17. #Filename: validint.sh  
  18. validint(){  
  19.     ret=`echo $1 | awk '{start = match($1,/^-?[0-9]+$/);if (start == 0) print "1";else print "0"}'`  
  20.     return $ret  
  21. }  
  22.   
  23. validint $1  
  24.   
  25. if [ $? -ne 0 ]; then  
  26.     echo "Wrong Entry"  
  27.     exit 1  
  28. else  
  29.     echo "OK! Input number is:" $1  
  30. fi  

例子:008】简单的按日期备份文件

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #!/bin/bash  
  2.   
  3. NOW=$(date +"%m-%d-%Y")      # 当前日期  
  4. FILE="backup.$NOW.tar.gz"    # 备份文件  
  5. echo "Backing up data to /tmp/backup.$NOW.tar.gz file, please wait..."  #打印信息  
  6. tar xcvf /tmp/backup.$NOW.tar.gz /home/ /etc/ /var       # 同时备份多个文件到指定的tar压缩文件中  
  7. echo "Done..."           

例子:009】交互式环境select的使用

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #!/bin/bash  
  2.   
  3. echo "What is your favorite OS?"  
  4.   
  5. select OS in "Windows" "Linux/Unix" "Mac OS" "Other"  
  6. do  
  7.     break  
  8. done  
  9.   
  10. echo "You have selected $OS"  
  11.   
  12. root@localhost:~/training# ./select.sh  
  13. What is your favorite OS?  
  14. 1) Windows  
  15. 2) Linux/Unix  
  16. 3) Mac OS  
  17. 4) Other  
  18. #? 1  
  19. You have selected Windows  
例子:010 】批量修改文件名的脚本
[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #!/bin/bash  
  2. # we have less than 3 arguments. Print the help text:  
  3. if [ $# -lt 3 ]; then  
  4.         cat <<-EOF  
  5.         ren -- renames a number of files using sed regular expressions  
  6.         USAGE: ren.sh 'regexp' 'replacement' files  
  7.         EXAMPLE: rename all *.HTM files in *.html:  
  8.         ren 'HTM$' 'html' *.HTM  
  9.         EOF  
  10.         exit 0  
  11. fi  
  12. OLD="$1"  
  13. NEW="$2"  
  14. # The shift command removes one argument from the list of  
  15. # command line arguments.  
  16. shift  
  17. shift  
  18. # $* contains now all the files:  
  19. for file in $*  
  20. do  
  21. if [ -f "$file" ]; then  
  22.      newfile=`echo "$file" | sed  "s/${OLD}/${NEW}/g"`  
  23.          if [ -f "$newfile" ]; then  
  24.              echo "ERROR: $newfile exists already"  
  25.          else  
  26.              echo "renaming $file to $newfile "  
  27.              mv "$file" "$newfile"  
  28.          fi  
  29. fi  
  30. done  
  31. root@localhost:~/training# ./ren.sh "HTML$" "html" file*.HTML  
  32. renaming file10.HTML to file10.html  
  33. renaming file1.HTML to file1.html  
  34. renaming file2.HTML to file2.html  
  35. renaming file3.HTML to file3.html  
  36. renaming file4.HTML to file4.html  
  37. renaming file5.HTML to file5.html  
  38. renaming file6.HTML to file6.html  
  39. renaming file7.HTML to file7.html  
  40. renaming file8.HTML to file8.html  
  41. renaming file9.HTML to file9.html  
例子:011 】break语句在脚本中的应用示例

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #!/bin/bash  
  2.   
  3. for VAR1 in 1 2 3  
  4. do  
  5.     for VAR2 in 0 5  
  6.     do  
  7.         if [ $VAR1 -eq 2 -a $VAR2 -eq 0 ]  
  8.         then  
  9.             break 2  # 退出第二重循环,亦即退出整个循环  
  10.         else  
  11.             echo "第一个变量:$VAR1 第二个变量:$VAR2"  
  12.         fi  
  13.     done  
  14. done  
  15. root@localhost:~/training# ./test.sh  
  16. 第一个变量:1 第二个变量:0  
  17. 第一个变量:1 第二个变量:5   

例子:012】/dev/tty在读取人工输入中的特殊作用

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #!/bin/bash  
  2. # 用来验证两次输入的密码是否一致  
  3.   
  4. printf "Enter your passwd: "            # 提示输入  
  5. stty -echo                              # 关闭自动打印输入字符的功能  
  6. read pwd1 < /dev/tty                    # 读取密码  
  7. printf "\nEnter again: "                # 再次提示输入  
  8. read pwd2 < /dev/tty                    # 再读取一次以确认  
  9. stty echo                               # 打开自动打印输入字符的功能  
  10.   
  11. if [[ "$pwd1" == "$pwd2" ]]; then       # 对两次输入的密码进行判断  
  12.     echo -e "\nPASSWORD: the same"  
  13. else  
  14.     echo -e "\nPASSWORD: not same"  
  15. fi  
  16. root@localhost:~/training# ./test.sh  
  17. Enter your passwd:  
  18. Enter again:   
  19. PASSWORD: the same   

例子:013】/dev/null在脚本中的简单示例

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #!/bin/bash  
  2.   
  3. if grep /bin/bash  $0 > /dev/null 2>&1    # 只关心命令的退出状态而不管其输出  
  4. then                                      # 对退出状态进行判断  
  5.     echo -e "/bin/bash in $0\n"  
  6. else  
  7.     echo -e "/bin/bash not in $0\n"  
  8. fi  
  9. 脚本输出:  
  10. root@localhost:~/training# ./test.sh  
  11. /bin/bash in ./test.sh  

例子:014】构建自己的bin目录存放执行脚本,然后随便执行的简单示例

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. $ cd                     # <span style="font-family:FangSong_GB2312;">进入家目录</span>                       
  2. $ mkdir bin              <span style="font-family:FangSong_GB2312;"># 创建$HOME目录下自己的bin目录</span>  
  3. $ mv test.sh bin         # 将我们自己的脚本放到创建的bin目录下  
  4. <span style="font-family:FangSong_GB2312;">$ </span>PATH=$PATH:$HOME/bin   # 将个人的bin目录放到PATH<span style="font-family:FangSong_GB2312;">中  
  5.   
  6. $ test.sh                # 现在就可以直接执行自己的脚本了</span>  

例子:015】将长句子中单词长度为5及以上的单词打印出来

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #!/bin/bash  
  2. # filename: test.sh  
  3.   
  4. sentence="When you're attracted to someone it just means that your subconscious is attracted to their subconscious, subconsciously.  
  5. So what we think of as fate, is just two neuroses knowing they're a perfect match."  
  6.   
  7. for word in ${sentence}  
  8. do  
  9.     new=`echo $word | tr -cd '[a-zA-Z]'`  # 去除句子中的 ,或者'  
  10.     len=${#new}                           # 求长度  
  11.     if [ "$len" -ge 5 ]                   # 再判断  
  12.     then  
  13.         echo $new  
  14.     fi  
  15. done  
  16. root@localhost:~# ./test.sh  
  17. youre  
  18. attracted  
  19. someone  
  20. means  
  21. subconscious  
  22. attracted  
  23. their  
  24. subconscious  
  25. subconsciously  
  26. think  
  27. neuroses  
  28. knowing  
  29. theyre  
  30. perfect  
  31. match   

例子:016】根据输入的数据(年4位,月2位),来判断上个月天数

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #!/bin/bash  
  2.   
  3. get_last_day()  
  4. {  
  5.     year=`expr substr $1 1 4`  
  6.     month=`expr substr $1 5 2`  
  7.     curr_month=`echo $month | tr -d '0'`   # 去掉里面的0,方便后面计算  
  8.     echo "curr_month=$curr_month"  
  9.     last_month=`expr $curr_month - 1`  
  10.     case $last_month in  
  11.         01|03|05|07|08|10|12|0)  
  12.             echo "上个月天数-->" 31   ;;  
  13.         02)  
  14.             if [ `expr $year % 400` = 0 ] ; then  
  15.                 echo "上个月天数-->" 29  
  16.             elif [ `expr $year % 4` = 0 ] && [ `expr $year % 100` != 0 ] ; then  
  17.                 echo "上个月天数-->" 29  
  18.             else  
  19.                 echo "上个月天数-->" 28  
  20.             fi                       ;;  
  21.          *)  
  22.             echo "上个月天数-->" 30  
  23.     esac  
  24. }  
  25.   
  26. if [ $# -ne 1 ]; then  
  27.     echo "Usage: $0 201608"  
  28. else  
  29.    get_last_day $1  
  30. fi  
  31.   
  32. root@localhost:~/training# ./test.sh 201601  
  33. 上个月天数--> 31  

例子:017】统计文件中每个单词出现的频率

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #!/bin/sh  
  2. # 从标准输入读取文件流,再输出出现频率的前n,默认:25个单词的列表  
  3. # 附上出现频率的计数,按照这个计数由大到小排列  
  4. # 输出到标准输出  
  5. # 语法: wf [n]  
  6.   
  7. tr -cs A-Za-z\' '\n' |  
  8.   tr A-Z a-z |  
  9.     sort |  
  10.       uniq -c |  
  11.         sort -k1,1nr -k2 |  
  12.           sed ${1:-25}q  
  13. root@localhost:~/training# wf 10 < /etc/hosts | pr -c4 -t -w80  
  14.       6 ip                1                   1 archive           1 capable  
  15.       3 ff                1 allnodes          1 are               1 cn  
  16.       2 localhost         1 allrouters  
例子:018 】使用while和break等待用户登录
 
[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #!/bin/bash  
  2. # 等待特定用户登录,每30秒确认一次  
  3. # filename: wait_for_user_login.sh  
  4.   
  5. read -p "Ener username:-> " user  
  6. while true  
  7. do  
  8.     if who | grep "$user" > /dev/null  
  9.     then  
  10.         echo "The $user now logged in."  
  11.         break  
  12.     else  
  13.         sleep 30  
  14.     fi  
  15. done  
  16. root@localhost:~/shell# ./wait_for_user_login.sh   
  17. Ener username:-> guest  
  18. The guest now logged in.  
例子:019 】结合while,case,break,shift做简单的选项处理
[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #!/bin/bash  
  2.   
  3. # 将标志变量设置为空值  
  4. file=  verbose=  quiet=  long=  
  5.   
  6. while [ $# -gt 0 ]                    # 执行循环直到没有参数为止  
  7. do  
  8.         case $1 in                    # 检查第一个参数  
  9.         -f)     file=$2  
  10.                 shift ;;              # 移位-f,使得结尾shift得到$2的值  
  11.         -v)     verbose=true   
  12.                 quiet= ;;  
  13.         -q)     quiet=true  
  14.                 verbose= ;;  
  15.         -l)     long=true ;;  
  16.         --)     shift  
  17.                 break ;;  
  18.         -*)     echo "$0: $1: unrecongnized option >&2" ;;  
  19.         *)      break ;;  
  20.         esac  
  21. done  
  22. ~                                                                                                                                                                                          
例子:020 】read读取多个变量处理,及文本遍历的两种常用方式
[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #!/bin/bash  
  2.   
  3. while IFS=: read user pwd pid gid fullname homedir shell         # IFS作为列之间的分隔符号,read读取多个变量  
  4. do  
  5.     printf "The user=%s homedir=%s\n" "$user" "$homedir"         # 对文本中的行进行处理  
  6. done < /etc/passwd                                               # 读取文件  
  7.   
  8. # 第二种方式  
  9. #!/bin/bash  
  10.   
  11. cat /etc/passwd |  
  12.     while IFS=: read user pwd pid gid fullname homedir shell  
  13. do  
  14.     printf "The user=%s homedir=%s\n" "$user" "$homedir"  
  15. done  
例子:021 】复制目录树的两个简单脚本
[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #!/bin/bash  
  2. # 方式一  
  3. find /root/shell -type d -print     |   # 寻找所有目录  
  4.   sed 's;/root/shell/;/tmp/shell/;' |   # 更改名称,使用;作为定界符  
  5.     sed 's/^/mkdir -p /'            |   # 插入mkdir -p 命令  
  6.       sh -x                             # 以Shell的跟踪模式执行  
  7.   
  8. # 方式二  
  9. find /root/shell -type d -print     |   # 寻找所有目录  
  10.   sed 's;/root/shell/;/tmp/shell/;' |   # 更改名称,使用;作为定界符  
  11.     while read newdir                   # 读取新的目录名  
  12.     do  
  13.         mkdir -p $newdir  
  14.     done  
  15. ~                             

例子:022】发邮件给系统前10名磁盘用户,要求清理磁盘空间

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #!/bin/bash  
  2.   
  3. cd /home                       # 移动到目录的顶端   
  4. du -s *       |                # 产生原始磁盘用量  
  5.   sort -nr    |                # 以数字排序,最高的在第一位  
  6.     sed 10q   |                # 在前10行之后就停止  
  7.       while read amount name   # 将读取的数据分别作为amount, name变量  
  8.       do  
  9.           mail -s "disk usage warning" $name << EOF  
  10. Gretings. You are one of the top 10 consumers of disk space  
  11. on the system. Your home directory users $amount disk blocks.  
  12.   
  13. Please clean up unneeded files, as soon as possible.  
  14.   
  15. Thanks,  
  16. Your friendly neighborhood system administrator.  
  17. EOF  
  18.       done  
例子:023 】将密码文件转换为Shell邮寄列表
[plain]  view plain  copy
 print ?
  1. #!/bin/bash  
  2.   
  3. # passwd-to-mailing-list  
  4. #  
  5. # 产生使用特定shell的所有用户邮寄列表  
  6. #  
  7. # 语法: passwd-to-mailing-list < /etc/passwd  
  8.   
  9. # 删除临时性文件  
  10. rm -rf /tmp/*.mailing-list  
  11.   
  12. # 从标准输入中读取  
  13. while IFS=: read user passwd uid gid name home Shell  
  14. do  
  15.     Shell=${Shell:-/bin/sh}           # 如为空shell,指/bin/sh  
  16.     file="/tmp/$(echo $Shell | sed -e 's;^/;;' -e 's;/;-;g').mailing-list"  
  17.     echo $user, >> $file  
  18. done  
  19. root@localhost:~# vim passwd-to-mailing-list   
  20. root@localhost:~# passwd-to-mailing-list < /etc/passwd  
  21. root@localhost:~# cat /tmp/bin-bash.mailing-list   
  22. root,  
  23. test,  
  24. user,  
  25. root@localhost:~# cat /tmp/bin-sh.mailing-list   
  26. libuuid,  
  27. jerry,  

例子:024】变更目录时更新PS1

[plain]  view plain  copy
 print ?
  1. #!/bin/bash  
  2.   
  3. cd()  
  4. {  
  5.     command cd "$@"       # 实际改变目录  
  6.     x=$(pwd)              # 取得当前目录的名称,传递给变量  
  7.     PS1="${x##*/}\$ "     # 截断前面的组成部分,指定给PS1  
  8. }  
  9. root$                     # 最后输出,类似于这种,看不到目录的完整路径  

例子:025】根据XML文件中的license时间来判断是否过期

[plain]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="gb2312"?>  
  2. <license>  
  3.   <pos>中国,福建,福州市,鼓楼区</pos>  
  4.   <installid>123123</installid>  
  5.   <device>hdsas_base_3.0.0.2_16Q2_RC2</device>  
  6.   <id>_RC257971fe611f0</id>  
  7.   <hwid>f04c3d1eb4bf6113</hwid>  
  8.   <issuetime>2016-08-02 16:46:39</issuetime>  
  9.   <expired>30 days</expired>  
  10. </license>  
  11.   
  12.   
  13. 获得<issuetime>2016-08-02 16:46:39</issuetime>时间加上<expired>30 days</expired>  
  14. 期限,得到时间减去系统当前时间,小于7天,显示license即将在几天后过期。  

代码如下:

[plain]  view plain  copy
 print ?
  1. #!/bin/bash  
  2.   
  3. CURR_TIME=$(date +'%Y%m%d')  
  4. FILE_TIME=$(grep 'issuetime' hdlicense.xml | tr -d '[\-a-z<>/]' | awk '{print $1}')  
  5. REAL_TIME=$(date -d "$FILE_TIME +30 days" +%Y%m%d)  
  6.   
  7. d1=$(date "+%s" -d "$REAL_TIME")  
  8. d2=$(date "+%s" -d "$CURR_TIME")  
  9.   
  10. EXPI_TIME=$(((d1-d2)/86400))  
  11.   
  12. if [ "$EXPI_TIME" -lt "7" ]; then  
  13.     echo "你的license将在 $EXPI_TIME 天后过期!"  
  14. fi   

例子:026】根据参数来判断是否要新创建目录

[plain]  view plain  copy
 print ?
  1. #!/bin/bash  
  2.   
  3. DIR=$1  
  4.   
  5. if [ X"$DIR" = X"" ]; then  
  6.     echo "Usage: `basename $0` directory to create" >&2  
  7.     exit 1  
  8. fi  
  9.   
  10. if [ -d $DIR ];then  
  11.     echo "The directory you create is exist."  
  12.     exit 0  
  13. else  
  14.     echo "The $DIR does not exist, will create now."  
  15.     echo -n "Create it now? [y/n]"  
  16.     read ANS  
  17.     if [ X"$ANS" = X"y" -o X"$ANS" = X"Y" ];then  
  18.         mkdir $DIR > /dev/null 2>&1  
  19.         if [ $? !=0 ]; then  
  20.                 echo "Error creating the direcory $DIR" >&2  
  21.                 exit 1  
  22.         else  
  23.                 echo "Create $DIR OK"  
  24.                 exit 0  
  25.         fi  
  26.     fi  
  27. fi  

例子:027】创建新目录,并将当前目录下的所有.txt文件拷贝到新目录中

[plain]  view plain  copy
 print ?
  1. #!/bin/bash  
  2.   
  3. DIR=testdir  
  4. THERE=`pwd`  
  5.   
  6. mkdir $DIR > /dev/null 2>&1  
  7.   
  8. if [ -d $DIR ]; then  
  9.     cd $DIR  
  10.     if [ $? = 0 ]; then  
  11.         HERE=`pwd`  
  12.         cp $THERE/*.txt $HERE  
  13.     else  
  14.         echo "Cannot cd to $DIR"  
  15.         exit 1  
  16.     fi  
  17. else  
  18.     echo "Cannot create directory $THERE"  
  19.     exit 1  
  20. fi  

例子:028】菜单显示小脚本

[plain]  view plain  copy
 print ?
  1. #!/bin/bash  
  2.   
  3. USER=`whoami`  
  4. HOST=`hostname -s`  
  5. DATE=`date '+%d/%m/%Y'`  
  6.   
  7. help(){  
  8. cat <<EOF  
  9. -----------------------------------------------------  
  10. User: $USER          Host: $HOST         Date: $DATE  
  11. -----------------------------------------------------  
  12.                 1. List files in current directory  
  13.                 2. Use the vi editor  
  14.                 3. See who is on the system  
  15.                 H. Help screen  
  16.                 Q. Exit Menu  
  17. -----------------------------------------------------  
  18.         Your Choice [1, 2, 3, 4, H, Q] >  
  19. EOF  
  20. }  
  21.   
  22. while :  
  23. do  
  24.         help  
  25.         echo -n "Enter your choice: "  
  26.         read ANS  
  27.         case $ANS in  
  28.                 1) ls -lart ;;  
  29.                 2) vi       ;;  
  30.                 3) who      ;;  
  31.                 H) help     ;;  
  32.                 Q) exit 0   ;;  
  33.         esac  
  34. done  
例子:029 】判断输入是否为纯字母示例

[plain]  view plain  copy
 print ?
  1. #!/bin/bash  
  2. error_info()  
  3. {  
  4.         echo "$@ error, please check your input."  
  5.         exit 1  
  6. }  
  7.   
  8. check_name()  
  9. {  
  10.         NAME=`echo $1 | tr -d '[a-zA-Z]'`  
  11.         if [ X"$NAME" = X"" ];then  
  12.                 return 0  
  13.         else  
  14.                 return 1  
  15.         fi  
  16. }  
  17.   
  18. while :  
  19. do  
  20.         echo -n "Please Input your first name:"  
  21.         read F_NAME  
  22.         if check_name $F_NAME; then  
  23.                 echo "Your First Name met the condition."  
  24.                 break  
  25.         else  
  26.                 echo "Wrong input, please enter again."  
  27.         fi  
  28. done  
  29.   
  30.   
  31. while :  
  32. do  
  33.         echo -n "Please Input your last name:"  
  34.         read L_NAME  
  35.         if check_name $L_NAME; then  
  36.                 echo "Your Last Name met the condition."  
  37.                 break  
  38.         else  
  39.                 error_info  
  40.         fi  
  41. done  
  42. ~  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值