当前位置:网站首页>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]$
边栏推荐
- B. Difference Array--Codeforces Round #808 (Div. 1)
- Postman batch test interface detailed tutorial
- 2022 edition of MySQL tutorial, top collection good, take your time
- Postman 批量测试接口详细教程
- SQL Server (design database--stored procedure--trigger)
- System availability: 3 9s, 4 9s in SRE's mouth... What is it?
- 隔离和降级
- APP专项测试:流量测试
- Deep learning Course2 first week Practical aspects of Deep Learning exercises
- excel clear format
猜你喜欢
一种灵活的智能合约协作方式
小程序毕设作品之微信美食菜谱小程序毕业设计成品(8)毕业设计论文模板
从0到100:招生报名小程序开发笔记
力扣第 304 场周赛复盘
10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享
When solving yolov5 training: "AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train"
【SeaTunnel】从一个数据集成组件演化成企业级的服务
编曲软件FL studio20.8中文版功能和作用
联邦学习入门
03、GO语言变量定义、函数
随机推荐
JS prototype hasOwnProperty in Add method Prototype end point Inherit Override parent class method
使用分类权重解决数据不平衡的问题
Prufer sequence
Error creating bean with name ‘dataSource‘:Unsatisfied dependency expressed through field ‘basicPro
prim生成树
Interpretation of the paper (GSAT) "Interpretable and Generalizable Graph Learning via Stochastic Attention Mechanism"
2022年最新河北建筑八大员(机械员)模拟考试题库及答案
[Recommended books] The first self-driving technology book
13、学习MySQL 分组
perspectiveTransform warpPerspective getPerspectiveTransform findHomography
Go 微服务开发框架DMicro的设计思路
CF1705D Mark and Lightbulbs
excel change cell size
企业公众号文章写作方向:如何写出读者认可的优质内容
JS prototype hasOwnProperty in 加方法 原型终点 继承 重写父类方法
ping no reply
浅析多服务在分布式系统下多事务通信处理机制方案
小程序毕设作品之微信体育馆预约小程序毕业设计成品(3)后台功能
y84. Chapter 4 Prometheus Factory Monitoring System and Actual Combat -- Advanced Prometheus Alarm Mechanism (15)
leetcode 204. Count Primes 计数质数 (Easy)