Shell 脚本未获取密码文件...
运行下面的 shell 脚本似乎会忽略我提供给它的密码文件。我不断被提示。如果我输入它,脚本的其余部分就会顺利进行,但是当我通过 cron 运行它时,我真的需要让它从文件中读取......有什么建议吗?
#!/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
Running the below shell script seems to ignore the password file I'm feeding it. I'm continually prompted for it. If I enter it, the rest of the script goes without a hitch, but as I'm running it via cron, I really need to get it to read from the 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的问题出在第 2 行:
您在这里所做的是在子 shell 中运行“空命令”,并将其输出存储在变量
p
中。不过,您更想做的是运行一个命令,将密码文件的内容输出到stdout
。所以:这就能解决问题。
Your problem lies in line 2:
What you are doing here is to run an "empty command" in a subshell, storing its output in the variable
p
. What you rather want to do though, is to run a command that outputs the contents of the password file tostdout
. So:This will do the trick.
您可能需要指定文件的完整路径。或者在您的 cron 作业中,首先 cd 到包含该文件的目录。
You probably need to specify a full path to the file. Or in your cron job, first cd to the directory containing that file.
--passphrase
真的存在吗?根据手册页,它没有,但版本可能不同。Does
--passphrase
really exist? According to the manpage it doesn't, but the versions might be different.密码文件在哪里?
cron
具有不同的PATH
,这可能会导致脚本在您自己运行时表现不同。可能的解决方案一,放在
脚本的顶部,当脚本运行时,它会
cd
进入脚本的目录。可能的解决方案二,尝试直接使用绝对路径指定文件:
Where is the password file?
cron
has a differentPATH
, which can cause scripts to behave differently when you run them yourself.Possible solution one, put
at the top of the script, which would
cd
into the script's directory when it runs.Possible solution two, try specifying the file directly with an absolute path: