请问zenity --password在脚本里怎么用

sh/bash/dash/ksh/zsh等Shell脚本
回复
linuxmm9
帖子: 214
注册时间: 2017-08-19 10:12
系统: linux

请问zenity --password在脚本里怎么用

#1

帖子 linuxmm9 » 2021-03-04 14:09

我想在thunar右键菜单里加一个改变文件权限的按钮 想用zenity来输入密码 不知道脚本里该怎么写
头像
astolia
论坛版主
帖子: 6455
注册时间: 2008-09-18 13:11

Re: 请问zenity --password在脚本里怎么用

#2

帖子 astolia » 2021-03-05 10:19

又是个典型的X-Y Problem。我觉得你实际遇到的问题是想以特权来执行某些命令如chmod,但用sudo的话不会有提示输入密码。
其实很简单,把sudo换用pkexec来执行就行,pkexec在图形环境下自动会调用一个图形对话框来接收密码。
如果需要用到sudo的一些特定的功能,没法换pkexec,那么需要查看sudo的manpage,看它有没有提供从其他方式接收密码的方式。
有两个参数可用

代码: 全选

     -A, --askpass
                 Normally, if sudo requires a password, it will read it from
                 the user's terminal.  If the -A (askpass) option is speci‐
                 fied, a (possibly graphical) helper program is executed to
                 read the user's password and output the password to the stan‐
                 dard output.  If the SUDO_ASKPASS environment variable is
                 set, it specifies the path to the helper program.  Otherwise,
                 if sudo.conf(5) contains a line specifying the askpass pro‐
                 gram, that value will be used.  For example:

                     # Path to askpass helper program
                     Path askpass /usr/X11R6/bin/ssh-askpass

                 If no askpass program is available, sudo will exit with an
                 error.

代码: 全选

     -S, --stdin
                 Write the prompt to the standard error and read the password
                 from the standard input instead of using the terminal device.
都是接收从标准输入传来的密码。由于zenity --password是把输入的密码直接输出到stdout,可以用一个管道解决问题

代码: 全选

zenity --password | sudo -S XXX
linuxmm9
帖子: 214
注册时间: 2017-08-19 10:12
系统: linux

Re: 请问zenity --password在脚本里怎么用

#3

帖子 linuxmm9 » 2021-03-08 0:45

谢谢astolia 问题解决了
回复