Shell script doesn't collect password file ...

Running the below shell script seems to be ignoring the password file I feed it. I suggest this all the time. If I enter it, the rest of the script runs smoothly, but when I run it through cron I really need to get it to read from a file ... Any suggestions?

#!/bin/sh
p=$(<password.txt)
set -- $p
pass_phrase=$1
destination="/var/www/d"
cd /var/sl/
for FILE in *.pgp;
do
    FILENAME=${FILE%.pgp}
    gpg --passphrase "$pass_phrase" --output "$destination/$FILENAME" --decrypt "$FILE"
    rm -f $FILE
done

      

+2


a source to share


4 answers


Your problem is line 2:

p=$(<password.txt)

      

What you are doing here is to run an "empty command" in a subshell, saving its output to a variable p

. Instead, you want to run a command that dumps the contents of the password file to stdout

. So:



p=$(cat <password.txt)

      

This will do the trick.

+1


a source


You probably need to provide the full path to the file. Or in your cron job, first cd to the directory containing that file.



0


a source


Does it really exist --passphrase

? According to the manpage, this does not happen, but the versions may differ.

0


a source


Where is the password file located? cron

has another PATH

one that can cause the scripts to behave differently when you run them yourself.

Possible solution one, put

cd `dirname $0`

      

at the top of the script, which will be cd

in the script directory when it is run.

There are two possible solution, try pointing the file directly with an absolute path:

gpg --passphrase-file /some/path/password.txt -o "$destination/$FILENAME" -d "$FILE"

      

0


a source







All Articles