pacfile脚本分析

sh/bash/dash/ksh/zsh等Shell脚本
回复
young001
帖子: 62
注册时间: 2008-10-04 20:34
来自: young001.blogbus.com
联系:

pacfile脚本分析

#1

帖子 young001 » 2009-07-08 19:56

版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://young001.blogbus.com/logs/42067599.html

#!/bin/bash

#这是个非常典型的shell编程实现,调用一些构造函数实现自己想要的功能。而且这个程序的实现也没有使用高级功能,非常适合初学者来学##习,中间如果有理解不当,万望指正啊。

#-----------------------------------------------------------
# A utility for discovering which ArchLinux package contains
# a given file. Downloads filelists for all active repos in
# pacman.conf; uses mirrors in order according to pacman's
# mirrorlist file.
#一个查询archlinux包中含有什么文件的的工具。通过下载软件库中filelists实现
#-----------------------------------------------------------

db=/var/lib/pacman/filelists
#定义数据存放地址
pacmanconf=/etc/pacman.conf
#pacman设置文件地址
mirrorlist=/etc/pacman.d/mirrorlist
#pacman镜像地址

#-----------------------------------------------------------
# Print active repos in $pacmanconf.
#-----------------------------------------------------------
function list_repos() {
sed -n '/REPOSITORIES/,$ s/^\[\(.*\)\]/\1/p' $pacmanconf
}
#打印活动软件库。 s/^\[\(.*\)\]/\1/p,我来解释一下。s///p这是替换之后打印替换行
#^\[表示查询[开头的句子,\ 就是一个转义作用。[\(.*\)这是后面\1联用,\1就是用\(.*\)中内容替换。

#-----------------------------------------------------------
# Print active mirrors in $mirrorlist.
#-----------------------------------------------------------
function list_mirrors() {
sed -n 's/^Server = \(.*\)$/\1/p' $mirrorlist
}
#这就是在mirrorlist中查找Server开头的句子打印
#-----------------------------------------------------------
# Download the filelist for repo $1. Assumes $mirrors
# is an array of urls to check for $file.
#在repo文件中mirror下载filelists,这里假设的是你的镜像有多个。
#-----------------------------------------------------------
function download() {
repo=$1
file="${repo}.files.tar.gz"
#一些定义,$1就是上面定义的function list_repos()返回值

i=0
while [[ ! -e "${db}/${file}" ]]; do
#判断是否存在file文件
if (( $i >= ${#mirrors[@]} )); then
return 1
#判断完返回值1
fi

url=$(echo "${mirrors[$i]}" | sed "s/\$repo/$repo/g")
#这是下载file文件的地址
wget --connect-timeout=15 -P "$db" "${url}/${file}"
let i+=1
#使用wget下载 -P就是定义下载地址
done
}
#这就是定义一个下载file文件的函数。

#-----------------------------------------------------------
# Decompress $1 into dir $2.
#-----------------------------------------------------------
#一下就是解压file文件的过程
function decompress() {
file=$1
repo=$2

mkdir "${db}/${repo}"
tar -xzf "${db}/${file}" -C "${db}/${repo}"
#加压文件到"${db}/${repo}"中 -C就是指定解压目录
rm -f "${db}/${file}"
}

#-----------------------------------------------------------
# Download the filelist for each active repo in
# pacman.conf. Tries mirrors in order until successful.
#-----------------------------------------------------------
function sync() {
mirrors=($(list_mirrors))
for repo in $(list_repos); do
echo "Fetching filelist for $repo"
download "$repo"
if [[ $? == 0 ]]; then
decompress "$file" "$repo"
else
echo
echo "Error: unable to retrieve $file from any mirror."
echo
fi
done
}
#这段比较容易,就是调用了上面的download函数下载filelists并且使用decompress函数来解压。如果失败打印Error: unable to retrieve $#file from any mirror."


#-----------------------------------------------------------
# Print usage message.
#-----------------------------------------------------------
function usage() {
echo "usage: $(basename $0) [options] [pattern]"
echo echo "Search package filelists for [pattern]."
echo "options:"
echo " $(basename $0) [-h --help] Print this help message."
echo " $(basename $0) [-S --sync] Syncronize package filelists."
}
#这个就是定义使用了,你直接输入pacfile就可以看见这个函数的效果了。

#-----------------------------------------------------------
# Start of `main'.
#-----------------------------------------------------------
#函数全部搞定,接下来就是main函数了
case "$1" in
-S|--sync)
if [[ $EUID != 0 ]]; then
#如果你同步,判断是否为root,如果不是输出以下文字,提醒你要使用root来更新。
echo "error: you cannot perform this operation unless you are root."
exit 1;
fi
#如果判断的确是root了,接下来就同步一下。

echo "Syncronizing ${db}..."; echo
rm -rf "$db" && mkdir -p "$db"
sync
;;

-h|--help)
usage
#-h,调用usage函数
;;
*)
if [[ $# < 1 ]]; then
echo "not enough parameters"; echo
usage
exit 1
fi
#如果没有跟查询对象,即<1,输出没有有效对象

if [[ ! -d "$db" ]]; then
echo "File database \"$db\" does not exist!"
echo "Have you synchronized the database?"
echo
exit 1
fi
#如果没有db存在,提醒是否同步过?
grep -rE "$@" "$db" | sed "s|$db/\(.*\)/files:|\1 |g"
#这句就是pacfile工作的原理,调用grep来查询,所以还是支持*等grep方式查询的。
;;
esac
http://young001.blogbus.com
努力做自己的博客,欢迎来踩
头像
xiooli
帖子: 6956
注册时间: 2007-11-19 21:51
来自: 成都
联系:

Re: pacfile脚本分析

#2

帖子 xiooli » 2009-07-08 20:01

不错,我的arch未安装命令提示就是用这个东东产生命令列表的。
头像
HuntXu
帖子: 5776
注册时间: 2007-09-29 3:09

Re: pacfile脚本分析

#3

帖子 HuntXu » 2009-07-08 23:11

不就是一grep有啥好分析...
HUNT Unfortunately No Talent...
头像
bones7456
帖子: 8495
注册时间: 2006-04-12 20:05
来自: 杭州
联系:

Re: pacfile脚本分析

#4

帖子 bones7456 » 2009-07-10 9:35

不用arch的,看这个很没意思....要不转到arch版去?
关注我的blog: ε==3
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: pacfile脚本分析

#5

帖子 eexpress » 2009-07-10 9:39

这样的事情,不用perl作,哎。
● 鸣学
头像
xiooli
帖子: 6956
注册时间: 2007-11-19 21:51
来自: 成都
联系:

Re: pacfile脚本分析

#6

帖子 xiooli » 2009-07-10 10:09

eexpress 写了:这样的事情,不用perl作,哎。
这叛徒,以前爱bash爱得要死,现在又叛变到perl :em36
回复