2

I want to include correctly the following script in Ubuntu 16.04. Code

#!/bin/sh
# http://tex.stackexchange.com/a/20886/13173
# $Id: pdf2eps,v 0.01 2005/10/28 00:55:46 Herbert Voss Exp $
# Convert PDF to encapsulated PostScript.
# usage:
# pdf2eps <page number> <pdf file without ext>

pdfcrop $2.pdf
pdftops -f $1 -l $1 -eps "$2-crop.pdf" 
rm  "$2-crop.pdf"
mv  "$2-crop.eps" $2.eps

Here what I did but I think it is wrong

  1. sudo vim /usr/local/bin/pdf2eps.sh
  2. Copy paste code there.
  3. sudo chmod 777 /usr/local/bin/pdf2eps.sh
  4. rehash
  5. You see the script as pdf2eps.sh in your PATH.

Things which I do not like here

  • run vim with sudo
  • other things in my /usr/local/bin/ are symbolic links, not files itself; indicating I am doing things wrong here

How to add correctly/safely a script to your PATH?

  • 1
    /usr/local/bin or most any other directory (may be with the exception of /proc and to some extent /dev) under any UNIX/Linux variant operating system is not something special. Putting files into these directories is not going to kill your system, unless you do something really stupid. Having said that, I am still unable to grasp what you are asking here. What is your problem ? What are you trying to do and what is NOT working ? – MelBurslan May 3 '16 at 14:51
  • 1
    .sh ending, as wel as programs in /usr/local/bin are conventions in Linux, not rules. You can put an executable anywhere on your system (again some exceptions apply here) and you can add the path to your shell's PATH variable and you are good to go. No need to sweat over it. The only thing to keep in mind is, remembering where you put what. Not only you vcan get hit by a truck tomorrow and your successor needs a fighting chance, but your memory may fail you one day. So, any change you do system-wide, better have a run book and record it in there. – MelBurslan May 3 '16 at 14:54
  • 1
    why do you type 'reset' in step 4? did you mean 'rehash'; to have the shell pick up new programs from $PATH ? Not even sure that's required in current shells. – Jeff Schaller May 3 '16 at 15:00
  • 1
    if you don't like the .sh ending then why did you create the script file with it? just create /usr/local/bin/pdf2eps without the .sh ending. – cas May 3 '16 at 23:56
2

There's nothing wrong with running vim with sudo. That's the correct way to create a file in a directory that needs root access. As for the rest of the files there being links, again, not a problem. If it makes you feel better, you can create the script in /usr/bin instead but there's absolutely nothing wrong with having a regular file in /usr/local/bin. In fact, according to the Filesystem Hierarchy Standard:

The /usr/local hierarchy is for use by the system administrator when installing software locally. It needs to be safe from being overwritten when the system software is updated. It may be used for programs and data that are shareable amongst a group of hosts, but not found in /usr.

Locally installed software must be placed within /usr/local rather than /usr unless it is being installed to replace or upgrade software in /usr. [28]

So, if you want to follow the FSH, you should put anything you install manually under /usr/local, so you did the right thing.

Now, if you don't want the .sh extension, simply remove it. It serves no function on *nix systems:

sudo mv /usr/local/bin/pdf2eps.sh /usr/local/bin/pdf2eps

Your script will now be in your $PATH as pdf2eps.


What you should do, however, is fix a few issues with your script that would make it fail if run on a file name with spaces or other strange characters. Modify it as follows:

#!/bin/sh
# http://tex.stackexchange.com/a/20886/13173
# $Id: pdf2eps,v 0.01 2005/10/28 00:55:46 Herbert Voss Exp $
# Convert PDF to encapsulated PostScript.
# usage:
# pdf2eps <page number> <pdf file without ext>

if [ $# -ne 2 ]; then
    echo "Exactly 2 filenames are needed"
    exit 1
fi

pdfcrop "$2.pdf"
pdftops -f "$1" -l "$1" -eps "$2-crop.pdf"
rm  "$2-crop.pdf"
mv  "$2-crop.eps" "$2.eps"
  • 1
    can I suggest sanity-checking that the user provided 2 arguments to the script? – Jeff Schaller May 3 '16 at 15:00
  • @JeffSchaller fair enough, done. – terdon May 3 '16 at 15:04
  • 1
    @Masi see update. You could also simplify that to [ $# -ne 2 ] && echo "At least 2 arguments are needed" && exit 1. – terdon May 3 '16 at 15:04
  • I updated the code to require exactly two arguments/files, since that's how it appears to me. Revert if I'm misunderstanding it. – Jeff Schaller May 3 '16 at 16:05
  • 1
    @Masi the issue isn't the name of the file. It's that I made a mistake in my previous comment, as Stephen explained in his answer to your question. You need [ $# -le 2 ] && echo ... instead of || echo. Even better, use [ $# -ne 2 ] && echo ..." (I've edited my comment to correct it). Also, as mentioned in the comments of your script, you need to give it the file name without the extension. – terdon May 4 '16 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.