Bash 脚本在终端外运行时不起作用
该脚本在以图形方式启动时无法正常运行(通过双击脚本图标并选择运行),但是如果从终端调用,则可以正常运行;不会保存文件或从现有文件加载内容。请帮忙!谢谢。
#!/bin/bash
# This script provides a simple and secure messaging system for users not
# familiar with gpg or using the terminal. The idea is to keep sensitive
# plaintext files off one's computer.
zenity --question --text="Select operation:" --ok-label="Compose" --cancel-label="Read"
if [[ $? == 0 ]]; then
usr=$(zenity --entry --text="Sender Key ID:")
rec=$(zenity --entry --text="Recipient Key ID:")
pwd=$(zenity --password)
outfile=$(zenity --file-selection --save --confirm-overwrite)
zenity --text-info --editable | gpg -aseu $usr -r $rec --passphrase $pwd --cipher-algo AES256 -o $outfile
else
infile=$(zenity --file-selection)
pwd=$(zenity --password)
gpg -d --passphrase $pwd $infile | zenity --text-info --height=600 --width=800
fi
This script does not function properly when launched graphically (by double clicking script icon and selecting run), however, runs just fine if called from the terminal; will not save a file or load contents from an existing file. Please help! Thank you.
#!/bin/bash
# This script provides a simple and secure messaging system for users not
# familiar with gpg or using the terminal. The idea is to keep sensitive
# plaintext files off one's computer.
zenity --question --text="Select operation:" --ok-label="Compose" --cancel-label="Read"
if [[ $? == 0 ]]; then
usr=$(zenity --entry --text="Sender Key ID:")
rec=$(zenity --entry --text="Recipient Key ID:")
pwd=$(zenity --password)
outfile=$(zenity --file-selection --save --confirm-overwrite)
zenity --text-info --editable | gpg -aseu $usr -r $rec --passphrase $pwd --cipher-algo AES256 -o $outfile
else
infile=$(zenity --file-selection)
pwd=$(zenity --password)
gpg -d --passphrase $pwd $infile | zenity --text-info --height=600 --width=800
fi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
导致该错误的可能原因是,通过交互式 shell 执行(从而获取
.bashrc
)和双击(非交互,而不是获取.bashrc< /code>
您可以通过执行
env > from_terminal
与env > double_click
然后使用diff
或类似的方法来比较环境。你也可以(做完之后上面)在脚本中使用 source
from_terminal
来查看它是否适用于终端环境,如评论之一所述,set -vx
是您的朋友。A probable cause for the error is that you have different environments when executing via an interactive shell (thus sourcing your
.bashrc
) and double-clicking (non-interactive, and not sourcing.bashrc
You can compare the environments by doing an
env > from_terminal
vs.env > double_click
and then usingdiff
or something similar.You could also (after doing the above) source
from_terminal
in your script to see if it works with the terminal environment. As stated in one of the comments,set -vx
is your friend.