【问题标题】:Bash script not working when run outside terminal在终端外运行时,Bash 脚本不起作用
【发布时间】:2011-09-19 19:03:14
【问题描述】:

此脚本在以图形方式启动时无法正常运行(通过双击脚本图标并选择运行),但是,如果从终端调用,则运行正常;不会保存文件或从现有文件加载内容。请帮忙!谢谢。

#!/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

【问题讨论】:

  • 来吧,你现在应该知道“不能正常工作”不是诊断......
  • 抱歉,我星期二不读心术。你必须真正告诉我们什么是行不通的。
  • 对不起。如果脚本通过双击它然后选择运行来运行,它会执行但不会保存文件或从现有文件加载内容。
  • 当您在终端中时,您是在 cd'ing 到一个目录? (我正在猜测)。双击运行怎么知道要在哪个目录?此外,您可能希望在顶部添加 set -vx 以查看调试和跟踪信息。祝你好运!

标签: bash ubuntu gnupg zenity


【解决方案1】:

错误的一个可能原因是您在通过交互式 shell(因此获取您的 .bashrc)和双击(非交互式,而不是获取 .bashrc)执行时有不同的环境

您可以通过执行env > from_terminalenv > double_click 然后使用diff 或类似的东西来比较环境。

您还可以(在完成上述操作后)在您的脚本中获取from_terminal 以查看它是否适用于终端环境。正如其中一个 cmets 所述,set -vx 是您的朋友。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-20
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多