• Chapter 12 End-User Task As Shell Scripts


    1.1 Starting Simple by Printing Dashes

    1. [maxwell@MaxwellDBA Day0801]$ cat dash.sh
    2. #!/usr/bin/env bash
    3. # cookbook filename: dash
    4. # dash - print a line of dashes
    5. # options: # how many (default 72)
    6. # -c X use char X instead of dashes
    7. #
    8. #
    9. function usagexit()
    10. {
    11. printf "usage: %s [-c X] [#]\n" $(basename $0)
    12. exit 2
    13. } >&2
    14. LEN=72
    15. CHAR='-'
    16. while (( $# > 0 ))
    17. do
    18. case $1 in
    19. [0-9]*) LEN=$1;;
    20. -c) shift
    21. CHAR=$1;;
    22. *) usagexit;;
    23. esac
    24. shift
    25. done
    26. if (( LEN > 4096 ))
    27. then
    28. echo "too large" >&2
    29. exit 3
    30. fi
    31. # build the string to the exact length
    32. DASHES=""
    33. for ((i=0;i
    34. do
    35. DASHES="${DASHES}${CHAR}"
    36. done
    37. printf "%s\n" "$DASHES"
    38. [maxwell@MaxwellDBA Day0801]$

    1.3 Loading Your MP3 Player

    Use a shell script to keep track of the available space as it copies files onto the MP3 player, quitting when it is full.

    1. [maxwell@MaxwellDBA Day0801]$ cat load_mp3.sh
    2. #!/usr/bin/env bash
    3. # cookbook filename: load_mp3
    4. # Fill up my mp3 player with as many songs as will fit.
    5. # N.B.: This assumes that the mp3 player is mounted on /media/mp3
    6. #
    7. # determine the size of a file
    8. #
    9. function FILESIZE ()
    10. {
    11. FN=${1:-/dev/null}
    12. if [[ -e $FN ]]
    13. then
    14. # FZ=$(ls -s $FN | cut -d ' ' -f 1)
    15. set -- $(ls -s "$FN")
    16. FZ=$1
    17. fi
    18. }
    19. #
    20. # compute the freespace on the mp3 player
    21. #
    22. function FREESPACE
    23. {
    24. # FREE=$(df /media/mp3 | awk '/^/dev/ {print $4}')
    25. set -- $(df /media/mp3 | grep '^/dev/')
    26. FREE=$4
    27. }
    28. # substract the (given) filesize from the (global) freespace
    29. function REDUCE()
    30. (( FREE-=${1:-0}))
    31. #
    32. # main:
    33. #
    34. let SUM=0
    35. let COUNT=0
    36. export FZ
    37. export FREE
    38. FREESPACE
    39. find . -name '*.mp3' -print | \
    40. (while read PATHNM
    41. do
    42. FILESIZE "$PATHNM"
    43. if ((FZ <= FREE))
    44. then
    45. echo loading $PATHNM
    46. cp "$PATHNM" /media/mp3
    47. if (( $? == 0 ))
    48. then
    49. let SUM+=FZ
    50. let COUNT++
    51. REDUCE $FZ
    52. else
    53. echo "bad copy of $PATHNM to /media/mp3"
    54. rm -f /media/mp3/$(basename "$PATHNM")
    55. # recompute because we don't know how far it got
    56. FREESPACE
    57. fi
    58. # any reason to go on?
    59. if (( FREE <= 0 ))
    60. then
    61. break
    62. fi
    63. else
    64. echo skipping $PATHNM
    65. fi
    66. done
    67. printf "loaded %d songs (%d blocks)" $COUNT $SUM
    68. printf " onto /media/mp3 (%d blocks free)\n" $FREE
    69. )
    70. # end of script
    71. [maxwell@MaxwellDBA Day0801]$

     1.5 Comparing Two Documents

    First, use an office suite that will let you save your documents in Open Document Format (ODF). This is the case for packages like OpenOffice.org while other commercial packages have promised to add support soon. Once you have your files in ODF, you can use a shell script to compare just the content of the files. We stress the word content here because the formatting differences are another issue, and it is (usually) the content that is the most important determinant of which version is new or more important to the end user.

    1. [maxwell@MaxwellDBA Day0801]$ cat oodiff.sh
    2. #!/usr/bin/env bash
    3. # cookbook filename: oodiff
    4. # oodiff -- diff the CONTENTS of two OpenOffice.org files
    5. # works only on .odt files
    6. #
    7. function usagexit ( )
    8. {
    9. echo "usage: $0 file1 file2"
    10. echo "where both files must be .odt files"
    11. exit $1
    12. } >&2
    13. # assure two readable arg filenames which end in .odt
    14. if (( $# != 2 ))
    15. then
    16. usagexit 1
    17. fi
    18. if [[ $1 != *.odt || $2 != *.odt ]]
    19. then
    20. usagexit 2
    21. fi
    22. if [[ ! -r $1 || ! -r $2 ]]
    23. then
    24. usagexit 3
    25. fi
    26. BAS1=$(basename "$1" .odt)
    27. BAS2=$(basename "$2" .odt)
    28. # unzip them someplace private
    29. PRIV1="/tmp/${BAS1}.$$_1"
    30. PRIV2="/tmp/${BAS2}.$$_2"
    31. # make absolute
    32. HERE=$(pwd)
    33. if [[ ${1:0:1} == '/' ]]
    34. then
    35. FULL1="${1}"
    36. else
    37. FULL1="${HERE}/${1}"
    38. fi
    39. # make absolute
    40. if [[ ${2:0:1} == '/' ]]
    41. then
    42. FULL2="${2}"
    43. else
    44. FULL2="${HERE}/${2}"
    45. fi
    46. # mkdir scratch areas and check for failure
    47. # N.B. must have whitespace around the { and } and
    48. # must have the trailing ; in the {} lists
    49. mkdir "$PRIV1" || { echo Unable to mkdir $PRIV1 ; exit 4; }
    50. mkdir "$PRIV2" || { echo Unable to mkdir $PRIV2 ; exit 5; }
    51. cd "$PRIV1"
    52. unzip -q "$FULL1"
    53. sed -e 's/>/>\
    54. /g' -e 's/
    55. content.xml > contentwnl.xml
    56. cd "$PRIV2"
    57. unzip -q "$FULL2"
    58. sed -e 's/>/>\
    59. /g' -e 's/
    60. content.xml > contentwnl.xml
    61. cd $HERE
    62. diff "${PRIV1}/contentwnl.xml" "${PRIV2}/contentwnl.xml"
    63. rm -rf $PRIV1 $PRIV2
    64. [maxwell@MaxwellDBA Day0801]$
  • 相关阅读:
    可全面适配信创生态环境的国产传输系统,了解一下
    VTK ImageData 与ITK、SimpleITK
    目前最好用的蓝牙耳机是哪个?600元真无线蓝牙耳机推荐
    基于卷尾猴算法优化概率神经网络PNN的分类预测 - 附代码
    【带头学C++】基础知识[入门篇]----1.18 volatile强制访问内存
    【从入门到起飞】JavaSE—File的使用,构造方法,成员方法
    【第13天】SQL进阶-索引的隐藏索引(SQL 小虚竹)
    当npm下载库失败时可以用cnpm替代
    死锁的产生条件以及如何避免死锁
    Java入门第106课——测试集合持有对象
  • 原文地址:https://blog.csdn.net/u011868279/article/details/126099276