当前位置:网站首页>Chapter 12 End-User Task As Shell Scripts
Chapter 12 End-User Task As Shell Scripts
2022-08-01 23:09:00 【Dreamer 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]$
边栏推荐
- How do programmers solve online problems gracefully?
- JS prototype hasOwnProperty in Add method Prototype end point Inherit Override parent class method
- Wechat Gymnasium Appointment Mini Program Graduation Design Finished Work (4) Opening Report
- CF1705D Mark and Lightbulbs
- 华为无线设备配置全局双链路冷备份(AC全局配置方式)
- ping no reply
- 牛客多校4 A.Task Computing 思维
- Chapter 12 End-User Task As Shell Scripts
- 联邦学习的框架搭建
- 对于在新标签页中打开的链接,始终使用“noopener”或“noreferrer”
猜你喜欢
Solve the port to take up
如何给 UE4 场景添加游戏角色
D - Linear Probing- 并查集
华为无线设备配置全局双链路冷备份(AC全局配置方式)
小程序毕设作品之微信美食菜谱小程序毕业设计成品(5)任务书
System availability: 3 9s, 4 9s in SRE's mouth... What is it?
华为无线设备配置双链路冷备份(AP指定配置方式)
drf生成序列化类代码
Wechat Gymnasium Appointment Mini Program Graduation Design Finished Work (4) Opening Report
2022年最新河北建筑八大员(机械员)模拟考试题库及答案
随机推荐
From 0 to 100: Notes on the Development of Enrollment Registration Mini Programs
美赞臣EDI 940仓库装运订单详解
Background project Express-Mysql-Vue3-TS-Pinia page layout-sidebar menu
When using DocumentFragments add a large number of elements
PostgreSQL Basics--Common Commands
Ten years after graduation, financial freedom: those things that are more important than hard work, no one will ever teach you
牛客多校4 A.Task Computing 思维
基于JAX的激活函数、softmax函数和交叉熵函数
Solve the port to take up
Error creating bean with name ‘dataSource‘:Unsatisfied dependency expressed through field ‘basicPro
chrome copies the base64 data of an image
Calculate the distance between two points
检查点是否在矩形内
对于在新标签页中打开的链接,始终使用“noopener”或“noreferrer”
IDEA入门看这一篇就够了
TCP 可靠吗?为什么?
毫秒级!千万人脸库快速比对,上亿商品图片检索,背后的极速检索用了什么神器?
【参营经历贴】2022网安夏令营
B. Difference Array--Codeforces Round #808 (Div. 1)
C#大型互联网平台管理框架源码:基于ASP.NET MVC+EF6+Bootstrap开发,支持多数据库