Why does this script not accept two arguments?

Code

#!/bin/sh
# http://tex.stackexchange.com/a/20886/13173
# http://unix.stackexchange.com/a/280830/16920
# $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>

[ $# -lt 2 ] || echo "At least 2 arguments are needed" && exit 1

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

Run it as

pdf2eps 1 ./01-02-2002-01-02-03.pdf

Output

At least 2 arguments are needed

and no output file.


Why you cannot run here the command on 01-02.pdf?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

[ $# -lt 2 ] || echo "At least 2 arguments are needed" && exit 1

checks whether the number of arguments is strictly less than 2, and if it’s not, outputs “At least 2 arguments are needed”. You need to replace the || with &&:

[ $# -lt 2 ] && echo "At least 2 arguments are needed" && exit 1

I think the example you linked to in Safe way to install .sh script in /usr/local/bin? is actually easier to understand (and -ne is better suited since having more than 2 arguments is probably an error):

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

Note too that you’re not supposed to specify the extension, so you should run the command as

pdf2eps 1 ./01-02-2002-01-02-03


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x