How do I create a simple script which executes (in Linux) a sequence of commands?

StackOverflow https://stackoverflow.com/questions/22091293

  •  18-10-2022
  •  | 
  •  

문제

Premise: I know (a bit of) Python.

I'm working on a a R Sweave (R + Latex) document.

To compile and execute it from command line I need to execute the following commands from console:

R CMD Sweaver fn.Rnw
pdflatex fn.tex
okular fn.pdf

Where

fn

varies from time to time (but is the same in all of the instructions).

Since I'm doing this many times, I'd like to make the process automatic and as simple as writing something like:

script <fn>

where fn is the parameters used in the 3 single commands.

I think this is somehow possible in Python or Perl, but I don't know where to begin.

Thanks in advance

도움이 되었습니까?

해결책 2

You could write a script containing:

R CMD Sweaver "$1.Rnw"
pdflatex "$1.tex"
okular "$1.pdf"

If you name it foo.sh, you could invoke it by saying:

sh foo.sh fn

or

bash foo.sh fn

and the shell would use the positional parameter that was passed in order to perform the variable substitution.

다른 팁

Here's an example Perl script (untested):

#!/usr/bin/perl -w

use strict;
use warnings;

my $fn = shift or die "Usage: $0 function_name\n";

# I assume this needs to exist?
-e "$fn.Rnw" or die "$fn.Rnw does not exist.\n";

0 == system( 'R', 'CMD', 'Sweaver', "$fn.Rnw" ) or die "R failed.\n";
0 == system( 'pdflatex ', "$fn.tex" ) or die "pdflatex failed.\n";
0 == system( 'okular ', "$fn.pdf" ) or die "R failed.\n";

exit 0;

Write that to a file, make it executable with chmod +x, and you should be set.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top