在简单的shell脚本中将文件作为参数传递的最简单方法是什么?

时间:2021-10-05 23:37:20

The following works fine on Mac OS X:

以下在Mac OS X上运行正常:

#!/bin/bash
R CMD Sweave myfile.Rnw
pdflatex myfile.tex
open myfile.pdf

Now, I realize that these 3 lines of code are really helpful for my work – independently of some particular file. Thus I'd like to use the file as an argument. I know how to use an argument itself but have problems splitting the input after the string and concat it afterwards. If I was able to split the filename argument like:

现在,我意识到这3行代码对我的工作非常有帮助 - 独立于某些特定文件。因此,我想将该文件用作参数。我知道如何使用参数本身,但在字符串之后拆分输入并在之后连接它时会出现问题。如果我能够分割文件名参数,如:

split($1,".") # return some array or list ("name","ext")

Or is there a simpler, completely different way than using Python within a shell script?

或者是否比在shell脚本中使用Python更简单,完全不同?

Thx in advance for any general advice and examples as well !

提前获取任何一般建议和示例!

5 个解决方案

#1


6  

You could just take the base name as an argument and use $1.Rnw, $1.tex, and $1.pdf. Python is great for shell scripts, but I usually stick with bash for things less than 10 lines long.

您可以将基本名称作为参数,并使用$ 1.Rnw,$ 1.tex和$ 1.pdf。 Python非常适合shell脚本,但我通常坚持使用bash来处理不到10行的事情。

If you really want to take a file name, you can use cut -f 1 -d '.' $1.

如果你真的想要一个文件名,你可以使用cut -f 1 -d'。' $ 1

#2


6  

I do all my shell scripting in python.
It's easier to read, more powerful and works on windows as well.

我在python中执行所有shell脚本编写。它更容易阅读,更强大,也适用于Windows。

#3


2  

The Python one liner would be:

Python one liner将是:

python -c "print '$1'.split('.')[0]"

But Nathon's idea "Use the base name as the argument." is IMHO the best solution.

但Nathon的想法是“使用基本名称作为参数”。恕我直言是最好的解决方案。

Edit:

编辑:

You use "backticks" to use text that a program put on the standard output, like so:

您使用“反引号”来使用程序放在标准输出上的文本,如下所示:

eike@lixie:~> FOO="test.foo"
eike@lixie:~> BAR=`python -c "print '$FOO'.split('.')[0]"`
eike@lixie:~> echo $BAR

This should result in:

这应该导致:

test

#4


1  

I agree with Gerald's suggestion to use Makefiles, but his downside comment (dedicated makefile for each project) isn't quite correct, as Makefiles can be made more generic.

我同意Gerald关于使用Makefiles的建议,但他的缺点评论(每个项目的专用makefile)并不完全正确,因为Makefile可以更通用。

Replace $(FILE) with $@ and then invoke with "make foo".

用$ @替换$(FILE)然后用“make foo”调用。

I'd leave this as a comment to Gerald's answer but do not have the points to do so.

我将此作为对Gerald答案的评论,但没有必要这样做。

#5


0  

Python is certainly a good choice for shell scripting, however for a simple example as yours using bash is easier. Yet again, for compiling LaTeX I'd recommend a makefile and using GNU make. In case you haven't heard of it, you can do sth like this:

Python当然是shell脚本的一个很好的选择,但是对于一个简单的例子,因为你使用bash更容易。再次,为了编译LaTeX,我建议使用makefile并使用GNU make。如果你还没有听说过,你可以这样做:

FILE = your_tex_filename
INCLUDES = preface.tex introduction.tex framework.tex abbreviations.tex 

all: $(FILE).pdf

$(FILE).pdf: $(FILE).tex $(INCLUDES) $(FILE).aux index bibliography
    pdflatex $(FILE).tex

index: $(FILE).tex
    makeindex $(FILE).idx

bibliography: $(FILE).bib $(FILE).aux
    bibtex $(FILE)

$(FILE).aux: $(FILE).tex
    pdflatex $(FILE).tex

# bbl and blg contain the bibliography
# idx and ind contain the index
.PHONY : clean
clean:
    rm *.aux *.bak $(FILE).bbl $(FILE).blg \
       *.flc *.idx *.ind *.log *.lof *.lot *.toc core \
       *.backup *.ilg *.out *~

and then simply compile your source document w/

然后简单地编译你的源文件w /

make

or clean up after building w/

建成后清理干净

make clean

A downside is that you'd need a dedicated makefile for each of your projects, but w/ a template that is not much of an issue. hth

一个缺点是你需要为你的每个项目提供一个专用的makefile,但是有一个不是问题的模板。心连心

PS: A great introduction to string manipulation w/ the bash shell can be found at http://www.faqs.org/docs/abs/HTML/string-manipulation.html.

PS:关于bash shell的字符串操作的一个很好的介绍可以在http://www.faqs.org/docs/abs/HTML/string-manipulation.html找到。

#1


6  

You could just take the base name as an argument and use $1.Rnw, $1.tex, and $1.pdf. Python is great for shell scripts, but I usually stick with bash for things less than 10 lines long.

您可以将基本名称作为参数,并使用$ 1.Rnw,$ 1.tex和$ 1.pdf。 Python非常适合shell脚本,但我通常坚持使用bash来处理不到10行的事情。

If you really want to take a file name, you can use cut -f 1 -d '.' $1.

如果你真的想要一个文件名,你可以使用cut -f 1 -d'。' $ 1

#2


6  

I do all my shell scripting in python.
It's easier to read, more powerful and works on windows as well.

我在python中执行所有shell脚本编写。它更容易阅读,更强大,也适用于Windows。

#3


2  

The Python one liner would be:

Python one liner将是:

python -c "print '$1'.split('.')[0]"

But Nathon's idea "Use the base name as the argument." is IMHO the best solution.

但Nathon的想法是“使用基本名称作为参数”。恕我直言是最好的解决方案。

Edit:

编辑:

You use "backticks" to use text that a program put on the standard output, like so:

您使用“反引号”来使用程序放在标准输出上的文本,如下所示:

eike@lixie:~> FOO="test.foo"
eike@lixie:~> BAR=`python -c "print '$FOO'.split('.')[0]"`
eike@lixie:~> echo $BAR

This should result in:

这应该导致:

test

#4


1  

I agree with Gerald's suggestion to use Makefiles, but his downside comment (dedicated makefile for each project) isn't quite correct, as Makefiles can be made more generic.

我同意Gerald关于使用Makefiles的建议,但他的缺点评论(每个项目的专用makefile)并不完全正确,因为Makefile可以更通用。

Replace $(FILE) with $@ and then invoke with "make foo".

用$ @替换$(FILE)然后用“make foo”调用。

I'd leave this as a comment to Gerald's answer but do not have the points to do so.

我将此作为对Gerald答案的评论,但没有必要这样做。

#5


0  

Python is certainly a good choice for shell scripting, however for a simple example as yours using bash is easier. Yet again, for compiling LaTeX I'd recommend a makefile and using GNU make. In case you haven't heard of it, you can do sth like this:

Python当然是shell脚本的一个很好的选择,但是对于一个简单的例子,因为你使用bash更容易。再次,为了编译LaTeX,我建议使用makefile并使用GNU make。如果你还没有听说过,你可以这样做:

FILE = your_tex_filename
INCLUDES = preface.tex introduction.tex framework.tex abbreviations.tex 

all: $(FILE).pdf

$(FILE).pdf: $(FILE).tex $(INCLUDES) $(FILE).aux index bibliography
    pdflatex $(FILE).tex

index: $(FILE).tex
    makeindex $(FILE).idx

bibliography: $(FILE).bib $(FILE).aux
    bibtex $(FILE)

$(FILE).aux: $(FILE).tex
    pdflatex $(FILE).tex

# bbl and blg contain the bibliography
# idx and ind contain the index
.PHONY : clean
clean:
    rm *.aux *.bak $(FILE).bbl $(FILE).blg \
       *.flc *.idx *.ind *.log *.lof *.lot *.toc core \
       *.backup *.ilg *.out *~

and then simply compile your source document w/

然后简单地编译你的源文件w /

make

or clean up after building w/

建成后清理干净

make clean

A downside is that you'd need a dedicated makefile for each of your projects, but w/ a template that is not much of an issue. hth

一个缺点是你需要为你的每个项目提供一个专用的makefile,但是有一个不是问题的模板。心连心

PS: A great introduction to string manipulation w/ the bash shell can be found at http://www.faqs.org/docs/abs/HTML/string-manipulation.html.

PS:关于bash shell的字符串操作的一个很好的介绍可以在http://www.faqs.org/docs/abs/HTML/string-manipulation.html找到。