views:

393

answers:

3

So, I have a directory, and in it are several files. I'm trying to decrypt those files and then move them to another directory. I can't seem to figure out how to set the output filename and move it.

So, the directory structure looks like this:

/Applications/MAMP/bin/decryptandmove.sh
/Applications/MAMP/bin/passtext.txt
/Applications/MAMP/bin/encrypted/test1.txt.pgp
/Applications/MAMP/bin/encrypted/test2.txt.pgp
/Applications/MAMP/htdocs/www/decrypted/

For all of the files found in the encrypted directory, I'm trying to decrypt it and then move them into www/decrypted/. I don't know what the filenames in the encrypted directory will be ahead of time (this script will eventually run via cron), so I wanted to just output the decrypted file with the same filename, but without the pgp. So, the result would be:

/Applications/MAMP/bin/decryptandmove.sh
/Applications/MAMP/bin/passtext.txt
/Applications/MAMP/bin/encrypted/
/Applications/MAMP/htdocs/decrypted/test1.txt.pgp
/Applications/MAMP/htdocs/decrypted/test2.txt.pgp

So, this is all I have so far, and it doesn't work... FILE and FILENAME are both wrong... I haven't even gotten to the moving part of it yet.... Help? I've written exactly one shell script ever, and it was so simple, a monkey could have done it... Feeling out of depth here...

pass_phrase=`cat passtext.txt|awk '{print $1}'`

for FILE in '/Applications/MAMP/bin/encrypted/';
 do
    FILENAME=$(basename $FILE .pgp) 
    gpg --passphrase $pass_phrase --output $FILENAME --decrypt $FILE
 done
A: 

This should fix the FILENAME and FILE problems, although it will only work if you're in the "decrypted" directory right now. If you want to fix that you'll have to add the correct directory to the FILENAME variable :)

pass_phrase=`cat passtext.txt|awk '{print $1}'`

for FILE in /Applications/MAMP/bin/encrypted/*; do
    FILENAME=$(basename $FILE .pgp) 
    gpg --passphrase $pass_phrase --output $FILENAME --decrypt $FILE
done
WoLpH
A: 

I like to do a cd first:

cd /Applications/MAMP/bin/encrypted/

Then

for FILE in $(ls); do ...

or

for FILE in `ls`; do ...

I personally prefer:

ls | while read FILE; do ...

Then maybe

cd -

Important: If you use the ls approach, make sure that your filenames don't contain spaces. See also the link in ghostdog74's comment (thanks, BTW) - this page is generally quite useful.

Chris Lercher
useless use of `ls`
ghostdog74
@ghostdog74: Maybe you're right, but using ls has some advantages: 1) You don't have to use basename to get rid of the unnecessary path. 2) You can use all the options of ls, and even pipe it through other tools before using the values.
Chris Lercher
using `ls` in a for loop has no advantages at all. you fork another process and its prone to error if file names has white spaces. As for 2), you did not use `ls -l`, so there are no options to talk about
ghostdog74
see here also http://mywiki.wooledge.org/BashPitfalls#for_i_in_.60ls_.2A.mp3.60
ghostdog74
I know. Still, I need it for my scripts to sort files by date. How would you do that?
Chris Lercher
then use the `while read FILE` approach.
ghostdog74
+1  A: 
#!/bin/bash
p=$(<passtext.txt)
set -- $p
pass_phrase=$1
destination="/Applications/MAMP/htdocs/www/decrypted/"
cd /Applications/MAMP/bin/encrypted
for FILE in *.pgp;
do
    FILENAME=${FILE%.pgp}
    gpg --passphrase "$pass_phrase" --output "$destination/$FILENAME" --decrypt "$FILE"
done
ghostdog74
I've been trying to use this, but I keep getting:gpg: /Applications/MAMP/bin/encrypted/: read error: Is a directorygpg: decrypt_message failed: eof
KittyYoung
Oh, wait... you changed it :) Let me try again...
KittyYoung
It's perfect! Thank you very much. I totally don't understand most of the syntax, but I'll definitely try to figure out what it all means...
KittyYoung