What is the easiest way to pass a file as an argument in a simple shell script?

On Mac OS X, the following works fine:

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

Now I understand that these 3 lines of code are really useful for my work - regardless of the specific file. So, I would like to use the file as an argument. I know how to use the argument itself, but it has problems with breaking the input after the string and then concatenating it. If I could break the file name argument, for example:

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

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

Thanks in advance for any general tips and examples!

+3
source share
5 answers

$1.Rnw, $1.tex $1.pdf. Python , bash 10 .

, cut -f 1 -d '.' $1.

+6

python.
, .

+6

Python :

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

" ". .

Edit:

"backticks" , , :

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

:

test
+2

Makefiles, ( makefile ) , Makefile .

$(FILE) $@, "make foo".

, .

+1

Python, , , , , bash, . , LaTeX make 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 *~

w/

make

make clean

The disadvantage is that you need a special makefile for each of your projects, but with a template that is not a big problem. Hth

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

0
source

Source: https://habr.com/ru/post/1759944/