--------------------------------------------------------------------
[006目录结构]
--------------------------------------------------------------------
.
|-- book-2008-08-25
|-- install.sh
|-- makebooks.sh
`-- readme
0 directories, 4 files
--------------------------------------------------------------------
[./readme]
--------------------------------------------------------------------
程序名称:
源代码目录结构
程序功能:
为了提交源代码到论坛
自制的极其粗陋的制作源代码目录结构脚本
安装方法:
./install.sh
使用说明:
1.makebooks.sh
到源代码所在的主文件夹下
运行makebooks.sh
当前目录生成的book-XXX即为源代码要发布到论坛的文章形式
建议:
因为是个人使用所以程序功能极其粗陋
待以后有需要时再改进
注意:
1.
源代码文件夹中只会录入文本文件
2.
不要以"filename~"的形式起文件的名字
程序会删掉名字后缀为~的文件
总结:
1.
错误代码:
find | while read name
do
echo "$name"
done
严格的讲, find | read 模式也是有问题的, read 会对反斜杠 \ 有特殊处理
find | sed -e 's/\\/\\\\/g' | while read name
do
echo "$name"
done
这样的代码才不会一遇到输入字符串的空格就进入do...done且处理了read的特殊情况
2.
错误代码:
echo $name
正确代码:
echo "$name"
echo "$name" 跟 echo $name 还是有区别的. 比如 name 中有两个连续空格的时候
3.
for name in *
do
echo $name
done
这样的代码不会一遇到输入字符串的空格就进入do...done
4.
for name in `ls`
do
echo $name
done
这样的代码会一遇到输入字符串的空格就进入do...done
5.
dir=`pwd`
echo $dir
即使`pwd`输出中有空格,dir也会归入其中,成为一个完整的字符串
6.
错误代码:
if [ -e $test ] ; then
echo "hoho"
fi
正确代码:
if [ -e "$test" ] ; then
echo "hoho"
fi
7.
错误代码:
file $name
cat $name
rm $test
basename $dir
cp "have a test" $installdir
正确代码:
file "$name"
cat "$name"
rm "$test"
basename "dir"
cp "have a test" "$installdir"
8.
正确代码:
output="have a test"
echo "just" > $output
--------------------------------------------------------------------
[./install.sh]
--------------------------------------------------------------------
#!/bin/sh
#程序名称:源代码目录结构
#联系方式:babyaries@126.com
info="安装文件缺失,安装过程中断!"
name="源代码目录结构"
installdir="/usr/local/bin/"
if [ ! -e makebooks.sh ] ; then
echo "$info"
exit 1
fi
chmod +x makebooks.sh
cp makebooks.sh "$installdir"
echo "$name安装成功!"
exit 0
--------------------------------------------------------------------
[./makebooks.sh]
--------------------------------------------------------------------
#!/bin/sh
filepath=
filetype=
bookname=book-`date '+%F'`
decorate="--------------------------------------------------------------------"
currentdir=`pwd`
calc=0
deco()
{
if [ $((calc%2)) -eq 0 -a $calc -ne 0 ] ; then
echo >> $bookname
fi
calc=$((calc+1))
echo "$decorate" >> $bookname
}
if [ -e "$bookname" ] ; then
rm -f "$bookname"
fi
find | sed -e 's/\\/\\\\/g' | sed -n -e '/^.*~$/p' | while read filepath
do
rm -f "$filepath"
done
deco
echo "[`basename "$currentdir"`目录结构]" >> $bookname
deco
tree >> $bookname
find | sed -e 's/\\/\\\\/g' | sed -e '/.\/'"$bookname"'/d' | while read filepath
do
if [ -f "$filepath" ] ; then
filetype=`file "$filepath"`
filetype=${filetype#* }
echo "$filetype" | grep -q "text"
if [ $? -eq 0 ] ; then
deco
echo "[$filepath]" >> $bookname
deco
cat "$filepath" >> $bookname
fi
fi
done
echo "$bookname制作完成"
--------------------next---------------------