当前位置:网站首页>Chapter 12 End-User Task As Shell Scripts
Chapter 12 End-User Task As Shell Scripts
2022-08-01 22:59:00 【梦想家DBA】
1.1 Starting Simple by Printing Dashes
[[email protected] Day0801]$ cat dash.sh
#!/usr/bin/env bash
# cookbook filename: dash
# dash - print a line of dashes
# options: # how many (default 72)
# -c X use char X instead of dashes
#
#
function usagexit()
{
printf "usage: %s [-c X] [#]\n" $(basename $0)
exit 2
} >&2
LEN=72
CHAR='-'
while (( $# > 0 ))
do
case $1 in
[0-9]*) LEN=$1;;
-c) shift
CHAR=$1;;
*) usagexit;;
esac
shift
done
if (( LEN > 4096 ))
then
echo "too large" >&2
exit 3
fi
# build the string to the exact length
DASHES=""
for ((i=0;i<LEN;i++))
do
DASHES="${DASHES}${CHAR}"
done
printf "%s\n" "$DASHES"
[[email protected] 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.
[[email protected] Day0801]$ cat load_mp3.sh
#!/usr/bin/env bash
# cookbook filename: load_mp3
# Fill up my mp3 player with as many songs as will fit.
# N.B.: This assumes that the mp3 player is mounted on /media/mp3
#
# determine the size of a file
#
function FILESIZE ()
{
FN=${1:-/dev/null}
if [[ -e $FN ]]
then
# FZ=$(ls -s $FN | cut -d ' ' -f 1)
set -- $(ls -s "$FN")
FZ=$1
fi
}
#
# compute the freespace on the mp3 player
#
function FREESPACE
{
# FREE=$(df /media/mp3 | awk '/^/dev/ {print $4}')
set -- $(df /media/mp3 | grep '^/dev/')
FREE=$4
}
# substract the (given) filesize from the (global) freespace
function REDUCE()
(( FREE-=${1:-0}))
#
# main:
#
let SUM=0
let COUNT=0
export FZ
export FREE
FREESPACE
find . -name '*.mp3' -print | \
(while read PATHNM
do
FILESIZE "$PATHNM"
if ((FZ <= FREE))
then
echo loading $PATHNM
cp "$PATHNM" /media/mp3
if (( $? == 0 ))
then
let SUM+=FZ
let COUNT++
REDUCE $FZ
else
echo "bad copy of $PATHNM to /media/mp3"
rm -f /media/mp3/$(basename "$PATHNM")
# recompute because we don't know how far it got
FREESPACE
fi
# any reason to go on?
if (( FREE <= 0 ))
then
break
fi
else
echo skipping $PATHNM
fi
done
printf "loaded %d songs (%d blocks)" $COUNT $SUM
printf " onto /media/mp3 (%d blocks free)\n" $FREE
)
# end of script
[[email protected] 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.
[[email protected] Day0801]$ cat oodiff.sh
#!/usr/bin/env bash
# cookbook filename: oodiff
# oodiff -- diff the CONTENTS of two OpenOffice.org files
# works only on .odt files
#
function usagexit ( )
{
echo "usage: $0 file1 file2"
echo "where both files must be .odt files"
exit $1
} >&2
# assure two readable arg filenames which end in .odt
if (( $# != 2 ))
then
usagexit 1
fi
if [[ $1 != *.odt || $2 != *.odt ]]
then
usagexit 2
fi
if [[ ! -r $1 || ! -r $2 ]]
then
usagexit 3
fi
BAS1=$(basename "$1" .odt)
BAS2=$(basename "$2" .odt)
# unzip them someplace private
PRIV1="/tmp/${BAS1}.$$_1"
PRIV2="/tmp/${BAS2}.$$_2"
# make absolute
HERE=$(pwd)
if [[ ${1:0:1} == '/' ]]
then
FULL1="${1}"
else
FULL1="${HERE}/${1}"
fi
# make absolute
if [[ ${2:0:1} == '/' ]]
then
FULL2="${2}"
else
FULL2="${HERE}/${2}"
fi
# mkdir scratch areas and check for failure
# N.B. must have whitespace around the { and } and
# must have the trailing ; in the {} lists
mkdir "$PRIV1" || { echo Unable to mkdir $PRIV1 ; exit 4; }
mkdir "$PRIV2" || { echo Unable to mkdir $PRIV2 ; exit 5; }
cd "$PRIV1"
unzip -q "$FULL1"
sed -e 's/>/>\
/g' -e 's/</\
</g' content.xml > contentwnl.xml
cd "$PRIV2"
unzip -q "$FULL2"
sed -e 's/>/>\
/g' -e 's/</\
</g' content.xml > contentwnl.xml
cd $HERE
diff "${PRIV1}/contentwnl.xml" "${PRIV2}/contentwnl.xml"
rm -rf $PRIV1 $PRIV2
[[email protected] Day0801]$边栏推荐
- 线上故障排查方案
- 03. GO language variable definition, function
- How to add a game character to a UE4 scene
- PAM Palindromic Automata
- 数据增强--学习笔记(图像类,cnn)
- 得物客服热线的演进之路
- vscode hide menu bar
- SQL29 Calculate the average next day retention rate of users
- Wechat Gymnasium Reservation Mini Program Graduation Design Finished Work Mini Program Graduation Design Finished Product (2) Mini Program Function
- y84.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶(十五)
猜你喜欢
随机推荐
图论——强连通分量缩点+拓扑排序
(翻译)按钮的对比色引导用户操作的方式
Still struggling with reporting tool selection?To take a look at this
familiar friend
联邦学习在金融领域的发展和应用
leetcode 204. Count Primes 计数质数 (Easy)
more grown, more lonely
How to prevent governance attacks in DAOs?
excel vertical to horizontal
欧拉路径与欧拉回路
别看了,这就是你的题呀
excel change cell size
Postman 批量测试接口详细教程
Wechat Gymnasium Reservation Mini Program Graduation Design Finished Work Mini Program Graduation Design Finished Product (2) Mini Program Function
下载安装 vscode(含汉化、插件的推荐和安装)
杭电多校3 1012. Two Permutations dp*
1. @Component注解的原理剖析
关于ETL的两种架构(ETL架构和ELT架构)
Wechat Gymnasium Appointment Mini Program Graduation Design Finished Work (4) Opening Report
From 0 to 100: Notes on the Development of Enrollment Registration Mini Programs








