新手刚学习bash shell 与 latex 就用前者写了个用于自动编译后者的脚本,望老鸟给予指导。。。

sh/bash/dash/ksh/zsh等Shell脚本
回复
adfrog
帖子: 9
注册时间: 2010-11-14 0:09

新手刚学习bash shell 与 latex 就用前者写了个用于自动编译后者的脚本,望老鸟给予指导。。。

#1

帖子 adfrog » 2012-01-16 20:24

因为前不久学习了一段时间的bash shell 接着又学习起latex来 简单的代码编译出来的pdf文件原来如此漂亮。。。
早知道有latex这么优秀的排版软件,毕业设计时果断会用的阿 想当初用word 排了几天版的人伤不起阿 大学杂这么优秀的工具都不过我们介绍呢!! :em35
后来才知道一些大学早就有自己的latex模板了。。。再一次鄙视windows。。哈哈
回到主题。。
后来编译latex文件时觉得很麻烦 于是就打算用bash shell 写一个脚本一次搞定,自动编译成pdf文件,最后删除除源文件和pdf以外的其他文件(可自行修改)。。。
没有很高技术含量,就像练练bash shell,脚本如下,望老鸟指正。。。

代码: 全选

#!/bin/bash
# Program:
#   name: t2p (just tex to pdf)
#   Using latex command once or more if necessary to make certain.tex to
#   certain.dvi .Then use dvipdfmx to transform certain.dvi to certain.pdf,at
#   last delet the uesless file ex> *.aux *.log *.dvi or *.doc
# History:
# 2012/01/15    adfrog  First  release
# 2012/01/15    adfrog  Second release
#    change: use the awk language in the function nahz to be more exact  
# 2012/02/04    adfrog  Third release
#    change: add function mte to make script concise

# 显示用法
function usage() {
    echo -e "Usage:\n t2p [n] {filename.tex}"
    echo "n==>指定编译次数 默认为一次" 
}

# 获取名称和后缀
function nahz() {
    na=`ls  $file | awk 'BEGIN {FS="."} {printf $1}'`  
    hz=`ls  $file | awk 'BEGIN {FS="."} {printf $2}'`   
}

# 编译指定次数
function make() {
    for (( i=1;i<=$1;i++ ))
    do 
       latex $file
    done
}

# 转化为pdf文件,并删除多余文件
function transclean() {
    dvipdfmx $na.dvi 
    ls $na.* |grep -v "$file"|grep -v "pdf$"|xargs rm -r 
}

# 处理文件的完整功能
function mte() {  # mte just mean make then transclean and evince
    make $num
    transclean
    evince $na.pdf
}

# 主程序
num=1
case $# in
    "1")
        file=$1
        nahz
        if [ "$?" == 0 ] && [ "$hz" == "tex" ];then
            mte
            exit 0
        else
            usage
            exit 1
        fi
        ;;
    "2")
        file=$2
        nahz && echo $1|grep "^[0-9]*$" && [ "$hz" == "tex" ]
        if [ "$?" == 0 ];then
            num=$1
            mte
            exit 0
        else
            usage
            exit 1
        fi
        ;;
      *)  
        usage
        exit 1
        ;;
esac
上次由 adfrog 在 2012-02-04 0:37,总共编辑 7 次。
头像
枫叶饭团
帖子: 14683
注册时间: 2010-06-16 1:05
系统: Mac OS X
来自: Tencent
联系:

Re: 新手刚学习bash shell 与 latex 就用前者写了个用于自动编译后者的脚本,望老鸟给予指导。。。

#2

帖子 枫叶饭团 » 2012-01-16 20:28

可以把function删掉 :em04
头像
月下叹逍遥
论坛版主
帖子: 33994
注册时间: 2010-10-07 14:23
系统: Archdows10
来自: 某系某星某洲某国某省某市
联系:

Re: 新手刚学习bash shell 与 latex 就用前者写了个用于自动编译后者的脚本,望老鸟给予指导。。。

#3

帖子 月下叹逍遥 » 2012-01-16 20:30

正在摸索CTEX的淡定路过。。。
浮生七十今三十,从此凄惶未可知
adfrog
帖子: 9
注册时间: 2010-11-14 0:09

Re: 新手刚学习bash shell 与 latex 就用前者写了个用于自动编译后者的脚本,望老鸟给予指导。。。

#4

帖子 adfrog » 2012-01-16 20:32

枫叶饭团 写了:可以把function删掉 :em04
usage 的无伤大雅 ,其他的以后也好修改把。。。不都讲究模块化设计吗。。。。
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: 新手刚学习bash shell 与 latex 就用前者写了个用于自动编译后者的脚本,望老鸟给予指导。。。

#5

帖子 aerofox » 2012-01-18 8:50

同意把 function 删掉。

代码: 全选

# 获取名称和后缀
function nahz() {
    na=`ls  $file | awk 'BEGIN {FS="."} {printf $1}'`  
    hz=`ls  $file | awk 'BEGIN {FS="."} {printf $2}'`   
}
用了 bash 的话,可以使用 bash 内置的功能,既快速又准确,尤其不应用 ls 管道到 awk 来处理。

代码: 全选

name="${file%.*}"
ext="${file##*.}"
楼主那段处理带多个句点的文件名时会有问题,处理带特殊字符的文件名时也可能会有问题。我贴的处理不带扩展名的文件时会有问题,需要时可以改一改。

另外,两个 case 分支中包含了太多的重复内容。
上次由 aerofox 在 2012-01-18 13:11,总共编辑 1 次。
谢宝良
帖子: 1983
注册时间: 2010-05-01 21:23

Re: 新手刚学习bash shell 与 latex 就用前者写了个用于自动编译后者的脚本,望老鸟给予指导。。。

#6

帖子 谢宝良 » 2012-01-18 9:00

其实编译latex文件不麻烦,在文件浏览器上装个终端,用x代替xelatex命令,打开目录,输入x,把tex文件拖入终端,回车搞定.如图:
附件
22.png
头像
tangboyun
帖子: 701
注册时间: 2009-07-25 1:57
联系:

Re: 新手刚学习bash shell 与 latex 就用前者写了个用于自动编译后者的脚本,望老鸟给予指导。。。

#7

帖子 tangboyun » 2012-01-18 13:10

我建议楼主不需要硬性指定编译次数,事实上本身有脚本就可以完成这件事情,你只需要调用这个脚本就可以了:latexmk,这个可以自动解决引用、参考文献之类需要多次编译的问题,一些有用的参数设定可以看man。ubuntu下哪个包带这个脚本,可以apt-file搜下。。。自己手装texlive的话,直接就有了。

还有我觉得楼主应该把编译写成Makefile,这样更符合传统,而且也更简单。
https://github.com/tangboyun
http://tangboyun.is-programmer.com/
提问的智慧————Eric Steven Raymond
回答的智慧————Andrew Clarke
吾尝终日而思矣,不如须臾之所学也;吾尝跂而望矣,不如登高之博见也。
急急急标题什么的,最讨厌了!
急急复急急,急急何其多,我生待急急,万事急急急。
tusooa
帖子: 6548
注册时间: 2008-10-31 22:12
系统: 践兔
联系:

Re: 新手刚学习bash shell 与 latex 就用前者写了个用于自动编译后者的脚本,望老鸟给予指导。。。

#8

帖子 tusooa » 2012-01-19 22:46

建议不要echo $1|grep "^[0-9]*$"
更不要ls $file|

代码: 全选

        if [ "$?" == 0 ];then
            make $1
            transclean
            evince $na.pdf
            exit 0
        else
这边重复了。可以在设个变量嘛。默认是1,在有参数的时候,再赋值啊。
最后make $num

代码: 全选

] ls -ld //
adfrog
帖子: 9
注册时间: 2010-11-14 0:09

Re: 新手刚学习bash shell 与 latex 就用前者写了个用于自动编译后者的脚本,望老鸟给予指导。。。

#9

帖子 adfrog » 2012-01-28 9:37

tusooa 写了:建议不要echo $1|grep "^[0-9]*$"
更不要ls $file|

代码: 全选

        if [ "$?" == 0 ];then
            make $1
            transclean
            evince $na.pdf
            exit 0
        else
这边重复了。可以在设个变量嘛。默认是1,在有参数的时候,再赋值啊。
最后make $num
同意,有段时间没上论坛,谢谢大家的反馈 ,其实这个贴重点还是学习shell 哈哈
回复