[英]Bash script works in terminal emulator, but not as i3 keybind
I have a lock screen script (via i3lock) that runs correctly in a terminal window, but not when used as a keybind in i3 config.我有一个锁屏脚本(通过 i3lock),它可以在终端窗口中正确运行,但在 i3 配置中用作键绑定时却不能。
The script is pretty straightforward:脚本非常简单:
# Take screenshot
scrot /tmp/screenshot.png
# Pixelate and add central logo
magick /tmp/screenshot.png -scale 10% -scale 1000% -gravity center /home/user/bin/archlogo.png -composite /tmp/lock.png
# Lock using pixelated image & logo
i3lock -e -f -c 000000 -i /tmp/lock.png
The script is saved to ~/bin/lock
and executable.脚本保存到
~/bin/lock
并且可执行。 The directory has also been identified in my ~/.bash_profile
.该目录也已在我的
~/.bash_profile
标识。
There is no error message, only brief flash of the terminal cursor.没有错误信息,只有终端光标的短暂闪烁。
The relevant lines in ~/.config/i3/config
: ~/.config/i3/config
的相关行:
# lock screen
bindsym $mod+l exec lock
(the default keybind for $mod+l was deleted and i3 was restarted). (删除了 $mod+l 的默认键绑定并重新启动了 i3)。
~/.bash_profile
is a configuration file for bash
and as such is only loaded when bash
starts (as an interactive login shell or when it is expclicitly sourced by other bash configuration files, eg ~/.bashrc
). ~/.bash_profile
是bash
的配置文件,因此仅在bash
启动时加载(作为交互式登录 shell 或由其他 bash 配置文件明确提供,例如~/.bashrc
)。 i3
itself does not load anything from ~/.bash_profile
and neither does X.org . i3
本身不会从~/.bash_profile
加载任何内容, X.org也不会。 So unless you start your X session from a bash shell (for example with startx
from the console) any changes to PATH will not have been loaded and i3
will not know about ~/bin
being added to PATH
.因此,除非您从 bash shell(例如从控制台使用
startx
)启动 X 会话,否则不会加载对 PATH 的任何更改,并且i3
不会知道~/bin
被添加到PATH
。
If you want to modify PATH
for i3
itself, then ~/.bash_profile
is not the right place.如果你想修改
i3
本身的PATH
,那么~/.bash_profile
不是正确的地方。 Unfortunately for X sessions the "right place" very much depends on how you start it (eg from the console with start
or with some desktop manager like lightdm
or kdm
).不幸的是,对于 X 会话,“正确的位置”很大程度上取决于您如何启动它(例如,从带有
start
的控制台或某些桌面管理器(如lightdm
或kdm
)。
You could add a setting for PATH
to ~/.pam_environment
, which - at least on Arch - should prepend the given value to the system default setting of PATH
.您可以将
PATH
的设置添加到~/.pam_environment
,它 - 至少在 Arch 上 - 应该将给定的值添加到PATH
的系统默认设置中。 For example, if you put the follwing in your ~/.pam_environment
例如,如果您将以下内容放在
~/.pam_environment
PATH=/home/soundscape/bin
and the default value is并且默认值为
/usr/local/sbin:/usr/local/bin:/usr/bin
you will end up with你最终会得到
/home/soundscape/bin:/usr/local/sbin:/usr/local/bin:/usr/bin
Of course, this value might be further modified by any settings in ~/.bash_profile
or similar files when you start bash
.当然,当你启动
bash
时,这个值可能会被~/.bash_profile
或类似文件中的任何设置进一步修改。
Note: ~/.pam_environment
does not support any parameter expansion, so adding something like注意:
~/.pam_environment
不支持任何参数扩展,所以添加类似
PATH=${PATH}:${HOME}/bin
will not work.不会工作。
I remember something similar... try specifying the absolute path like this, might be that exec
doesn't honor the $PATH
environment variable:我记得类似的事情......尝试指定这样的绝对路径,可能是
exec
不尊重$PATH
环境变量:
# lock screen
bindsym $mod+l exec "${HOME}/bin/lock"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.