LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 3981|回复: 0

一组小脚本

[复制链接]
发表于 2004-5-28 14:36:38 | 显示全部楼层 |阅读模式
在网上找的,这些小脚本很有趣,也很简单 希望对大家有帮助 :thank
------------------------------------------------------------------
calc

  1. #!/bin/ksh
  2. #
  3. # A very simple calculator - one expression per command
  4. #
  5. print $(($*))
复制代码

------------------------------------------------------------------
calc2

  1. #!/bin/ksh
  2. #
  3. # A more complex calculator - multiple expressions till ctrl-c
  4. #
  5. trap 'print Thank you for calculating!' EXIT

  6. while read expr'?expression> '; do
  7. print $(($expr))
  8. done
复制代码

------------------------------------------------------------------
conj

  1. #!/bin/ksh
  2. #
  3. # A program to convert tiff to jpeg - with checking
  4. #
  5. print there are $# files to convert
  6. print $*
  7. print Is this correct
  8. done=false
  9. while [[ $done = false ]]; do
  10. done=true
  11. {
  12. print 'Enter y for yes'
  13. print 'Enter n for no'
  14. } >&2
  15. read REPLY?'answer? '
  16. case $REPLY in
  17. y ) GO=y ;;
  18. n ) GO=n ;;
  19. * ) print 'invalid.'
  20. done=false ;;
  21. esac
  22. done
  23. if [[ "$GO" = "y" ]] then
  24. for filename in "$@" ; do
  25. newfile=${filename%.tif}.jpg
  26. eval convert $filename $newfile
  27. done
  28. fi
复制代码

------------------------------------------------------------------
conjx

  1. #!/bin/ksh
  2. #
  3. # A simple program to convert tiff to jpeg
  4. #
  5. for filename in "$@" ; do
  6. newfile=${filename%.tif}.jpg
  7. eval convert $filename $newfile
  8. done
复制代码

------------------------------------------------------------------
copro

  1. #!/bin/ksh
  2. ed - memo |&
  3. print -p /word/
  4. read -p search
  5. print "$search"
复制代码

------------------------------------------------------------------
copro2

  1. #!/bin/ksh
  2. search=eval echo /word/ | ed - memo
  3. print $search
复制代码

------------------------------------------------------------------
files

  1. #!/bin/ksh
  2. #
  3. # A program to give information about a file
  4. #
  5. if [[ ! -a $1 ]]; then
  6.     print "file $1 does not exist."
  7.     return 1
  8. fi
  9. if [[ -d $1 ]]; then
  10.     print -n "$1 is a directory that you may"
  11.     if [[ ! -x $1 ]]; then
  12.         print -n " not "
  13.     fi
  14.     print "search."
  15. elif [[ -f $1 ]]; then
  16.     print "$1 is a regular file."
  17. else
  18.     print "$1 is a special type of file."
  19. fi
  20. if [[ -O $1 ]]; then
  21.     print 'you own the file.'
  22. else
  23.     print 'you do not own the file.'
  24. fi
  25. if [[ -r $1 ]]; then
  26.     print 'you have read permission on the file.'
  27. fi
  28. if [[ -w $1 ]]; then
  29.     print 'you have write permission on the file.'
  30. fi
  31. if [[ -x $1 && ! -d $1 ]]; then
  32.     print 'you have execute permission on the file.'
  33. fi
复制代码

------------------------------------------------------------------
flist

  1. #!/bin/ksh
  2. #
  3. # A program to list multiple files seperated with file name as
  4. #  a sub-header and the date as the header
  5. #
  6. narg=$#
  7. if test $# -eq 0
  8. then echo "No files requested for listing"
  9. exit
  10. fi
  11. if test $# -eq 2
  12. then
  13. head=$1
  14. shift
  15. fi
  16. echo `date`
  17. for i
  18. in $*
  19. do
  20. echo "------------------------------------------------------------------"
  21. if test $narg -eq 1
  22. then head=$i
  23. fi
  24. echo $head
  25. echo
  26. cat $i
  27. done
复制代码

------------------------------------------------------------------
lower

  1. #!/bin/ksh
  2. #
  3. # A program to convert file names to lower case
  4. #
  5. for filename in "$@" ; do
  6. typeset -l newfile=$filename
  7. eval mv $filename $newfile
  8. done
复制代码

------------------------------------------------------------------
term1

  1. #!/bin/ksh
  2. #
  3. # An example of using select and setting terminal options
  4. #
  5. PS3='terminal? '
  6. select term in vt100 vt102 vt220 xterm; do
  7.     if [[ -n $term ]]; then
  8.         TERM=$term
  9.         print TERM is $TERM
  10.         break
  11.     else
  12.         print 'invalid.'
  13.     fi
  14. done
复制代码

------------------------------------------------------------------
term2

  1. #!/bin/ksh
  2. #
  3. # An example of using select and case to set terminal type
  4. #
  5. print 'Select your terminal type:'
  6. PS3='terminal? '
  7. select term in \
  8.     'DEC vt100' \
  9.     'DEC vt102' \
  10.     'DEC vt220' \
  11.     'xterm'
  12. do
  13.     case $REPLY in
  14.         1 ) TERM=vt100 ;;
  15.         2 ) TERM=vt102 ;;
  16.         3 ) TERM=vt220 ;;
  17.         4 ) TERM=xterm ;;
  18.         * ) print 'invalid.' ;;
  19.     esac
  20.     if [[ -n $term ]]; then
  21.         print TERM is $TERM
  22.         break
  23.     fi
  24. done
复制代码

------------------------------------------------------------------
testit

  1. #!/bin/ksh
  2. #
  3. # Script to test functions inside a shell program
  4. #
  5. testopt $*
复制代码

------------------------------------------------------------------
upper

  1. #!/bin/ksh
  2. #
  3. # A program to convert file names to upper case
  4. #
  5. for filename in "$@" ; do
  6. typeset -u newfile=$filename
  7. eval mv $filename $newfile
  8. done[/coe]
  9. ------------------------------------------------------------------
  10. usernames
  11. [code]
  12. #!/bin/ksh
  13. #
  14. # A program to generate email addresses of users sorted by surname
  15. #
  16. niscat passwd.org_dir | gawk 'BEGIN {FS=":"} /area/ && !/ftp/ && !/cccb/ && !/africa/ {print $5,$1}' | gawk '{print $(NF-1),$0 | "sort"}' | gawk 'ORS=" "{for ( i=2;i\n"}' > users
复制代码

------------------------------------------------------------------
grep2

  1. #!/bin/ksh
  2. #
  3. # Script to search for two words in a file
  4. #
  5. filename=$1
  6. word1=$2
  7. word2=$3
  8. if grep $word1 $filename && grep $word2 $filename
  9. then
  10. print "$word1 and $word2 are both in $filename."
  11. fi
复制代码

------------------------------------------------------------------
guess

  1. #!/bin/ksh
  2. #
  3. # A simple number guessing program
  4. #
  5. trap 'print Thank you for playing!' EXIT

  6. magicnum=$(($RANDOM%10+1))
  7. print 'Guess a number between 1 and 10:'
  8. while read guess'?number> '; do
  9.     sleep 1
  10.     if (( $guess == $magicnum )); then
  11.         print 'Right!'
  12.         exit
  13.     fi
  14.     print 'Wrong!'
  15. done
复制代码

------------------------------------------------------------------
guesshl

  1. #!/bin/ksh
  2. #
  3. # Another number guessing program
  4. #
  5. magicnum=$(($RANDOM%100+1))
  6. print 'Guess a number between 1 and 100:'
  7. while read guess'?number> '; do
  8.     if (( $guess == $magicnum )); then
  9.         print 'Right!'
  10.         exit
  11.     fi
  12. if (( $guess < $magicnum )); then
  13. print 'Too low!'
  14. else
  15. print 'Too high!'
  16. fi
  17. done
复制代码

------------------------------------------------------------------
message

  1. #!/bin/ksh
  2. #
  3. # An
  4. #
  5. eval /disk2/bin/msgs
  6. print 'Select your option:'
  7. PS3='(1-3 or q)? '
  8. select opt in \
  9.     'quit' \
  10.     'list header of all messages' \
  11.     'read all available messages'
  12. do
  13.     case $REPLY in
  14.         1|q ) break ;;
  15.         2 ) eval /disk2/bin/msgs -h 1 ;;
  16.         3 ) eval /disk2/bin/msgs 1 ;;
  17. * ) print "type q to quit" ;;
  18.     esac
  19. done
复制代码

------------------------------------------------------------------
minute

  1. #!/bin/ksh
  2. #
  3. # A simple program to count a minute
  4. #
  5. i=0
  6. date
  7. while test $i -lt 60;do
  8. print $i
  9. sleep 1
  10. let i=i+1
  11. done
  12. date
复制代码

------------------------------------------------------------------
minute2

  1. #!/bin/ksh
  2. #
  3. # A slightly more elegant version of minute
  4. #
  5. i=0
  6. date
  7. print
  8. while test $i -lt 60;do
  9. print ""$i
  10. sleep 1
  11. let i=i+1
  12. done
  13. date
复制代码

------------------------------------------------------------------
copro

  1. #!/bin/ksh
  2. ed - memo |&
  3. print -p /word/
  4. read -p search
  5. print "$search"
复制代码

------------------------------------------------------------------
copro2

  1. #!/bin/ksh
  2. search=eval echo /word/ | ed - memo
  3. print $search
复制代码

------------------------------------------------------------------
infloop

  1. #!/bin/ksh
  2. #
  3. # A simple program that loops indefinitely
  4. #
  5. trap 'print "You hit control-C"' INT
  6. trap 'print "You hit control-"' QUIT
  7. trap 'print "You tried to kill me!"' TERM

  8. while true; do
  9. sleep 60
  10. done
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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