Using $1 in shell script

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Using $1 in shell script Fri Nov 06, 2015 5:58 am • by dzg
I'm trying to write a rule to decrypt PDF files with qpdf, which requires both input & output filename.

So given the input filename $1, how do I generate the output filename to be equal to $1 but with, say, 'decrypted.pdf' at the end?

I tried
Code: Select all
qpdf --decrypt --password=xxx $1 "$1_decrypted.pdf"

but it gives an error.

I also tried
Code: Select all
qpdf --decrypt --password=xxx $1 `"$1"_decrypted.pdf`

and
Code: Select all
f="$1"_decrypted.pdf; qpdf --decrypt --password=xxx $1 $f

and so on.

Ideally, I would like to parse just the filename without extension, and add 'decrypted' to that.
dzg
 
Posts: 25
Joined: Tue Jun 26, 2012 10:19 pm

Re: Using $1 in shell script Fri Nov 06, 2015 11:33 am • by Mr_Noodle
Keep in mind that $1 is the full path and not just the filename. Search around for shell scripting resources on the net as there are various ways to extract the different parts of the path/filename. Also, it's a good practice to have $1 in double-quotes, in case the path has spaces in it.
Mr_Noodle
Site Admin
 
Posts: 11868
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Using $1 in shell script Fri Nov 06, 2015 12:25 pm • by dzg
Well, all those attempts were my best shot at scripting to add a suffix.

They worked in Terminal, but not within Hazel.

Can you give me a simple 'add suffix xxx to the output filename' script?
dzg
 
Posts: 25
Joined: Tue Jun 26, 2012 10:19 pm

Re: Using $1 in shell script Fri Nov 06, 2015 12:54 pm • by dzg
I did some homework and got this ...

if $f is
Code: Select all
"/Volumes/HD/Users/z/aa bb.pdf"

then this in Terminal:
Code: Select all
echo $(dirname $f)/$(basename $f ".pdf")_decrypted.pdf

gives
Code: Select all
"/Volumes/HD/Users/z/aa bb_decrypted.pdf"


but this within Hazel gives errors:
Code: Select all
qpdf --decrypt --password=xxx "$1" \"$(dirname "$1")/$(basename "$1" ".pdf")_decrypted.pdf\"


I'm probably not getting all the quoting right
dzg
 
Posts: 25
Joined: Tue Jun 26, 2012 10:19 pm

Re: Using $1 in shell script Fri Nov 06, 2015 1:02 pm • by dzg
OK, I got it!

Code: Select all
o=$(dirname "$1")/$(basename "$1" ".pdf")_decrypted.pdf; qpdf --decrypt --password=xxx "$1" "$o"
dzg
 
Posts: 25
Joined: Tue Jun 26, 2012 10:19 pm


Return to Support