当前位置:网站首页>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]$边栏推荐
- SAP Spartacus Accessibility E2E 端到端测试
- 移动端人脸风格化技术的应用
- 小程序毕设作品之微信体育馆预约小程序毕业设计成品(1)开发概要
- img 响应式图片的实现(含srcset属性、sizes属性的使用方法,设备像素比详解)
- 线程池分析
- Wechat Gymnasium Appointment Mini Program Graduation Design Finished Work (4) Opening Report
- PAM Palindromic Automata
- excel edit a cell without double clicking
- APP special test: traffic test
- 系统可用性:SRE口中的3个9,4个9...到底是个什么东西?
猜你喜欢

华为无线设备配置双链路冷备份(AP指定配置方式)

2022 edition of MySQL tutorial, top collection good, take your time

xctf攻防世界 Web高手进阶区 web2

13、学习MySQL 分组

编曲软件FL studio20.8中文版功能和作用

10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享

小程序毕设作品之微信体育馆预约小程序毕业设计成品(1)开发概要

罗克韦尔AB PLC RSLogix5000中的比较指令使用方法介绍

Quarantine and downgrade

域名重定向工具 —— SwitchHosts 实用教程
随机推荐
AQS
E - Integer Sequence Fair
excel clear format
vscode hide menu bar
(Translation) How the contrasting color of the button guides the user's actions
小程序毕设作品之微信美食菜谱小程序毕业设计成品(6)开题答辩PPT
CAKE:一个用于多视图知识图谱补全的可扩展性常识感知框架
深度学习Course2第一周Practical aspects of Deep Learning习题整理
最短路模板
Use Jenkins for continuous integration, this knowledge point must be mastered
PHP算法之有效的括号
毫秒级!千万人脸库快速比对,上亿商品图片检索,背后的极速检索用了什么神器?
13、学习MySQL 分组
C#大型互联网平台管理框架源码:基于ASP.NET MVC+EF6+Bootstrap开发,支持多数据库
Error creating bean with name ‘dataSource‘:Unsatisfied dependency expressed through field ‘basicPro
46.全排列
Postman batch test interface detailed tutorial
如何理解 new (...args: any[]) => any
excel change cell size
1. @Component注解的原理剖析